实例名称必须是唯一的,您不能使用实例名称来查找一组影片剪辑。您应该改用数组,并在创建花生时也使用 say 将其添加到那里push()
,并在收集花生时将其拼接出来。
事实上,每当你获得一个具有类似功能的多实例类(又名“收集”)时,使用 anArray
来存储对所有这些的引用,因此你将始终知道你的所有实例都可以通过该数组访问。
如何使用数组
示例代码:
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
}
}
}