我继续得到错误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秒,依此类推。
您每次启动时都会创建一个新计时器,因此一旦新游戏启动,您可能只需要在后台运行旧计时器。您需要调用timer.stop()
来停止计时器,从您想要使用的任何最终游戏方法。你好像在removeAllEvents()
方法上做的那样,但你实际上从来没有调用过这种方法。