1

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

我目前正在制作一款专为 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);
}
4

1 回答 1

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();
于 2013-07-01T18:14:16.663 回答