2

I have made a game in flash CS6 using AS3 and Air 3.2 for Android, now this game you go to different menus, when you die, it will go back to main menu and show your score, now when you select a new character and play through again, the monsters speed are doubled!?!?

Any idea why? I can give you a piece of my code but I'm really not sure on what part is the problem? would it be a event listener that wasn't deleted?

Here is the function that is called to started the level off

public function startLevel1( navigationEvent:NavigationEvent ):void
    {
        //classSelect = null;
        removeChild( classSelect );
        levelManager = new LevelManager( heroGra, hero);
        addChild( levelManager );
        levelManaOn = true;

        gameTimer = new Timer( 30 );
        //On every 30ms we call apon moveEvent function
        gameTimer.addEventListener( TimerEvent.TIMER, tick );
        gameTimer.start();

    }

Here is the tick event that is deleted that calls the updated function for the monsters

    public function tick( timerEvent:TimerEvent ):void
    {
        if(levelManaOn == true)
        {
            levelManager.update();
            if(hero.hp <= 0)
            {
                trace("DEAD");
                onScoreState();
                levelManaOn = false;
                removeEventListener( TimerEvent.TIMER, tick );
            }
        }
    }

From tick event, it will call this function

public function onScoreState( ):void
    {
        scoreState = new ScoreState();
        scoreState.waveCompletedScore.text = levelManager.level.score.toString();
        //
        scoreState.addEventListener( NavigationEvent.ENDGAME, backMainMenu );
        addChild( scoreState );
        removeChild( levelManager );
    }

this removes levelManager, but still the monsters move at double the speed, and every time you restart from the beginning after dieing, the speed is doubled again, and again, any idea why?

Thank you reading and for the help

Canvas

Canvas
  • 5,779
  • 9
  • 55
  • 98

1 Answers1

3
    gameTimer = new Timer( 30 );
    //On every 30ms we call apon moveEvent function
    gameTimer.addEventListener( TimerEvent.TIMER, tick );
    gameTimer.start();

I bet this code is being executed again without the first timer being removed.

You need to call removeEventListener( TimerEvent.TIMER, tick ); as a method on your gameTimer object. Like this:

gameTimer.removeEventListener( TimerEvent.TIMER, tick );

Make sure you keep a reference to gameTimer. Also do gameTimer.stop(); before you remove the listener.

Austin Henley
  • 4,625
  • 13
  • 45
  • 80
  • gameTimer.reset() is also good for when you don't recreate the timer and use the same one. Move the creation of the timer out of the start level and just start it there. – Gone3d Jan 08 '13 at 21:27
  • 1
    Cheers mate, it works correctly now :), well, it's good too know i was on the right track :) – Canvas Jan 08 '13 at 21:43