0

我正在从 enterFrame 调用 timerEvent 函数,它在 everyFrame 上运行 timerEvent 函数。有没有办法控制它?

我的子弹射击功能的 timerEvent 为 500,所以它以 24fps 的速度每半秒发射一次子弹。它现在工作正常。现在我想改变武器的子弹速度和皮肤。

////////////////////////////////
//this function is called first time within an EnterFrame function
///////////////////////////////



function weaponCheck():void
{
switch (weaponState)
{
    case STATE_GUN :
        gun();
        break;
    case STATE_DOUBLE_GUN :
        doubleGun();
        break;
    }
}


function gun():void
{
trace("single gun");
laserTimer = new Timer(600);
laserTimer.addEventListener(TimerEvent.TIMER, timerListener);
laserTimer.start();
function timerListener(e:TimerEvent):void
{
    var tempLaser:MovieClip = new Laser();
    var tempGunBlast:MovieClip = new Gun_blast_01();
    tempLaser.x = player.x +((player.width/2)+12);
    tempLaser.y = player.y;

    tempGunBlast.x = stage.mouseX + 104;
    tempGunBlast.y = tempLaser.y;
    Lasers.push(tempLaser);
    addChildAt(tempLaser,1);
    addChildAt(tempGunBlast, 3);

    if (tempGunBlast.currentFrame >= tempGunBlast.totalFrames)
    {
        removeChild(tempGunBlast);
    }

    }

}

function doubleGun():void
{
trace("Double gun");
doubleGunTimer = new Timer(400);
doubleGunTimer.addEventListener(TimerEvent.TIMER, timerListener2);
doubleGunTimer.start();
function timerListener2(e:TimerEvent):void
{
    var tempLaser:MovieClip = new doubleG();
    var tempGunBlast:MovieClip = new Gun_blast_01();
    tempLaser.x = player.x +((player.width/2)+12);
    tempLaser.y = player.y;

    tempGunBlast.x = stage.mouseX + 104;
    tempGunBlast.y = tempLaser.y;
    Lasers.push(tempLaser);
    addChildAt(tempLaser,1);
    addChildAt(tempGunBlast, 3);

    if (tempGunBlast.currentFrame >= tempGunBlast.totalFrames)
    {
        removeChild(tempGunBlast);
    }
}
}



///////////////////////////////////////////////////
//Following function is called within an enterFrame event function
/////////////////////////////////////////////////

function playGame():void
{
weaponCheck();
blah1();
blah2();
blah3();
testForEnd();
}




function testForEnd():void
{

if (level == 3)
{
    laserTimer.stop();
    weaponState = STATE_DOUBLE_GUN;
    weaponCheck();
}


}

所以当游戏第一次运行时,它工作正常并使用 600 的计时器事件来击中子弹,但是当 level == 3 并且武器状态发生变化时,第二次射击函数 doubleGun(); 被调用,但它开始在每帧计数上发射子弹,而不是在受控的 timerEvent 上。请帮忙。谢谢。

4

1 回答 1

1

为什么不放弃计时器并使用输入帧侦听器作为一种计算时间的方式?您已经weaponCheck()从一个 enterframe 侦听器调用。使实际gun()doublegun()调用将只生成动画,例如 firin' mah lazers 和 blasts,而 main 函数将只计算时间。

function weaponCheck():void
{
    this.reloading+=this.weaponFiringSpeed; // you alter this to make your weapon fire slower or faster
    if (this.reloading<FULLY_RELOADED) return; // we are not yet ready to fire
    this.reloading=0;
    switch (weaponState) // and here we fire with the gun state
    {
        case STATE_GUN :
            gun();
            break;
        case STATE_DOUBLE_GUN :
            doubleGun();
            break;
    }
}

function gun():void
{
    trace("single gun");
    var tempLaser:MovieClip = new Laser();
    var tempGunBlast:MovieClip = new Gun_blast_01();
    tempLaser.x = player.x +((player.width/2)+12);
    tempLaser.y = player.y;
    tempGunBlast.x = stage.mouseX + 104;
    tempGunBlast.y = tempLaser.y;
    Lasers.push(tempLaser);
    addChildAt(tempLaser,1);
    addChildAt(tempGunBlast, 3);
}

同样是双枪。FULLY_RELOADED是一个常数,reloading是一个用来跟踪时间的变量,它应该是开火者的一个属性。

注意,这种方法需要你在别处管理你的“tempGunBlast”,也许在weaponCheck函数中,如果是这样,修改如下:

function weaponCheck():void
{
    if (tempGunBlast) if (tempGunBlast.currentFrame >= tempGunBlast.totalFrames)
        removeChild(tempGunBlast);
    this.reloading+=this.weaponFiringSpeed; // you alter this to make your weapon fire slower or faster
    if (this.reloading<FULLY_RELOADED) return; // we are not yet ready to fire
    ... // rest of code unchanged

您很可能无法复制粘贴实现此功能,但请尝试。

于 2012-12-18T11:15:07.113 回答