我想在MovieClip更改位置时收到通知,无论我的代码是否更改了位置,还是通过内置拖动操作。 有这样的事件吗?
答案 0 :(得分:4)
没有内置。你基本上有两个主要选择:
1)重复轮询以检查位置是否已更改。 2)创建一个扩展MovieClip的新类,并覆盖set 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 ) );
}
}
如果您的位置发生了很大变化,那么请保留一个本地事件并反复触发,而不是每次都创建一个新事件。如果您愿意,还可以创建一个包含更新位置的新Event类。
答案 1 :(得分:2)
+1 @divillysausages:)
然后你可以做一些脏事并“手动”监控这些变化。
MovieClip类是动态的,因此我们可以创建变量来存储最后的X / Y位置,然后在EnterFrame上执行增量。如果delta不为0,则剪辑已移动。
真的很难看:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Mover extends Sprite
{
private var mc:MovieClip;
public function Mover()
{
mc = new MovieClip();
mc.graphics.beginFill( 0xCC00FF );
mc.graphics.drawRect( 0, 0, 50, 50 );
addChild( mc );
//storing dynamic properties
mc.lx = mc.x;
mc.ly = mc.y;
//and add monitoring function
mc.addEventListener( Event.ENTER_FRAME, onEnterFrameHandler );
mc.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler );
mc.addEventListener( MouseEvent.MOUSE_UP, mouseHandler );
//move clip from outisde
// this.addEventListener( Event.ENTER_FRAME, moveClipHandler );
}
private function moveClipHandler(e:Event):void
{
mc.x++
}
private function mouseHandler(e:MouseEvent):void
{
switch( e.type )
{
case MouseEvent.MOUSE_DOWN: ( e.target as MovieClip ).startDrag(); break;
case MouseEvent.MOUSE_UP: ( e.target as MovieClip ).stopDrag(); break;
}
}
private function onEnterFrameHandler(e:Event):void
{
var m:MovieClip = e.target as MovieClip;
//check the delta
if ( m.x != m.lx || m.y != m.ly ) trace( 'moved!' );
m.lx = m.x;
m.ly = m.y;
}
}
}
但它有效:)
最好将delta检查集中在一个类中,这样可以根据需要注册/取消注册剪辑和DisplayObjects。