Question

Okay, so I came up with this brilliant idea earlier to practice ActionScript 3.0, I decided I would create a flappy bird clone. I have the basic bird movements down, so the bird can move up and down appropriately and he'll rotate and it looks really slick.

But I have a problem with a boolean variable and an if statement. It isn't really bothering the code being there but I would like to know why it is doing what it is doing. If you run this code below in flash with your own symbol called bird, it'll work fine. You can press any button and the bird will fly up and the fall back down.

var hasGameStarted:Boolean=false
//input stuff
stage.addEventListener(TouchEvent.TOUCH_TAP, tap1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, tap1);

function tap1(event):void{
    if (!hasGameStarted){
        startGame();
    }
}
function startGame():void{  
    hasGameStarted=true;

    var jumpLevel:int=10
    var jumpLevelCap:int=36

    removeEventListener(KeyboardEvent.KEY_DOWN, tap1);
    removeEventListener(TouchEvent.TOUCH_TAP, tap1);

    stage.addEventListener(Event.ENTER_FRAME, update);
    stage.addEventListener(TouchEvent.TOUCH_TAP, tap2);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, tap2);

    function update(event):void{
        bird.y+=jumpLevel;
        //gravity
        if (jumpLevel<jumpLevelCap){
            jumpLevel+=6;
        }
        //rotation
        if (bird.rotation<100 && bird.rotation>-90 && jumpLevel>0){
            bird.rotation+=jumpLevel;
        }
        if (bird.rotation<100 && bird.rotation>-100 && jumpLevel<0){
            bird.rotation+=jumpLevel*2;
        }
        if (bird.rotation>=100){
            bird.rotation=99;
        }
        if (bird.rotation<-90){
            bird.rotation=-89;
        }
        //out of bounds
        if (bird.y<1){
            killBird();
        }
    }

    function tap2(event):void{
        jumpLevel=-30;
    }

    function killBird():void{

    }
}

However, if you run the exact same code with all things pertaining to the boolean variable hasGameStarted removed from the code or commented, the program will get ridiculously fast. There are no other variables depending on this boolean, and it is not needed at all.

//var hasGameStarted:Boolean=false
//input stuff
stage.addEventListener(TouchEvent.TOUCH_TAP, tap1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, tap1);

function tap1(event):void{
        //if (!hasGameStarted){
        startGame();
        //}
}
function startGame():void{    
        //hasGameStarted=true;
...

If you need to know exactly what happens, it will pretty much function the same besides the bird will move ridiculously fast and will move faster every time you press a button.

Please help, I'm still learning, I started like a week ago.

Was it helpful?

Solution

You added the listener tap1 for TouchEvent.TOUCH_TAP to Stage, but in startGame you are removing it from the current object, not Stage (at lines 17 & 18). If you don't have that boolean, every tap call will call startGame.

The solution is to correctly remove the previous listeners:

stage.removeEventListener(KeyboardEvent.KEY_DOWN, tap1);
stage.removeEventListener(TouchEvent.TOUCH_TAP, tap1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top