2014-02-27 63 views
-1

我一直有麻煩,當我運行我的代碼和錯誤#1009彈出似乎停止我的代碼工作。我相信問題是當我的AddDalek()函數試圖爲臨時對象的x和y屬性賦值時。但是,我無法弄清楚如何解決這個問題。對解決方案的改變提供解決方案和解釋會很有幫助。AS3 Error#1009 Position Starts as null

此代碼僅僅是我爲學校製作的遊戲的開始,所以請隨時推薦對我的代碼進行任何更改。

僅供參考Cyber​​men,所有的Daleks,沉默,哭泣的天使是聯動的符號

import flash.events.* 
    import flash.display.* 

    var currentx = 229.95; 
    var currenty = 429.6; 
    var enemyspeed = 1; 

    function randRange(min:Number, max:Number):Number { 
     var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min; 
     return randomNum; 
    } 

    function AddCyberman(){ 
    var tempobject:Cyberman = new Cyberman(); 
    addChild (tempobject); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
    } 

    function AddSilence(){ 
    var tempobject:Silence = new Silence(); 
    addChild (tempobject); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
    } 

    function AddWeepingAngel(){ 
    var i = randRange(1,2); 
    if (i == 1) 
{ 
    var tempobject:WeepingAngel1 = new WeepingAngel1(); 
    addChild (tempobject); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 

else if (i == 2) 
{ 
    var tempobject1:WeepingAngel2 = new WeepingAngel2(); 
    addChild (tempobject1); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
    } 

    function AddDalek() 
    { 
var i = randRange (1,5); 
if (i == 1) 
{ 
    var tempobject:BlueDalek = new BlueDalek(); 
    addChild (tempobject); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
else if (i == 2) 
{ 
    var tempobject1:YellowDalek = new YellowDalek(); 
    addChild (tempobject1); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
else if (i == 3) 
{ 

    var tempobject2:RedDalek = new RedDalek(); 
    addChild (tempobject2); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
else if (i == 4) 
{ 
    var tempobject3:GreenDalek = new GreenDalek(); 
    addChild (tempobject3); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
else if (i == 5) 
{ 
    var tempobject4:SilverDalek = new SilverDalek(); 
    addChild (tempobject4); 
    tempobject.x = currentx; 
    tempobject.y = currenty; 
} 
    } 

    function moveEnemys() 
    { 
var templist2:Array = new Array() 
var templist:Array = [Silence , Cyberman , WeepingAngel1 , WeepingAngel2 , BlueDalek , YellowDalek , RedDalek , GreenDalek , SilverDalek]; 
for (var i = 0; i < templist.length; i++) 
{ 
    if (templist[i]) 
    { 
     templist2.push (templist[i]); 
    } 
} 
for (var t = 0; t < templist2.length; i++) 
{ 
    templist2[t].y += enemyspeed; 
} 
    } 

    addEventListener (Event.ENTER_FRAME , function() {OnFrameReset()}); 

    function OnFrameReset() 
    { 
moveEnemys(); 
    } 

    AddDalek(); 



    TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at com.tylerkasper::Engine/AddDalek() 
at com.tylerkasper::Engine/frame1() 

回答

0

您有一個錯誤在你addDalek功能,在別人的語句,你仍然繼續指定tempobject因爲它穿上」 t存在。試試這個:

function AddDalek() 
{ 
    var i = randRange (1,5); 
    if (i == 1) 
    { 
     var tempobject:BlueDalek = new BlueDalek(); 
     addChild (tempobject); 
     tempobject.x = currentx; 
     tempobject.y = currenty; 
    } 
    else if (i == 2) 
    { 
     var tempobject1:YellowDalek = new YellowDalek(); 
     addChild (tempobject1); 
     tempobject1.x = currentx; 
     tempobject1.y = currenty; 
    } 
    else if (i == 3) 
    { 

     var tempobject2:RedDalek = new RedDalek(); 
     addChild (tempobject2); 
     tempobject2.x = currentx; 
     tempobject2.y = currenty; 
    } 
    else if (i == 4) 
    { 
     var tempobject3:GreenDalek = new GreenDalek(); 
     addChild (tempobject3); 
     tempobject3.x = currentx; 
     tempobject3.y = currenty; 
    } 
    else if (i == 5) 
    { 
     var tempobject4:SilverDalek = new SilverDalek(); 
     addChild (tempobject4); 
     tempobject4.x = currentx; 
     tempobject4.y = currenty; 
    } 
} 

當你有你的代碼中的錯誤嘗試調試影片(按Ctrl + Shift + Enter鍵),而不是僅僅運行它(按Ctrl + Enter),你將有更好的錯誤信息和相應的行錯誤。

希望這會幫助你;)

+0

謝謝。我不敢相信這是一個簡單的錯誤。另外,感謝您在未來的調試信息。它確實有幫助。 – Chubzorz