KomokoChan
KomokoChan

Reputation: 3

Actionscript 3 timer is unidentified, how do I fix this?

I'm building a memory Flash game, in which there's a timer that gives you a certain amount of time to finish the deck of cards. The code for this timer is shown below:

public function memory():void
{
      levelDuration = 10;
      gameTime = levelDuration;
      var gameTimer:Timer = new Timer(1000,levelDuration);
      gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
      gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired);
      gameTimer.start();
}

function updateTime(e:TimerEvent):void // what happens when the time runs out
    {
        // your class variable tracking each second, 
        gameTime--;
        //update your user interface as needed
        timeText.text = "Tijd : " + String(gameTime); //gameTime is defined before the public function memory

    }

function timeExpired(e:TimerEvent):void
    {
        var gameTimer:Timer = e.target as Timer;
        gameTimer.removeEventListener(TimerEvent.TIMER, updateTime);
        gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired);
        finalScore = score;
        musicchannel.stop();
        gameTimer.stop();

        MovieClip(root).gotoAndStop("gameover");
    }

Now, this works fine. The timer counts down, and when it expires, it brings you to the gameover screen. However, when you finish the game BEFORE the time expires, the timer doesn't stop. It will bring you back to the gameover screen when it decides it has run out, even when you've started a new game.

I've tried to fix this by putting gameTimer.stop() in other functions that bring you to the gameover screen, but then there was another problem. This occurred while trying to stop the timer in other functions, like this one (stop button while playing):

function stopplaying(event:MouseEvent){
    gameTimer.stop();
    finalScore = score;
    musicchannel.stop();
    MovieClip(root).gotoAndStop("introduction");
}

This will give me a compile error 1120: access of undefined property gameTimer. I understand that the gameTimer usually can only be influenced within a function if that function listens to a TimerEvent, but I don't see any options to do this in any other way.

I've tried to make gameTimer a public variable, but it won't allow me to do that within the main memory function. Also, when I try to define it a public variable out of a funcion, but within the class, the timer will still count down. But when it expires, it just gives a random high number in return and doesn't go to the gameover screen.

I hope the explanation of my problem wasn't too vague and that you are able to help me with this. This is a schoolproject and it's due pretty soon! Also, I can't figure this out on my own with internet alone :( Several tries have only made things worse, I'm afraid to screw this up now that I've come this far.

Upvotes: 0

Views: 72

Answers (1)

Fygo
Fygo

Reputation: 4665

You were doing it probably right-ish by declaring it as a public variable.. but where did you declare it?

In AS (all versions) you have function scope. That means if you declare your variable inside a function, that variable "exists" (is accessible) only inside of that function. Check where you are declaring your variable:

public function memory():void
{
      levelDuration = 10;
      gameTime = levelDuration;
      var gameTimer:Timer = new Timer(1000,levelDuration); // <-
      gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
      gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired);
      gameTimer.start();
}

That means your variable "gameTimer" is local to the function memory(). You won't be able to use your variable anywhere outside of this function because it exists only inside of memory(). To solve this issue, you have move it outside of the function:

private var gameTimer:Timer;

public function memory():void
{
      levelDuration = 10;
      gameTime = levelDuration;
      gameTimer = new Timer(1000,levelDuration);
      gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
      gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired);
      gameTimer.start();
}

That will solve all your issues.

Upvotes: 2

Related Questions