删除循环中创建的对象的计时器

发布于 2024-11-08 02:58:41 字数 363 浏览 0 评论 0原文

我想删除一个使用计时器在循环中创建的对象。

var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER, voegtoe);

在函数 voegtoe 中,我添加了一个名为 man 的 Movieclip 对象:

man.x=Math.random()*650;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);

我想在 2 秒后删除该对象。问题是有多个“人”对象。

有人可以帮我吗?

谢谢。

I want to remove an object that is created in a loop, with a timer.

var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER, voegtoe);

In function voegtoe I'm adding an Movieclip Object called man:

man.x=Math.random()*650;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);

I want to remove this object after 2 seconds. The problem is that there are multiple 'man' objects.

Can somebody help me out?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-11-15 02:58:41

将 Movieclip 对象保存在数组中将使以后更容易引用。

var man:Array = new Array();

for (var i:int=0; i<10;i++)
{ 
  man[i] = new Movieclip();
  man[i].x=Math.random()*650;
  man[i].y=Math.floor(Math.random()*(70))+350;
  addChild(man[i]);
}

现在你可以参考你的mc所在的数组位置:

function removeMC(id:int):void
{
  removeChild(man[id])
}

Keeping your Movieclip objects inside an array will make easier to refer to then later.

var man:Array = new Array();

for (var i:int=0; i<10;i++)
{ 
  man[i] = new Movieclip();
  man[i].x=Math.random()*650;
  man[i].y=Math.floor(Math.random()*(70))+350;
  addChild(man[i]);
}

now you can refer to the array position where your mc is located:

function removeMC(id:int):void
{
  removeChild(man[id])
}
墨洒年华 2024-11-15 02:58:41

您是否正在寻找 setTimeout< /a> 功能??

man.x=Math.random()*650;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);

将计时器设置为:

setTimeout(removeMan, 1000);

将被调用的函数定义为:

function removeMan():void
{
  removeChild(man)
}

这可能会更容易,但即使是 livedocs 也建议使用 Timer 对象(如 nelsond 的回答)。

Are you looking for the setTimeout function??

man.x=Math.random()*650;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);

Set timer as:

setTimeout(removeMan, 1000);

Define the called function as:

function removeMan():void
{
  removeChild(man)
}

It might be easier, but even the livedocs suggest using a Timer object (As in nelsond's answer).

怂人 2024-11-15 02:58:41

这可以通过几种不同的方式来完成。

我的建议是你为你的男人开设一个课程。在这个类中,您有一个启动removeTimer的函数,并且应该在创建该人时触发该函数。当 2 秒结束时,我们删除事件监听器并将其从其父级中删除。

添加男人的计时器应该位于您想要在其中实例化男人的任何类中。

这是一个示例

public function initMyTimer() 
{
    var timer:Timer = new Timer(2000);
    timer.addEventListener(TimerEvent.TIMER, voegtoe);
    timer.start();
}

public function voegtoe(e:TimerEvent):void
{
    trace ('set me up');
    var man:Man = new Man;
    man.x=Math.random()*650;
    man.y=Math.floor(Math.random()*(70))+350;
    addChild(man);
    man.setMyRemoveTimer();
}

现在您的 Man 类中应该包含这些函数来处理删除。请注意,我们在上面的代码中调用了 setMyRemoveTimer。您可以在构造函数中调用该函数,但我想在此处显示链接,因此我将其放在上面的代码中。该函数将存在于 Man 类中:

public function setMyRemoveTimer():void
{
    var removeTimer:Timer =  new Timer(2000,1);
    removeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeMe);
    removeTimer.start();
}


public function removeMe(e:TimerEvent):void
{
    var removeTimer:Timer = e.target as Timer;
    removeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, removeMe);
    this.parent.removeChild(this);
}

这只是一个建议,有很多方法可以做到这一点。如果你要创造很多人,我认为用一个数组来容纳这些人的想法是一个很好的想法,正如所建议的那样。

但这里的概念是,如果计时器是 Man 类的成员,则 eventHandler 将有权从其父类中删除侦听器以及 man 。

我不认为这是一个理想的实现,但我试图尽我所能匹配您当前的设计——即我不想进入数组来教授这个概念。

This can be done several different ways.

My suggestion is that you have a Class for your man. In this class you have a function that starts the removeTimer, and that should get triggered when the man is created. When the 2 seconds is up, we remove the event Listener and remove him from his parent.

The timer that is adding the men, should be in whatever class you want to instantiate your men in.

Here's an example

public function initMyTimer() 
{
    var timer:Timer = new Timer(2000);
    timer.addEventListener(TimerEvent.TIMER, voegtoe);
    timer.start();
}

public function voegtoe(e:TimerEvent):void
{
    trace ('set me up');
    var man:Man = new Man;
    man.x=Math.random()*650;
    man.y=Math.floor(Math.random()*(70))+350;
    addChild(man);
    man.setMyRemoveTimer();
}

Now your Man class should have these functions in it to handle the removal. Please note that from we are calling setMyRemoveTimer in the above code. You could call that function in the constructor, but I wanted to show the link here so I put it in the above code. That function will exist here in the Man Class :

public function setMyRemoveTimer():void
{
    var removeTimer:Timer =  new Timer(2000,1);
    removeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeMe);
    removeTimer.start();
}


public function removeMe(e:TimerEvent):void
{
    var removeTimer:Timer = e.target as Timer;
    removeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, removeMe);
    this.parent.removeChild(this);
}

This is just a suggestion, many ways to do this. If you are creating many men, I think the idea of an array holding these men is a good one as has been suggested.

But the concept here is that if the timer is a member of the Man class, the eventHandler will have access to remove the listener as well as the man from its parent.

I don't consider this an ideal implementation, but I was trying to match your current design as best I could --ie I didn't want to go into arrays to teach this concept.

舞袖。长 2024-11-15 02:58:41

引用许多男人的一个简单方法是使用 name 属性并调用每个 man.name="man"+i;

然后使用 getChildByName("man"+5) 检索您想要的影片剪辑。

A simple way to refer to many men is to use the name property and call each man.name="man"+i;

Then use getChildByName("man"+5) to retrieve whichever movieclip you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文