2014-12-31 42 views
0

我試圖添加一個帶有重新啓動按鈕的gameover屏幕,我已將重新啓動按鈕放在框架上22.當我的玩家去世時,它會進入第22幀,我可以通過單擊按鈕重新開始遊戲,但是這條消息會在輸出區域循環播放。請幫助我如何解決此問題。使用gotoAndplay函數時出現「TypeError:Error#1009:Can not access a property or method of null object reference」

問題是不存在的,當我刪除行

gotoAndPlay(22);

在17幀,但沒有,我不會得到所期望的功能。

請找我的代碼如下

在框架17 - 遊戲代碼

stop(); 

import flash.events.Event; 
import flash.events.MouseEvent; 

var mouseIsDown = false; 
var speed = 0; 
var score = 0; 

addEventListener(Event.ENTER_FRAME,mainLoop); 

stage.addEventListener(MouseEvent.MOUSE_DOWN,clicked); 
stage.addEventListener(MouseEvent.MOUSE_UP,unclicked); 

function clicked(m:MouseEvent) 
{ 
    mouseIsDown = true; 
} 

function unclicked(m:MouseEvent) 
{ 
    mouseIsDown = false; 
} 

function mainLoop(e:Event) 
{ 
    score = score + 10; 

    output.text = "Score: "+score; 

    if(mouseIsDown) 
    { 
     speed -= 2; 
    } 
    else 
    { 
     speed+=2; 
    } 

    if(speed > 10) speed = 10; 
    if(speed < -10) speed = -10; 

    player.y += speed; 

    for(var i = 0; i < numChildren; i++) 
    { 
     if (getChildAt(i) is Cloud || getChildAt(i) is Boundary) 
      { 
       var b = getChildAt(i) as MovieClip;   

       if(b.hitTestObject(player)) 
       { 
        for(var counter = 0; counter < 12; counter++) 
        { 
         var boom = new Boom(); 
         boom.x = player.x; 
         boom.y = player.y; 
         boom.rotation = Math.random() * 360; 
         boom.scaleX = boom.scaleY = 0.5 + Math.random(); 
         addChild(boom); 
        } 

        player.visible = false; 

        removeEventListener(Event.ENTER_FRAME,mainLoop); 
        gotoAndPlay(22); 
       } 
      } 
    } 
} 

在框架22 - 重新啓動屏幕

stop(); 
import flash.events.MouseEvent; 

foutput.text = "Score: "+ fscore; 

btn_playagain.addEventListener(MouseEvent.CLICK, playagain); 

function playagain(m:MouseEvent) 
{ 
    gotoAndPlay(17); 
} 

btnback3.addEventListener(MouseEvent.CLICK, backMain3); 

function backMain3(m:MouseEvent) 
{ 
    gotoAndPlay(1); 
} 

在第1幀 - 主菜單屏幕

stop(); 

import flash.events.MouseEvent; 
import flash.system.fscommand; 

btnnewgame.addEventListener(MouseEvent.CLICK, newGame); 

function newGame(m:MouseEvent) 
{ 
    gotoAndPlay(17); 
} 

btnins.addEventListener(MouseEvent.CLICK, instruct); 

function instruct(m:MouseEvent) 
{ 
    gotoAndPlay(6); 
} 

btncredits.addEventListener(MouseEvent.CLICK, credits); 

function credits(m:MouseEvent) 
{ 
    gotoAndPlay(11); 
} 

btnexit.addEventListener(MouseEvent.CLICK, exitfunc); 

function exitfunc(m:MouseEvent) 
{ 
    fscommand("quit"); 
} 

在6幀 - 設定屏幕

stop(); 

btnback1.addEventListener(MouseEvent.CLICK, backMain1); 

function backMain1(m:MouseEvent) 
{ 
    gotoAndPlay(1); 
} 

在11幀 - 現金網

stop(); 

btnback2.addEventListener(MouseEvent.CLICK, backMain2); 

function backMain2(m:MouseEvent) 
{ 
    gotoAndPlay(1); 
} 
+0

讓我看看來自第22幀的代碼。 – tziuka

+0

@tziuka sorry.i已更新我的查詢 –

回答

1

該錯誤意味着您正試圖調用一個方法在空對象,這意味着您在第22幀上使用的其中一個對象實際上並不存在。

違規變量的可能候選人爲foutput,btn_playagainbtnback3。檢查以確保它們在第22幀的舞臺上,並拼寫正確。

您在第17幀上使用output.text,您確定它應該在第22幀上是foutput.text

+0

好吧,發現了。 錯誤#1009將與嘗試訪問拼寫錯誤的對象名稱的方法一致。 –

相關問題