【问题标题】:timerEvent within enterFrame function?enterFrame 函数中的 timerEvent?
【发布时间】:2012-12-18 09:34:54
【问题描述】:

我正在从 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 上。请帮忙。谢谢。

【问题讨论】:

  • 您需要为此使用增量,并自己跟踪时差,如果您将计时器事件粘在 enterframe 事件循环中,您将遇到麻烦。

标签: actionscript-3 timer


【解决方案1】:

您为什么不放弃计时器并使用进入帧侦听器来计算时间?您已经从 enterframe 侦听器调用 weaponCheck()。使实际的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

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

【讨论】:

  • 非常感谢 vesper,听起来很合乎逻辑,我今晚会尝试实现它并分享结果。
  • 嘿,感谢晚祷完成 :D,你就是那个人。昨晚我花了将近 4 个小时的时间在它上面,但可以这样想,我发现它很有帮助,现在我的其他东西,比如放置新武器和敌人会容易得多。关于这个项目的 as3 开发,我可能会在未来咀嚼你的大脑。百万谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 2011-08-18
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多