TypeError:Error#2007:参数子项必须为非null。在flash.display::DisplayObjectContainer/addChild()处
创建了一个按钮,现在试图将其添加到屏幕上,我收到了这个错误。按钮的代码为-TypeError:Error#2007:参数子项必须为非null。在flash.display::DisplayObjectContainer/addChild()处,flash,actionscript-3,button,hyperlink,addchild,Flash,Actionscript 3,Button,Hyperlink,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_bu
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();
}
我已将其声明为公共var,并使用addChild(submit_按钮)代码>
- 编辑-所有代码都在一个类文件中。我在开始时设置了提交按钮,-private var submit_按钮:SimpleButton;我有一个函数private函数submitButton():void{
提交按钮=新建SimpleButton();
提交按钮x=200;
提交按钮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
函数buttonShape()
没有返回任何内容。您的初始行var rectangle:Shape=new Shape;
正在创建一个局部变量,该变量超出范围,在函数末尾不再存在
由于buttonShape()
不返回任何内容,它的实际函数签名是:
private function buttShape():void {}
函数末尾暗示有一个返回void;
这意味着您的代码
submit_button.upState=buttonShape();
基本上是为了:
submit_button.upState=null;
您在哪里使用addChild(submit_按钮)?另外,您最初在哪里声明它的实例变量?我假设在该函数之外?比如,var submit_按钮:SimpleButton;靠近类构造函数的顶部?我认为buttonShape()应该返回var rectangleyeah在类文件的顶部得到了这个-private var submit_button:SimpleButton;返回var矩形是什么意思@chchrist?在buttonShape()的末尾放上“return rectangle”然后看看这是否解决了问题。您绘制了形状,但没有返回它,以便将其分配给一个变量。它仍然为语句提供了errorive added return矩形。它仍然提供了errorive added return矩形。您可以将代码发布到声明提交按钮
的位置,调用提交按钮吗ton()
函数并将其添加到舞台?我刚刚获取了您发布的代码,它正在我的终端工作。您需要检查submitButton()
是否在gameOver()
之前实际被调用。要快速测试,只需直接在addChild(submit_按钮);