AS3游戏,收集点数,多个电影剪辑具有相同的实例名称,不起作用



我决定创建一款Android触摸屏游戏。我是一个完全的初学者,边学习边学习。

我的游戏有一头小象,当你在屏幕上按住时,它会向上移动,当你与屏幕没有接触时,它就会下降。目的是收集尽可能多的飞过的花生,以获得最高分数。很简单,你会这么认为的。

到目前为止,我已经设法做到了大象可以与花生相撞,花生就会消失的地步。

我现在的问题是,我不能用相同的实例名"花生"创建多个花生,因为只有一个可以工作,其他的不会被识别。我在谷歌上搜索得很好,但没有什么能真正给我正确的选择。有人能给我一个明确的答案,告诉我从这里该做什么或去哪里吗?

如果你需要更多的信息,代码或图片,我到目前为止有什么可以帮助你理解,只需告诉我:)

  • Samantha

实例名称必须唯一,并且不能使用实例名称查找一组电影片段。相反,您应该使用一个数组,在创建花生时,也可以使用push()将其添加到那里,在收集花生时,将其拼接出来。

事实上,每当您获得具有类似功能的多实例类(也称为"collect")时,都可以使用Array来存储对所有这些的引用,因此您将始终知道all实例可以通过该数组访问。

如何使用阵列

示例代码:

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
        }
    }
}

最新更新