AS3垂直移动并随机移动

时间:2013-06-29 15:58:33

标签: android actionscript-3 flash air

我这里有代码,应该在舞台上随机生成一个链接的影片剪辑。它确实随机生成但不在顶部。生成的项目也应该向下移动并消失,但事实并非如此。

我目前正在制作专为Android智能手机设计的游戏..

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Create an enemies array
var enemies:Array;
enemies = new Array();


makeEnemies();
moveEnemies();




//Call this function for how many enemies you want to make...
function makeEnemies():void
{
    var tempEnemy:MovieClip;
    //Make sure a Library item linkage is set to Enemy...
    tempEnemy = new Enemy();
    tempEnemy.speed = 20;
    tempEnemy.x = Math.random()* 800;
    tempEnemy.cacheAsBitmapMatrix = tempEnemy.transform.concatenatedMatrix;
    tempEnemy.cacheAsBitmap = true;
    trace("enemy");
    addChild(tempEnemy);
    enemies.push(tempEnemy);


    tempEnemy.addEventListener(MouseEvent.CLICK, killEnemies);
}




function killEnemies(event:MouseEvent):void {
trace("tap");
}

//Create an enemies array if you do not already have one...
//Create tempEnemy from Enemy library item if you do not already have one...
function moveEnemies():void
{
    var tempEnemy:MovieClip;
    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];

        //rotate the enemy between 10-5 degrees
        tempEnemy.rotation += (Math.round(Math.random()*50-5));
        //Find the rotation and move the x position that direction
        tempEnemy.x -=  (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
        tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
        if (tempEnemy.x < 0)
        {
            tempEnemy.x = 0;
        }
        if (tempEnemy.x > stage.stageWidth)
        {
            tempEnemy.x = stage.stageWidth;
        }

        if (tempEnemy.y > stage.stageHeight)
        {

            //Remove enemy from enemies array
            removeEnemy(i);
            //Subtract a life
            trace("awwwww");
        }
    }
}

function removeEnemy(idx:int)
{
    removeChild(enemies[idx]);
    enemies.splice(idx,1);
}

1 个答案:

答案 0 :(得分:0)

你应该调用moveEnemies();每一帧,如下:

import flash.event.Event;
addEventListener(Event.ENTER_FRAME, onEnter_frame);
function onEnter_frame(e:Event):void {
    moveEnemies();
}

另外,将敌人的类型设置为Enemy而不是MovieClip可能是个好主意

var enemy:Enemy = new Enemy();