如何在 AS3 中让 MovieClip 自行删除?

发布于 2024-09-08 11:18:57 字数 1826 浏览 6 评论 0 原文

AS3 中的 removeMovieClip() 等效项是什么?

显然很多人都有同样的问题:
StackOverflow:

  1. 如何完全删除as3中的影片剪辑
  2. 删除影片剪辑 as3
  3. 如何删除 childmovieclip 并添加到新的父 movieclip

其他:

  1. 在 AS3 中删除MovieClip(this)?
  2. 销毁/删除影片剪辑???
  3. 删除影片剪辑

但他们的解决方案似乎都不起作用,对我来说:

我正在使用 AS3 开发 flash CS4

我有一个非常带有一个名为单击的按钮的简单电影。按下按钮后,就会创建一个新的coin实例:

this.click.addEventListener(MouseEvent.CLICK,justclick);
function justclick(e:MouseEvent){
    var money=new coin
    this.addChild(money)
    money.x=e.stageX
    money.y=e.stageY
}

它可能不是最好的代码,但它工作得很好。现在,硬币 MovieClip 应该显示一个小动画并自行移除。在旧的 AS2 中,我会添加:

this.removeMovieClip()

在动画的最后一帧中。但这在 AS3 中不存在。
我已经尝试过,但没有成功:

this.parent.removeChild(this) // 'Cannot access a property or method of nullobject reference'...     

this.removeMovieClip() // 'removeMovieClip is not a function'      

removeMovieClip(this) //'call to possibly undefined method removeMovieClip'       

unloadMovie(this)//'call to possibly undefined method removeMovieClip'       

解决方案?

谢谢,

What is the equivalent to removeMovieClip() in AS3?

Apparently many have the same question:
StackOverflow:

  1. How to completely remove a movieclip in as3
  2. Remove movie clip as3
  3. How to remove childmovieclip and add to new parent movieclip

Others:

  1. removeMovieClip(this) in AS3?
  2. Destroy/Delete a Movieclip???
  3. Remove movie clip

But none of their solutions seem to work, for me:

Im working on flash CS4 with AS3:

I have a very simple movie with a single button called click. On pressing the button, a new instance of coin is created:

this.click.addEventListener(MouseEvent.CLICK,justclick);
function justclick(e:MouseEvent){
    var money=new coin
    this.addChild(money)
    money.x=e.stageX
    money.y=e.stageY
}

It might not be the best code, but it works fine. Now, the coin MovieClip is supposed to show a small animation and remove itself. In good old AS2 I would have added:

this.removeMovieClip()

in the last frame of the animation. But this doesn't exist in AS3.
I have tried, without success:

this.parent.removeChild(this) // 'Cannot access a property or method of nullobject reference'...     

this.removeMovieClip() // 'removeMovieClip is not a function'      

removeMovieClip(this) //'call to possibly undefined method removeMovieClip'       

unloadMovie(this)//'call to possibly undefined method removeMovieClip'       

Solutions?

Thanks,

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

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

发布评论

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

评论(5

享受孤独 2024-09-15 11:18:57
this.parent.removeChild(this);

这个应该可以工作;这就是我用的。当我切换到 AS3 时遇到的一个问题是,有时它不会作为子项添加,因此您可能需要检查一下。如果您还没有导入 flash.display,则还必须将其放在顶部:

import flash.display.*

您还应该在删除它之前删除其上的事件侦听器。

this.parent.removeChild(this);

This one should be working; it's what I use. One problem I had when I switched to AS3 is that sometimes it wouldn't be added as a child right, so you might want to check that. You also have to import flash.display via putting this at the top if you're not already:

import flash.display.*

You should also remove the event listener on it before removing it.

憧憬巴黎街头的黎明 2024-09-15 11:18:57

如果您的动画在第 20 帧结束。

注意:使用 19 是因为 Flash 从零 (0) 开始计数帧,类似于数组索引。

class animatedCloud
{

    public function animatedCloud(){
        addFrameScript(19, frame20);
    }

    private function frame20(){
        parent.removeChild(this);
    }
}

If your animation is ending on frame 20.

note: using 19 because flash count frames from zero(0) similar to array index.

class animatedCloud
{

    public function animatedCloud(){
        addFrameScript(19, frame20);
    }

    private function frame20(){
        parent.removeChild(this);
    }
}
别闹i 2024-09-15 11:18:57

始终确保那些自动删除的影片剪辑可以被垃圾收集。
此解决方案从加载的 swf 库符号中擦除了我的所有实例:

var mc:MovieClip = new definition() as MovieClip;
addChild(mc);

mc.x = 1000 * Math.random();
mc.y = 1000 * Math.random();

mc.addFrameScript(mc.totalFrames - 1, function onLastFrame():void
{
    mc.stop();
    mc.parent.removeChild(mc);
    mc = null;
});

Always ensure that those self removing movieclips can get garbage collected.
This solution wiped away all my instances from a loaded swf's library symbol:

var mc:MovieClip = new definition() as MovieClip;
addChild(mc);

mc.x = 1000 * Math.random();
mc.y = 1000 * Math.random();

mc.addFrameScript(mc.totalFrames - 1, function onLastFrame():void
{
    mc.stop();
    mc.parent.removeChild(mc);
    mc = null;
});
罗罗贝儿 2024-09-15 11:18:57
public static function removeDisplayObject(displayObject:DisplayObject):void {
    /* normal code
    if(displayObject && displayObject.parent){
        displayObject.parent.removeChild(displayObject);
    }
     */
    displayObject ? displayObject.parent ? displayObject.parent.removeChild(displayObject) : null : null;
}
public static function removeDisplayObject(displayObject:DisplayObject):void {
    /* normal code
    if(displayObject && displayObject.parent){
        displayObject.parent.removeChild(displayObject);
    }
     */
    displayObject ? displayObject.parent ? displayObject.parent.removeChild(displayObject) : null : null;
}
对岸观火 2024-09-15 11:18:57

我在 MovieClip 末尾的额外空白关键帧中使用它,该关键帧应该自行删除:

stop();
MovieClip(parent).removeChild(this);

发现它是正确且最佳的解决方案。

I use, in an extra blank keyframe at the end of the MovieClip which should remove itself:

stop();
MovieClip(parent).removeChild(this);

Found it to be the proper and best solution.

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