TypeError:错误#1009

时间:2014-06-14 10:49:25

标签: actionscript-3 flash

我有一个文档类,这个Enemy类。我有一个功能来制作文档类中的敌人。在这个敌人类中,我击中了子弹队(Kogel),如果敌人不在舞台上。在这两个中,我正在移除孩子。它有效。但它给了我这个TypeError#1009。这是敌人的代码,其中“删除舞台”和“kogel hitTest”都是。 (我对船也有同样的问题,但那是另一次。我想我可以解决这个问题。)

package 
{

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

public class vijandEen extends MovieClip
{
    var speed:int = 8;
    var theHero:ruimteschip;
    var schot:Bullet;

    public function vijandEen(aSchip:ruimteschip, Kogel:Bullet)
    {
        theHero = aSchip;
        schot = Kogel;
        addEventListener(Event.ENTER_FRAME, vijandBewegen);
        addEventListener(Event.ENTER_FRAME, collissionSchip);
        addEventListener(Event.ENTER_FRAME, collissionKogel);
    }

     function vijandBewegen(e:Event)
    {
        x +=  speed;
        if (this.x > stage.stageWidth)
        {
            this.parent.removeChild(this);
        }
    }

     function collissionSchip(e:Event)
    {
        if (theHero.hitTestObject(this))
        {
            trace("geraakt...");
            verwijderSchip(e);

        }
    }

     function collissionKogel(e:Event)
    {
        if (schot.hitTestObject(this))
        {
            trace('raak!');
            verwijderEnemy(e);
        }
    }

    function verwijderEnemy(e:Event){
        if (this.parent != null){
            this.parent.removeChild(this);
        }
    }

    function verwijderSchip(e:Event){
        if (this.parent != null){
            theHero.parent.removeChild(theHero);
        }
    }
}
}

编辑 - 这是我的文档类代码:

package 
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.media.Sound;

public class amateurodius extends MovieClip
{
    public var schip:ruimteschip = new ruimteschip();
    public var Kogel = new Bullet();
    var sound = new lazor();
    var BGM = new Muziek();
    var backdrop = new background();
    var shootAllow:Boolean = true;
    var enemyTime:int = 0;
    var enemyLimit:int = 50;

    public function amateurodius()
    {
        addEventListener(Event.ADDED_TO_STAGE, initialize);
    }

    function initialize(e:Event)
    {
        addChild(backdrop);
        addChild(schip);
        BGM.play();
        addEventListener(Event.ENTER_FRAME, vijandmaken);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, spawnKogel);
        addEventListener(Event.ENTER_FRAME, kogelCheck);
        Mouse.hide();
    }

    public function vijandmaken(e:Event)
    {
        if (enemyTime < enemyLimit)
        {
            enemyTime++;
        }
        else
        {
            var newEnemy = new vijandEen(schip,Kogel);
            newEnemy.x = -1 * newEnemy.width;
            newEnemy.y = int(Math.random()*(stage.stageHeight - newEnemy.height));
            addChild(newEnemy);
            enemyTime = 0;
        }
    }

    public function spawnKogel(e:KeyboardEvent):void
    {
        if (shootAllow == true)
        {
            if (e.keyCode == Keyboard.SPACE)
            {
                addChild(Kogel);
                Kogel.x = schip.x;
                Kogel.y = schip.y;
                sound.play();
                shootAllow = false;
                kogelCheck(e);
            }
        }
    }

    function kogelCheck(e:Event)
    {
        if (Kogel.x < -30)
        {
            shootAllow = true;
        }
        else
        {
            shootAllow = false;
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

private function stopEvents():void
{
    this.removeEventListener(Event.ENTER_FRAME, vijandBewegen);
    this.removeEventListener(Event.ENTER_FRAME, collissionSchip);
    this.removeEventListener(Event.ENTER_FRAME, collissionKogel);
}
function vijandBewegen(e:Event)
{
    x +=  speed;
    if (this.x > stage.stageWidth)
    {
        stopEvents();
        this.parent.removeChild(this);
    }
}

function verwijderEnemy(e:Event)
{
    if (this.parent != null)
    {
        stopEvents();
        this.parent.removeChild(this); 
    }
}

我认为你的代码会不断检查每一帧,但是当你删除它时会丢失对该阶段的引用,因此它不知道哪个阶段(== null)。每当您从舞台上移除它时都会发生这种情况。

编辑:我jsut意识到其他事情,你不应该初始化处理舞台的事件监听器,直到你将你的对象添加到舞台上。即:

public function vijandEen(aSchip:ruimteschip, Kogel:Bullet)
{
    theHero = aSchip;
    schot = Kogel;
    this.addEventListener(Event.ADDED_TO_STAGE, function (e:Event)
    {
        addEventListener(Event.ENTER_FRAME, vijandBewegen);
        addEventListener(Event.ENTER_FRAME, collissionSchip);
        addEventListener(Event.ENTER_FRAME, collissionKogel);
     });
 }

另外,作为旁注,您不应该使用Capital首字母作为变量名,将它们保留为类名。对于小写的第一个字母也是如此,它们应该保留为变量。