在阵列AS3中移动硬币

时间:2014-05-30 12:34:08

标签: arrays actionscript-3

我正在尝试创建一个可以创建4个硬币的数组(我已经完成了),但问题是我正在尝试向下移动硬币,我无法弄清楚如何访问数组列表移动硬币(注释掉len​​ght [i] .y + = 4)这是行不通的。另一个问题是我想在更新循环中每帧只创建4个硬币而不是4个硬币。

感谢。

public class Coin extends Sprite
{
    //..SPRITES
    public static var coinSprite:CoinSprite;

    public var _coins:Array = new Array();
    public var c = new CoinSprite();
    public static var COINCOUNT:int = 4;

    //-------------------------------------------------------------------------|
    //##SETUP------------------------------------------------------------------|
    //-------------------------------------------------------------------------|
    public function Coin()
    {
        super();
        this.addEventListener(starling.events.Event.ADDED_TO_STAGE, stageSetup);
    }

    public function stageSetup(event:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, stageSetup);
    }
    //-------------------------------------------------------------------------|

    //-------------------------------------------------------------------------|
    //##MANAGER----------------------------------------------------------------|
    //-------------------------------------------------------------------------|

    public function manager():void {
        this.addEventListener(Event.ENTER_FRAME, updateCoin);
    }
    //-------------------------------------------------------------------------|


    //-------------------------------------------------------------------------|
    //##COINHANDLER------------------------------------------------------------|
    //-------------------------------------------------------------------------|

    public function coinHandler():void {

        var length:Number = 4;

        for ( var i:Number = 0; i < length; i++) {
            //length[i].y += 4;
            createCoin();
            c.y = i * 50;
        }

    }
    //-------------------------------------------------------------------------|

    //-------------------------------------------------------------------------|
    //##COLLISION||------------------------------------------------------------|
    //-------------------------------------------------------------------------|

    public function hitCoinPlayer():void {
        //..cleanup
        if (c.hitTestObject(Player.playerSprite)) {
            c.alpha = 0;
        }
        //..check bounds
        if (c.y >= 1500) {
            c.y = -200;
            c.alpha = 1;
        } 
    }
    //-------------------------------------------------------------------------|



    //-------------------------------------------------------------------------|
    //##UPDATE-----------------------------------------------------------------|
    //-------------------------------------------------------------------------|

    public function updateCoin(e:Event):void {
        //coinHandler();
    }
    //-------------------------------------------------------------------------|

    //-------------------------------------------------------------------------|
    //##CREATE COIN------------------------------------------------------------|
    //-------------------------------------------------------------------------|
    public function createCoin():void {
        c = new CoinSprite();
        c.x = 305;
        Starling.current.nativeOverlay.addChild(c);
        _coins.push(c);
    }
    //-------------------------------------------------------------------------|
}

1 个答案:

答案 0 :(得分:2)

public var _coins:Array = {}; // you need an array to store the coins you've created
public function stageSetup(event:Event):void {
    coinHandler(); //create four coins upon setting up the stage
    this.removeEventListener(Event.ADDED_TO_STAGE, stageSetup); //done setup
}

//other parts of the code are not changed

public function coinHandler():void { // you only need to call this once from ur stagesetup

    var length:Number = 4;

    for ( var i:Number = 0; i < length; i++) {

        createCoin();
        c.y = i * 50;
    }

}

public function updateCoin(e:Event):void {
    moveCoin();
}

public function moveCoin(){
    for ( var i:Number = 0; i < _coins.length; i++) {

        _coins[i].y +=4;
    }
}

public function createCoin():void {
    c = new CoinSprite();
    c.x = 305;
    Starling.current.nativeOverlay.addChild(c);
    _coins.push(c);
}