2011-04-03 47 views
0

創建一個按鈕,現在試圖將其添加到屏幕上,我得到這個錯誤。按鈕的代碼 -TypeError:錯誤#2007:參數子項必須爲非空值。在flash.display :: DisplayObjectContainer/addChild()

private function submitButton():void { 
    submit_button=new SimpleButton(); 
    submit_button.x=200; 
    submit_button.y=200; 
    submit_button.upState=buttonShape(); 
    submit_button.overState=buttonShape(); 
    submit_button.downState=buttonShape(); 
} 

private function buttonShape() { 
    var rectangle:Shape = new Shape; // initializing the variable named rectangle 
    rectangle.graphics.beginFill(0x8C2B44); // choosing the colour for the fill, here it is red 
    rectangle.graphics.drawRect(200, 200, 300,20); // (x spacing, y spacing, width, height) 
    rectangle.graphics.endFill(); 
} 

我已經聲明它作爲一個公共變種,我使用addChild(submit_button);

  • 編輯 - 所有的代碼是在一個類文件。我在開始時設置了提交按鈕, - private var submit_button:SimpleButton;我有一個函數的私人功能submitButton():void { submit_button = new SimpleButton(); submit_button.x = 200; submit_button.y = 200;

    submit_button.upState=buttonShape(); 
        submit_button.overState=buttonShape(); 
        submit_button.downState=buttonShape(); 
    
    } and the drawing function seen above. and i add it to the stage in another function - private function gameOver():void { 
    
        addChild(submit_button); 
        addChild(gameOver1);  basically this function is called on a hit test. and i have a text box that appears (this works) but when i add the button i get the error 
    
+0

?另外,你最初在哪裏聲明瞭它的實例變量?在這個函數之外,我假設?像,var submit_button:SimpleButton;靠近類構造函數的頂部? – jpea 2011-04-03 22:48:30

+0

我認爲buttonShape()應該返回var矩形 – chchrist 2011-04-03 22:50:47

+0

是啊,這是在類文件的頂部 - 私人var submit_button:SimpleButton;通過返回var矩形你是什麼意思@chchrist? – DIM3NSION 2011-04-03 22:58:41

回答

2

功能buttonShape()不返回任何東西。您的inital行var rectangle:Shape = new Shape;正在創建一個局部變量,該變量超出了範圍,並在函數結束時不再存在。

由於buttonShape()不返回任何東西,它的實際功能的簽名是:

private function buttShape():void {} 

return void;在函數結束被暗示。

這意味着你的代碼

submit_button.upState=buttonShape(); 

基本上編譯爲:你在哪裏使用addChild(submit_button)

submit_button.upState=null; 
+0

添加返回矩形的語句。它仍然給出了錯誤 – DIM3NSION 2011-04-03 23:14:14

+0

你可以發佈聲明'submit_button'的代碼,調用'submitButton()'函數並將其添加到舞臺上嗎?我剛剛拿到了你發佈的代碼,它在我的最終版本上工作 – divillysausages 2011-04-03 23:23:02

+0

更新了我的頂級帖子,請參閱 - 編輯 - – DIM3NSION 2011-04-03 23:51:29

相關問題