0

I'm trying to make flappy birds in actionscript (just for practicing and fun). This is my first programming language and I'm still new to this.

So the problem starts here, I want to make the bird rotates (as the real flappy bird does) every 2 seconds when no button is pressed. But it turned out that the timer still activates after I pressed spacebar again which I think it is supposed to stop the last timer first before activating the new one.

If I press spacebar 2 times, the timer will activate twice. Without stopping the timer first.

Code :

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();

}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3411184
  • 71
  • 1
  • 10

2 Answers2

1

I think the problem might be you are creating a new instance of Timer every time the key is pressed and myTimer4 takes in a new reference. Try removing it outside the function scope like this:

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();
}
Mc Kevin
  • 962
  • 10
  • 31
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