0

我想在创建孩子 x 秒后删除它。我怎样才能做到这一点?

孩子是在函数内部创建的。

基本上,像这样的东西......

function makechild() {
    addChild(thechild);
    thechild.x=240;
    thechild.y=330;
    // what should go here? so it deletes after x seconds?
}
4

2 回答 2

1

通过 flash.utils.setTimeout() 使用一次性计时器,如下所示:

setTimeout(dropChild,seconds*1000);
...
function dropChild():void {
    removeChild(thechild);
}
于 2012-10-27T04:34:07.663 回答
0

使用 Actionscript 2,您将使用 setInterval。但是,Actionscript 3 的方式是使用 Timer 类,如下所示:

function makechild() {
    addChild(thechild);
    thechild.x=240;
    thechild.y=330;
    // add a timer to "thechild" that will trigger it to be deleted
    thechild.selfdestruct:Timer = new Timer(1000, 1); // 1 second
    thechild.selfdestruct.addEventListener(TimerEvent.TIMER, deleteobject);
    thechild.selfdestruct.start();
}

function deleteobject(event:TimerEvent):void {
    // delete the child object, below is one example
    this.parent.removeChildAt(0);
}

您可以从 Actionscript 文档中获得有关 Timer 类的更多详细信息。有关 Timer 类与 setInterval 的更多信息,请参阅此链接:http: //blogs.adobe.com/pdehaan/2006/07/using_the_timer_class_in_actio.html

于 2012-10-27T04:36:51.127 回答