在数组中移动对象

时间:2011-04-14 16:28:21

标签: arrays flash actionscript-3

我有一个阵列,里面装满了应该移动的平台。

    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个移动平台。但只有一个上下移动。但他们都注意到小鸟的触摸。难道我做错了什么?非常感谢帮助:)

3 个答案:

答案 0 :(得分:2)

在你的听众中,你只设置一个平台的位置,这个平台只有一个“movingplatform”是对它的引用。由于所有移动平台的舞台实例都被命名为“movingplatform”,因此一个幸运平台将被名称引用(其余被忽略),而不是您想要的,即使用数组中的引用并调整每个平台。

您可能希望 movingplatform 成为事件处理程序中的局部变量,声明如下:

var movingplatform:DisplayObject = MovingPlatformArray[c] as DisplayObject;

我建议在每个循环中使用代替 for in ,因为我觉得它更干净一点,但这是一个小风格的东西:< / p>

for each (var platform:DisplayObject in MovingPlatformArray)
{
    platform.y += speed;
    ... rest of your code ...
}

为了清楚起见,我将循环变量编辑为 platform 而不是movingplatform,以避免混淆局部变量影子作为舞台实例(即this.movi​​ngplatform)。我希望很清楚这里没有使用舞台实例名称,因为代码中无意的实例名称引用首先是问题的根源。

答案 1 :(得分:1)

就我而言,你有两种选择。每个人使用a,就像adam smith所建议的那样,或者使用for循环,因为它是打算使用的:)

for(var c:uint = 0; c < MovingPlatformArray.length; c++){...

和btw:应该是“MovingPlatform [c] .y + = speed;”不是“MovingPlatformArray [c] .y + = speed;”?

编辑:查看你的代码,我还建议你使用MovingPlatformArray [c] .hitTestObject(birdie)而不是MovingPlatformArray [c] .hitTestPoint(birdie.x,birdie.y,true)

答案 2 :(得分:0)

如果我是你,我会把平台的逻辑带出来,然后把它存放在一个类中。 (理想情况下,你也会为小鸟对象做这个)。我在下面创建了一个例子。舞台上的动画片段应该扩展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);
        }       
    }
}