为什么flash输出TypeError:错误#2007:参数child必须为非null。在flash.display :: DisplayObjectContainer / addChild()

时间:2016-03-01 14:26:07

标签: actionscript-3 flash

这是我到目前为止的代码

package  {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        var leftpressed:Boolean = false;
        var rightpressed:Boolean = false;

        public function Main() 
        {
            //constructor code
            Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnpress);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, FlyBullet);      
        }       

        function moveToKey (event:Event) 
        {
            if (leftpressed && (Spaceship.x>0))
            {
                Spaceship.x = -5;
            }
            if (rightpressed && (Spaceship.x<550))
            {
                Spaceship.x = +5;
            }

        }       

        function setKeyPress(e:KeyboardEvent):void 
        {
            if (e.keyCode == 37)
            {
                leftpressed = true;
            }
            if (e.keyCode == 39)
            {
                rightpressed = true;
            }
        }

        function setKeyUnpress(e:KeyboardEvent): void   
        {
            if (e.keyCode == 37)
            { 
                leftpressed = false;
            }
            if (e.keyCode == 39)
            {
                rightpressed = false;
            }

        }

        function FlyBullet (e:KeyboardEvent):void
        {           
            if (e.keyCode == 32)
            var bulletshot:Bullet = new Bullet();
            addChild(bulletshot);
            bulletshot.x = Spaceship.x;
            bulletshot.y = Spaceship.y
        }

    }
}

最终FlyBullet函数存在最大问题,输出错误列为

TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at Main/FlyBullet()

2 个答案:

答案 0 :(得分:1)

这是你的问题:

if  (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y

没有大括号,因此if条件仅适用于分配了bulletshot的第一行。换句话说,如果您按SPACE以外的任何键,它将跳过bulletshot的分配但仍尝试addChild(bulletshot),但由于bulletshot未分配值,是null,你会收到错误。

我认为你的意思是:

if (e.keyCode == Keyboard.SPACE) {
    var bulletshot:Bullet = new Bullet();
    addChild(bulletshot);
    bulletshot.x = Spaceship.x;
    bulletshot.y = Spaceship.y
}

PS:你的代码在很多地方都很难缩进。它有助于正确缩进代码。自动格式化程序可以帮助您。

答案 1 :(得分:1)

在多行if语句中省略括号通常被认为是不好的做法,这是一个例子。每当e.keyCode != 32 addChild行尝试运行时,

解决方案是:

///...in the Main function//
stage.addEventListener(KeyboardEvent.KEY_DOWN, flyBullet);
///...
function flyBullet (e:KeyboardEvent):void
{

    if  (e.keyCode == 32)
    {
        var bulletshot:Bullet = new Bullet();
        addChild(bulletshot);
        bulletshot.x = Spaceship.x;
        bulletshot.y = Spaceship.y;
    }
}

一些不相关的as3代码质量提示,以避免以后可能出现的错误:

我猜测Spaceship不是静态类,所以我建议使用小写的第一个字母表示变量/函数名称,大写字母表示类,但这只是一个很好的做法而且不会影响你的代码正确无误。

定义函数的可访问性也是一种好习惯,即public, private, internal, protected。如果省略,默认值为internal

只有在必要时才会在条件限制之后跳过括号。