数组中的移动对象

| 我有一个到处都是应该移动的平台。
    var MovingPlatformArray:Array = new Array();

    for (var c:int = numChildren - 1; c >= 0; c--){
        var child3:DisplayObject = getChildAt(c);
        if (child3.name == \"movingplatform\"){
            MovingPlatformArray.push(child3);
        }
    }

    this.addEventListener(Event.ENTER_FRAME,ctrl_birdie);

    function ctrl_birdie(e:Event):void{

        for(var c in MovingPlatformArray){

    MovingPlatform[c].y += speed;

   if(MovingPlatformArray[c].hitTestPoint(birdie.x,birdie.y,true)){
        birdtelleryvertrager=0;
        birdtellery = 0;
        birdie.y-=14;
    }

        if(movingplatform.y <= 25){
                speed = 2;  
        }

        if(movingplatform.y >= 350){
             speed = -2;  
        }

   }
现在,我在这个阵列中有2个移动平台。但是只有一个上下移动。但是他们都和小鸟保持了联系。难道我做错了什么?非常感谢帮助:)     
已邀请:
就我而言,您有两种选择。如adam smith所建议,对每个变量使用a,或者按原计划使用for循环:)
for(var c:uint = 0; c < MovingPlatformArray.length; c++){...
顺便说一句:\“ MovingPlatformArray [c] .y + = speed; \”应该不是\“ MovingPlatformArray [c] .y + = speed; \”吗? 编辑:查看您的代码,我也建议您使用MovingPlatformArray [c] .hitTestObject(birdie)而不是MovingPlatformArray [c] .hitTestPoint(birdie.x,birdie.y,true)     
在您的侦听器中,您仅设置一个平台的位置,而“移动平台”是该平台的参考。因为您所有移动平台的舞台实例都被命名为\“ movingplatform \”,所以一个幸运的平台正被名称引用(其余部分被忽略),而不是您想要的,而是使用数组中的引用并调整每个平台。 您可能打算将移动平台作为事件处理程序中的局部变量,声明如下:
var movingplatform:DisplayObject = MovingPlatformArray[c] as DisplayObject;
我建议为每个循环使用for来代替for in,因为我认为它更干净一些,但这是次要的事情:
for each (var platform:DisplayObject in MovingPlatformArray)
{
    platform.y += speed;
    ... rest of your code ...
}
为了清楚起见,我将循环变量编辑为平台而不是moveplatform,以避免混淆在舞台实例(即this.movi​​ngplatform)中包含局部变量。我希望清楚一点,此处未使用阶段实例名称,因为代码中的无意实例名称引用首先是问题的根源。     
如果您是我,我将带出平台的逻辑,并将其存储在一个类中。 (理想情况下,您也会对小鸟对象执行此操作)。我在下面创建了一个示例。舞台上的动画片段应扩展Platform而不是MovieClip,以便它们调用底部的方法。
// Use vectors if you know all the items are going to be the same type
var platforms:Vector.<Platform> = new <Platform>[];

for (var c:int = numChildren - 1; c >= 0; c--){
    var child:DisplayObject = getChildAt(c);

    // You shouldn\'t check against names (as per the original post). Because
    // names should be unique
    if (child is Platform){
        platforms.push(child);

        // This could be random so each platform has a different range
        // This means platform 1 could go from y 30 to y 400, platform 2
        // could go from y 60 to y 200, etc
        child.setRange(25, 400); 
    }
}

this.addEventListener(Event.ENTER_FRAME, gameLoop);

// Have an overall game loop
function gameLoop(e:Event):void {

    // Loop over the platforms
    platforms.forEach(function(item:Platform, i:int, a:Vector.<Platform>):void {
        // Hit test function in the class means you only have to pass in one mc 
        // rather than the points and a boolean
        if(item.hitTest(birdie)) {
            birdtelleryvertrager=0;
            birdtellery = 0;
            birdie.y-=14;
        }

        // Removed the movement logic, this should be kept out of the game loop
        // plus how much better does this read?
        item.move();
    });
}
然后在某个地方的类位置,例如在文件夹game / activeObjects中
// A class for the platform stored else where
package game.activeObjects
{
    import flash.display.MovieClip;
    /**
     * 
     */
    public class Platform extends MovieClip {

        private const SPEED:Number = 2;

        private var _direction:int = 1;
        private var _minimumHeight:Number = 25;
        private var _maximumHeight:Number = 350;

        public function Platform() {
        }

        public function setRange(minimumHeight:Number, maximumHeight:Number) {
            _minimumHeight = minimumHeight;
            _maximumHeight = maximumHeight;
        }

        public function move():void {
            this.y += SPEED * _direction;

            if(this.y <= _minimumHeight) {
                _direction = 1;
            } else if(this.y >= _maximumHeight) {
                _direction = -1;
            }
        }

        public function hitTest(mc:MovieClip):Boolean {
            return hitTestPoint(mc.x,mc.y,true);
        }       
    }
}
    

要回复问题请先登录注册