【问题标题】:My timer increases speed after a single play through of my game why?我的计时器在玩了一次游戏后会加快速度,为什么?
【发布时间】:2015-10-12 13:31:04
【问题描述】:

我继续收到错误 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 秒,以此类推。

【问题讨论】:

    标签: flash timer action


    【解决方案1】:

    您每次开始时都会创建一个新计时器,因此很可能您只是在新游戏开始时让旧计时器在后台运行。您需要调用timer.stop() 来停止计时器,无论您想使用哪种结束游戏方法。您似乎只是在 removeAllEvents() 方法上执行此操作,但您从未真正调用过该方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      相关资源
      最近更新 更多