[英]AS3 Game, Collecting points, multiple movie clips with the same instance name, not working
我決定創建一個Android觸摸屏游戲。 我是一個完整而完全的初學者,並且我正在學習。
我的游戲中有一只小象,當您按住屏幕時會向上移動,而在沒有與屏幕接觸時會掉下。 目的是收集盡可能多的花生飛過以獲得最高分。 很簡單,您會這么認為。
到目前為止,我已經設法達到大象可以與花生碰撞而花生消失的地步。
我現在的問題是,我不能創建一個以上的花生,並且實例名稱為“花生”,因為只有一個花生能起作用,而其他花生不能被識別。 我在google上做了很好的搜索,沒有任何事情給我正確的選擇。 有人可以給我一個明確的答案,告訴我從這里做什么或去哪里?
如果您需要更多信息,請提供到目前為止我可以幫助您理解的代碼或圖片,讓我知道:)
實例名稱必須是唯一的,並且您不能使用實例名稱來查找一組影片剪輯。 您應該改為使用數組,並在創建花生時使用say push()
將其添加到那里,並在收集花生時將其拼接起來。
實際上,每當您獲得具有類似功能的多實例類(也稱為“收集”)時,都可以使用Array
來存儲對所有這些的引用,因此您將始終知道可以通過該數組訪問所有實例。
示例代碼:
var peanuts:Array=new Array();
function addPeanut(x:Number,y:Number):void {
var peanut:Peanut=new Peanut(); // make a peanut, you do this somewhere already
peanut.x=x;
peanut.y=y;
peanuts.push(peanut); // this is where the array plays its role
game.addChild(peanut); // let it be displayed. The "game" is whatever container
// you already have to contain all the peanuts.
}
function removePeanut(i:int):void {
// give it index in array, it's better than giving the peanut
var peanut:Peanut=peanuts[i]; // get array reference of that peanut
peanuts.splice(i,1); // remove the reference from the array by given index
game.removeChild(peanut); // and remove the actual peanut from display
}
function checkForPeanuts():void {
// call this every so often, at least once after all peanuts and player move
for (var i:int=peanuts.length-1; i>=0; i--) {
// going through all the peanuts in the array
var peanut:Peanut=peanuts[i];
if (player.hitTestObject(peanut)) {
// of course, get the proper reference of "player"!
// YAY got one of the peanuts!
// get some scoring done
// get special effects, if any
removePeanut(i); // remove the peanut
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.