以as3

时间:2018-09-15 20:05:39

标签: actionscript-3

我在制作游戏时遇到了麻烦。我的角色会射击子弹,但以不同方向射击子弹时,它无法正常工作。射击时,子弹会遵循角色的方向。我在下面发布代码。

var Bulli:Array = new Array();
var ShootTimer:Timer = new Timer(0);
stage.addEventListener(MouseEvent.MOUSE_DOWN, startShootTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, stopShootTimer);
ShootTimer.addEventListener(TimerEvent.TIMER, shootBullo);
stage.addEventListener(Event.ENTER_FRAME, mainLoop);

function startShootTimer(e:MouseEvent):void
{
    ShootTimer.start();
}
function stopShootTimer(e:MouseEvent):void
{
    ShootTimer.stop();
}       
function shootBullo(e:TimerEvent):void
{
    var bullo:Bullo = new Bullo();
    bullo.x = Jumbo.x;
    bullo.y = Jumbo.y - 50; 

    if(destra)
    {
        bullo.dir = destra;
    }
    else
    {
        bullo.dir = sinistra;
    }

    addChild(bullo);
    Bulli.push(bullo);
}       
function mainLoop (e:Event):void
{
    for (var b:int = 0; b < Bulli.length; b++)
    {
        if (Bulli[b].dir == destra)
        {
            Bulli[b].x += 10;
        }
        else
        {
            Bulli[b].x -= 10;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

不要将该侦听器添加到Stage中,而是将其添加到每个唯一的bullo ...

//# not to Stage...
//stage.addEventListener(Event.ENTER_FRAME, mainLoop);

尝试一下(试用,但可能对某些想法有用)

function shootBullo(e:TimerEvent):void
{
    var bullo:Bullo = new Bullo();
    bullo.x = Jumbo.x;
    bullo.y = Jumbo.y - 50; 

    if(destra) { bullo.dir = destra; }
    else { bullo.dir = sinistra; }

    bullo.addEventListener(Event.ENTER_FRAME, mainLoop);
}

function mainLoop (e:Event):void //the "e" of this function parameter is each unique "Bullo"
{
    //# currentTarget is whichever "bullo" is talking to this Event (via Listener).
    if (e.currentTarget.dir == destra) 
    { e.currentTarget.x += 10; }

    else { e.currentTarget.x -= 10; }

}