简体   繁体   English

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

[英]AS3 Game, Collecting points, multiple movie clips with the same instance name, not working

I've decided to create an Android touch screen game. 我决定创建一个Android触摸屏游戏。 I am a complete and utter beginner and am learning as I go. 我是一个完整而完全的初学者,并且我正在学习。

My game has a little elephant that moves up when you press and hold on the screen and falls when there is no contact with the screen. 我的游戏中有一只小象,当您按住屏幕时会向上移动,而在没有与屏幕接触时会掉下。 The aim is to collect as many peanuts that fly past as possible to gain the highest score. 目的是收集尽可能多的花生飞过以获得最高分。 Pretty simple, you'd think so. 很简单,您会这么认为。

So far, I've managed to get to the point where the elephant can collide with a peanut and the peanut disappears. 到目前为止,我已经设法达到大象可以与花生碰撞而花生消失的地步。

My issue right now is, I can't create more than one peanut, with the same instance name of "peanut" because only the one will work and the others will not be recognized. 我现在的问题是,我不能创建一个以上的花生,并且实例名称为“花生”,因为只有一个花生能起作用,而其他花生不能被识别。 I've done a good ole google search and nothing has really given me the right way to go. 我在google上做了很好的搜索,没有任何事情给我正确的选择。 Could someone give me a clear answer of what to do or where to go from here? 有人可以给我一个明确的答案,告诉我从这里做什么或去哪里?

If you need any more info, the code or a picture of what i've got so far to help you understand just let me know :) 如果您需要更多信息,请提供到目前为止我可以帮助您理解的代码或图片,让我知道:)

  • Samantha 萨曼莎(Samantha)

Instance name must be unique, and you cannot use instance name to find a set of movie clips. 实例名称必须是唯一的,并且您不能使用实例名称来查找一组影片剪辑。 You should instead use an array, and at creating a peanut add it there too using say push() , and at collecting a peanut, splice it out. 您应该改为使用数组,并在创建花生时使用say push()将其添加到那里,并在收集花生时将其拼接起来。

In fact, whenever you get a multi-instance class with similar functionality (aka "collect"), use an Array to store references to all of these, so you will always know that ALL of your instances are accessible through that array. 实际上,每当您获得具有类似功能的多实例类(也称为“收集”)时,都可以使用Array来存储对所有这些的引用,因此您将始终知道可以通过该数组访问所有实例。

How to work with arrays 如何使用数组

A sample code: 示例代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM