类型错误:错误#2007:参数子项必须为非空。在 flash.display::DisplayObjectContainer/addChild()

发布于 2024-10-30 18:53:24 字数 1244 浏览 0 评论 0原文

创建了一个按钮,现在尝试将其添加到屏幕上,但我收到此错误。该按钮的代码是 -

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);

  • edit - 所有代码都在一个类文件中。我在开始时设置了提交按钮, - private var Submit_button:SimpleButton;我有一个函数 private function SubmitButton():void { 提交按钮=新的SimpleButton(); 提交按钮.x=200; 提交按钮.y=200;

    submit_button.upState=buttonShape();
        commit_button.overState=buttonShape();
        commit_button.downState=buttonShape();
    
    } 和上面看到的绘图函数。我将它添加到另一个函数中的舞台 - private function gameOver():void {
    
        addChild(提交按钮);
        addChild(gameOver1);基本上这个函数是在命中测试中调用的。我有一个出现的文本框(这有效),但是当我添加按钮时,我收到错误
    

Created a button, now tried to add it to the screen and im getting this error. The code for the button is -

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();
}

I've declared it as a public var and I'm using addChild(submit_button);

  • edit - all the code is in one class file. i set the submit button at the start, - private var submit_button:SimpleButton; i have a function 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();
    
    } 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
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-11-06 18:53:24

函数 buttonShape() 没有返回任何内容。您的初始行 var矩形:Shape = new Shape; 正在创建一个局部变量,该变量超出范围并在函数末尾不再存在。

由于 buttonShape() 不返回任何内容,因此它的实际函数签名是:

private function buttShape():void {}

在函数末尾隐含 return void;

这意味着您的代码

submit_button.upState=buttonShape();

本质上是编译为:

submit_button.upState=null;

The function buttonShape() isn't returning anything. Your inital line var rectangle:Shape = new Shape; is creating a local variable, which goes out of scope and ceases to exist at the end of the function.

As buttonShape() doesn't return anything, it's actual function signature is:

private function buttShape():void {}

with a return void; being implied at the end of the function.

This means that your code

submit_button.upState=buttonShape();

is essentially compiling to:

submit_button.upState=null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文