enterFrame函数中的timerEvent?

时间:2012-12-18 09:34:54

标签: actionscript-3 timer

我从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且weaponState更改时,第二次触发功能doubleGun();被调用,但它开始按每帧计数发射子弹,而不是在受控的timerEvent上。请帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

为什么不放弃定时器并使用输入帧监听器作为计算时间的方式?您已经从enterframe侦听器调用weaponCheck()。这样做是为了让实际的gun()doublegun()调用只生成动画,例如firin' mah lazers和blasts,主要功能只计算时间。

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是一个用于跟踪时间的变量,它应该是被解雇者的属性。

注意,这种方法要求您在其他地方管理您的&#34; 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

您很可能无法以此方式执行此操作,但请尝试。