0

我创建了 4 个 Notes 实例,我让它们向右移动,直到它们的 x 值大于 100。一旦它们在那里,我该如何删除它们?我运行了一个跟踪语句并确认这些实例的父级是 root(确切地说是 root1)。如果我输入 root.removeChild(this); 如果我输入 removeChild(this); 我收到一条错误消息,提示“提供的 DisplayObject 必须是调用者的子对象”。完整代码发布在下面。最后的 } 之前的最后一行是问题行。非常感谢你的帮忙!

package 
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.getDefinitionByName;
import flash.utils.Timer;
import flash.events.TimerEvent;

[Frame(factoryClass="Preloader")]
public class Main extends Sprite 
{
private var speed:int = 8;
[Embed(source="../lib/Dodgethis.jpg")]
public var Notes:Class;
public var numnotes:Number;
public var timer:Timer = new Timer(500, 1)
public var rootContainer:DisplayObjectContainer = DisplayObjectContainer (root);

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point
        timer.start();
        timer.addEventListener(TimerEvent.TIMER, testevent);

        }

    private function testevent(e:Event = null):void {
        trace("testevent has run");
        appear();
    }

    private function appear() {
        var arr1:Array = new Array;
        numnotes = 4;

        for (var i = 0; i < numnotes; i++)
            {
            trace (i);
            var nbm:Bitmap = new Notes;
                stage.addChild(nbm);
                nbm.y = i * 50;
                arr1.push(nbm);
                addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        }

    }
    private function loop (e:Event):void {
        this.x += speed;
        trace(this.x) ;
        if (this.x > 100) {
            removeEventListener(Event.ENTER_FRAME, loop);
            trace ("Event listener was removed");
            //removeChild(this);
            //rootContainer.removeChild (nbm);
            /*trace(this.contains)


            trace(this.name)
            trace(this)*/
            trace(this.parent.name); //root
            removeChild(this);
        }
    }


}

}

4

2 回答 2

0

尝试在循环函数中使用它

e.target.parent.removeChild(e.target);
//or
stage.removeChild(e.target);
于 2013-09-05T20:29:26.390 回答
0

您正在将注释添加到舞台。所以你需要把它们从舞台上移除。

stage.removeChild( note );

您只能从其父容器中移除子容器,而不能从任何其他容器中移除。所以在不同的容器上调用 removeChild 总是会失败

于 2013-09-05T20:36:53.003 回答