我的计时器在单次游戏之后会提高速度为什么?

时间:2015-10-12 13:31:04

标签: flash timer action

我继续得到错误 TypeError:错误#1009:无法访问空对象引用的属性或方法。     在ChazS1127_win_loose_screen_fla :: MainTimeline / updateTime()     在flash.utils :: Timer / _timerDispatch()     在flash.utils :: Timer / tick() 这是我主要游戏的代码

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

和我的胜利屏幕

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

我的屏幕松散

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

那么为什么我的游戏通过一次游戏计时器会加速并且无缘无故地快速前进。经过一次游戏后,每1秒钟会持续2秒,依此类推。

1 个答案:

答案 0 :(得分:0)

每次启动时都会创建一个新计时器,因此一旦新游戏启动,您可能只需要在后台运行旧计时器。你需要调用timer.stop()来停止计时器,从你想要使用的最终游戏方法开始。您似乎只是在removeAllEvents()方法上执行此操作,但您实际上从未调用该方法。