Action Script 3.0计时器不会停止

时间:2014-03-14 01:21:16

标签: actionscript-3 timer flappy-bird-clone

我正试图在动作中制作飞扬的小鸟(只是为了练习和乐趣)。这是我的第一个编程语言,我还是新手。

所以问题从这里开始,我想在没有按下按钮的情况下每2秒钟使鸟旋转(就像真正的飞鸟一样)。但事实证明,在我再次按下空格键后,计时器仍然会激活,我认为它应该在激活新的计时器之前先停止最后一个计时器。

如果我按空格键2次,计时器将激活两次。不先停止计时器。

代码:

stage.addEventListener (KeyboardEvent.KEY_DOWN, jump);

function jump(event: KeyboardEvent):void
{
var myTimer4:Timer = new Timer (2000)

    if(event.keyCode == 32)
        {bird.y=bird.y-40;
         bird.rotation=0;
            myTimer4.stop();
            myTimer4.start();
}


myTimer4.addEventListener(TimerEvent.TIMER, fall);

function fall (e:TimerEvent):void{

    bird.rotation=40;
    myTimer4.stop();

}

2 个答案:

答案 0 :(得分:1)

我认为问题可能是你每次按下键并且myTimer4接受新的引用时都会创建一个新的Timer实例。尝试将其移除到功能范围之外,如下所示:

var myTimer4:Timer = new Timer (2000);

function jump(event: KeyboardEvent):void
{


    if(event.keyCode == 32)
        {bird.y=bird.y-40;
         bird.rotation=0;
            myTimer4.stop();
            myTimer4.start();
}

答案 1 :(得分:0)

onClipEvent (load) {

power = 0.3;
yspeed = 0;
xspeed = 0;
friction = 0.95;
gravity = 0.5;
thrust = 3.75;
wind = 0.18;
_root.level1_text.text = 0+collected_coin19;
reverse = new Sound();
reverse.attachSound("hit2");



}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
    xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
    xspeed += power;
}
if (Key.isDown(1)) {
    yspeed -= power*thrust;
}
if (Key.isDown(Key.SPACE)) {
    yspeed -= power*thrust;
}
xspeed += wind;
xspeed *= friction;
yspeed += gravity;
_y += yspeed;
_x += xspeed;

Try This one Might Help you