当 MovieClip 改变位置时是否有事件?

Rob*_*sen 1 flash actionscript-3 movieclip

我希望在 MovieClip 更改位置时收到通知,无论位置是由我的代码更改还是由内置拖动操作更改。有这样的活动吗?

div*_*ges 5

没有内置。您基本上有两个主要选项:

1) 要么反复轮询以检查位置是否发生变化。2) 创建一个扩展 MovieClip 的新类并覆盖设置的 x 和 y 属性以触发事件:

public class PosNotifyMC extends MovieClip
{

    // the name of the event we're firing
    public static const MOVED:String = "moved";

    // override the set x property
    override public function set x( n:Number ):void
    {
        super.x = n;
        this.dispatchEvent( new Event( PosNotifyMC.MOVED ) );
    }

    // override the set y property
    override public function set x( n:Number ):void
    {
        super.x = n;
        this.dispatchEvent( new Event( PosNotifyMC.MOVED ) );
    }

}
Run Code Online (Sandbox Code Playgroud)

如果您的位置变化很大,那么保留一个本地事件并重复触发它,而不是每次都创建一个新事件。如果需要,您还可以创建一个新的 Event 类来保存更新的位置。