Search code examples
flashtimeraction

My timer increases speed after a single play through of my game why?


I continue to get the error TypeError: Error #1009: Cannot access a property or method of a null object reference. at ChazS1127_win_loose_screen_fla::MainTimeline/updateTime() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick() and here is my code for my main game

stop();
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.TimerEvent;
import flash.utils.Timer;

var timer:Timer = new Timer(1000, 9999);
timer.addEventListener(TimerEvent.TIMER, updateTime);

var timeRemaining = 30;
timerBox.text = timeRemaining;

function updateTime(evt:TimerEvent)
{
    timeRemaining--;
    timerBox.text = timeRemaining;
    if(timeRemaining <= 0)
{

       gotoAndStop(1, "Loose Screen");
}
}

addEventListener(MouseEvent.CLICK, onMouseClick);

var score = 0 ;
function onMouseClick(evt:MouseEvent)
{
        if(evt.target.name == "playBtn") 
    {
        texts.visible = false;
        playBtn.visible = false;
        backpackss.visible = false;
        backgrounds.visible = false;
        timer.start();
    }
     if(evt.target.name == "Backpack")
     {
    Backpack.visible = false;
    message.text = "A backpack is a necessity for carrying all your items.";
    score = score + 1;
    scoredisplay.text = "score:" + score;

0
}
if(evt.target.name == "Bandage")
{
    Bandage.visible = false;
    message.text = "Bandages for cuts and scraps and to keep from bleeding out.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Brace")
{
    Brace.visible = false;
    message.text = "A brace is good for a sprain or even a break in a bone allow you to keep going.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "canned")
{
    canned.visible = false;
    message.text = "Canned foods are a good resource as food will be scarce in situations.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Compass")
{
    Compass.visible = false;
    message.text = "Going the wrong direction in a survival situation can be your downfall.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Flashlight")
{
    Flashlight.visible = false;
    message.text = "A flashlight can help you see at night and help you stay sane.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Iodine")
{
    Iodine.visible = false;
    message.text = "An ioddine purfication kit can assist in keeping water clean for drinking.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Lighter")
{
    Lighter.visible = false;
    message.text = "A windproof lighter for even the windest of days to light fires.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Radio")
{
    Radio.visible = false;
    message.text = "A radio to help keep up to date on news around if it's still brodcasting.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Sewing")
{
    Sewing.visible = false;
    message.text = "A sewing kit for salvaging clothes and for patching up wounds.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
if(evt.target.name == "Tent")
{
    Tent.visible = false;
    message.text = "A tent can be a home for the night and a home for life.";
    score = score + 1;
    scoredisplay.text = "score:" + score;
}
 if(score >= 11)
{

   gotoAndStop(1, "Victory Screen");
}
}
function removeAllEvents()
{
       removeEventListener(MouseEvent.CLICK, onMouseClick);      
       timer.removeEventListener(TimerEvent.TIMER, updateTime);
       timer.stop();
     }

and my Victory Screen

stop();
import flash.events.MouseEvent;

addEventListener(MouseEvent.CLICK, onMouseClick2);
play();
function onMouseClick2(evt:MouseEvent)
{

   if(evt.target.name == "restartButton")
   {
          gotoAndStop(1, "Main Game");
          removeAllEvents2();
   }
}
 function removeAllEvents2()
{
   removeEventListener(MouseEvent.CLICK, onMouseClick2);      
}

and my loose screen

stop();
import flash.events.MouseEvent;
play();
addEventListener(MouseEvent.CLICK, onMouseClick2);

function onMouseClick3(evt:MouseEvent)
{

   if(evt.target.name == "restartButton")
   {
          gotoAndStop(1, "Main Game");
          removeAllEvents3();
}

}
function removeAllEvents3()
{
   removeEventListener(MouseEvent.CLICK, onMouseClick3);      
} 

so why does my game when through one play the timer will speed up and go fast for no reason. After one play through it'll go 2 seconds for every 1 second and so on.


Solution

  • You are creating a new timer every time it starts, so it's likely you just have your old timer running on the background once a new game starts. You need to call timer.stop() to stop the timer, from whatever end game method you want to use. You seem to be doing just that on the removeAllEvents() method, but you're never actually calling that method.