0

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

所以问题从这里开始,我想让鸟在没有按下任何按钮时每 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();

}
4

2 回答 2

1

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

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();
}
于 2014-03-14T03:02:44.883 回答
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 
于 2014-03-14T01:27:41.223 回答