我想在创建子项之后将其删除x秒。我怎样才能做到这一点?x秒后移除一个孩子? AS3
孩子是在一个函数内部创建的。
基本上,这样的事情...
function makechild() {
addChild(thechild);
thechild.x=240;
thechild.y=330;
// what should go here? so it deletes after x seconds?
}
我想在创建子项之后将其删除x秒。我怎样才能做到这一点?x秒后移除一个孩子? AS3
孩子是在一个函数内部创建的。
基本上,这样的事情...
function makechild() {
addChild(thechild);
thechild.x=240;
thechild.y=330;
// what should go here? so it deletes after x seconds?
}
使用通过flash.utils.setTimeout()这样的一次性计时器:
setTimeout(dropChild,seconds*1000);
...
function dropChild():void {
removeChild(thechild);
}
使用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
是啊,就像你贴我想通了......我是想同样的事情,但我搞砸了函数名(增加了一个额外的大写字母):(无论如何! – Butterflycode