0

我正在制作一个游戏,其中你是一架试图躲避来袭导弹的飞机。我试图让我的坏人产生并从舞台的顶部移动到底部。目前没有错误,坏人产卵但不动。请帮忙。

import flash.utils.Timer;
import flash.events.TimerEvent;

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var baddies:Array = new Array();
var baddieSpeed:int = 15;
var timer: Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.start();

//deg2rad = Math.PI/180;
//rad2deg = 180/Math.PI;

plane.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);



function fl_MoveInDirectionOfKey_2(event:Event)
{
    if (upPressed)
    {
        plane.y -= 8;
    }
    if (downPressed)
    {
        plane.y += 8;
    }
    if (leftPressed)
    {
        plane.x -= 8;
    }
    if (rightPressed)
    {
        plane.x += 8;
    }
}

function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = true;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = true;
            break;
        }
        case Keyboard.LEFT:
        {
            leftPressed = true;
            break;
        }
        case Keyboard.RIGHT:
        {
            rightPressed = true;
            break;
        }
    }
}

function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = false;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = false;
            break;
        }
        case Keyboard.LEFT:
        {
            leftPressed = false;
            break;
        }
        case Keyboard.RIGHT:
        {
            rightPressed = false;
            break;
        }
    }
}

timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.start();
function addBaddie(evt:TimerEvent):void {
    var baddie:Baddie = new Baddie();
    baddie.x = - baddie.width
    baddie.y = Math.random() * stage.stageHeight;
    baddie.angle = getAngle(baddie.x, baddie.y, target.x, target.y);
    baddie.rotation = getAngle(baddie.x, baddie.y, target.x, target.y);
    baddie.speed = baddieSpeed
    addChild(baddie);
    baddies.push(baddie);
    }


var counter:int = 0;
function moveBaddies():void {
    for (var i:int = 0; i < baddies.length; i++) {
        var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
        var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
        baddies[i].x += dx;
        baddies[i].y += dy;
    }
}


function deg2rad(deg:Number):Number {
    return deg * (Math.PI/180);
}

function rad2deg(rad:Number):Number {
    return rad * (180 / Math.PI);
}



function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
    var radians:Number = Math.atan2(y2 - y1, x2 - x1);
    return rad2deg(radians);
}
4

2 回答 2

0

看起来您正在调用它两次:

timer.addEventListener(TimerEvent.TIMER, addBaddie);

我建议您创建一个专用于移动敌人的计时器:

var baddieMover:Timer = new Timer(40);
baddieMover.addEventListener(TimerEvent.TIMER, moveBaddies);
baddieMover.start();
var counter:int = 0;
function moveBaddies(evt:TimerEvent):void {
    for (var i:int = 0; i < baddies.length; i++) {
        var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
        var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
        baddies[i].x += dx;
        baddies[i].y += dy;
    }
}
于 2013-06-05T17:50:29.207 回答
0

我相信你已经忘记调用这个Movebaddies函数了。正如 EgoAnt 所说 - 为了保持干净,我建议您为该功能创建一个新的 Listener。

于 2013-06-05T18:52:52.123 回答