0

我对 AS3 中的 TIMER 有疑问

我的舞台上有一个僵尸物体,我希望他来攻击英雄。

我想做的是:

  1. 僵尸走向英雄
  2. 当他足够接近攻击时,他会继续攻击。
  3. 问题:我希望他每5秒只攻击一次,这样英雄就有机会反击他。问题是我不熟悉计时器,我仍然找不到任何可以帮助我的提示/tuts/答案。我不知道我应该把计时器放在哪里,在一个新的计时器函数中还是在我的僵尸函数中。

谢谢你 :)

这是代码

if (zombie.x>hero.x+50)
{
    zombie.x-=5;
    zombie.scaleX=-1;

    if(zombie.x<hero.x+100){
        zombie.gotoAndStop("attack"); 
        //so that the zombie attacks when the hero is in range

    }
}
4

2 回答 2

0

你应该在你的僵尸上定义“空闲”、“行走”和“攻击”动画,到目前为止我只看到你的僵尸切换到“攻击”姿势并停留在那里。另外,把你的僵尸变成一个类,这样它就会控制自己的动画,知道什么时候攻击和什么时候停止攻击(回到空闲动画)。最后,有一个标志“这个僵尸可以再次攻击”,当僵尸攻击时将设置为true,并flash.utils.setTimeout()使用适当的参数调用来调用将重置此标志的函数。这个基于时间的函数可以用于简单的一次性调用,直到您更好地学习 Actionscript。

于 2013-03-15T08:06:32.400 回答
0

你可以这样做:

var timer:Timer = new Timer(5000);//that's 5 second

if (zombie.x>hero.x+50)
{
    zombie.x-=5;
    zombie.scaleX=-1;

    if(zombie.x<hero.x+100){
        attack();
    }
}

function attack(  ) : void
{
  // attack the first time
  zombie.gotoAndStop("attack"); 

 //than launch the timer
  timer.addEventListener(TimerEvent.TIMER, repeatAttack );
  timer.start();
}

//will be called every 5000 ms == 5 sec
function repeatAttack ( event : TimerEvent ) : void
{
 zombie.gotoAndStop("attack"); 
}

//if you want to stop the attack you can use this function for example

function stopAttack() : void
{
   timer.removeEventListener(TimerEvent.TIMER, repeatAttack );
   timer.stop();//stop timer
   timer.reset();//resetCount to zero   
}

我希望这能帮助你解决你的问题

于 2013-03-15T08:44:55.840 回答