我正在 as3 上制作 DragDrop 类。我正在尝试使可移动的影片剪辑“粘”到目标 MovieClip。我已经完成了基本的拖放和定位/粘贴工作,但是当我尝试使用 Enter Frame 创建“缓动”效果时,影片剪辑会以某种方式移动到 0 x 和 y 位置。
这是有效的代码(没有 EnterFrame 事件)。
package {
public class DragDrop {
public var clip:MovieClip;
public var targ:MovieClip;
public var myHomeX:Number;
public var myHomeY:Number;
public var myFinalX:Number;
public var myFinalY:Number;
public function selectClip(clipV:MovieClip,targV:MovieClip):void {
clip = clipV;
targ = targV;
var myHomeX = clip.x;
var myHomeY = clip.y;
var myFinalX = targ.x;
var myFinalY = targ.y;
clip.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
clip.addEventListener(MouseEvent.MOUSE_UP,stopDragging);
clip.addEventListener(MouseEvent.ROLL_OVER,rollOver);
clip.addEventListener(MouseEvent.ROLL_OUT,rollOut);
function startDragging(e:MouseEvent):void {
clip.startDrag();
clip.beingDragged = true;
clip.parent.setChildIndex(clip,clip.parent.numChildren - 1);
}
function stopDragging(e:MouseEvent):void {
clip.stopDrag();
clip.beingDragged = false;
var onTarget:MovieClip;
if (clip.dropTarget != null)
{
onTarget = e.target.dropTarget.parent;
}
else
{
onTarget = null;
}
if ((onTarget == targ))
{
clip.onTarget = true;
targ.gotoAndStop(2);
clip.x = myFinalX;
clip.y = myFinalY;
}
else
{
clip.onTarget = false;
targ.gotoAndStop(1);
clip.x = myHomeX;
clip.y = myHomeY;
}
}
function rollOver(e:MouseEvent) {
clipGlow.restart();
}
function rollOut(e:MouseEvent) {
clipGlow.reverse();
}
}
}
}
这是不起作用的代码(使用 EnterFrame 事件)。
package {
public class DragDrop {
public var clip:MovieClip;
public var targ:MovieClip;
public var clip:MovieClip;
public var targ:MovieClip;
public var myHomeX:Number;
public var myHomeY:Number;
public var myFinalX:Number;
public var myFinalY:Number;
public function selectClip(clipV:MovieClip,targV:MovieClip):void {
clip = clipV;
targ = targV;
var myHomeX = clip.x;
var myHomeY = clip.y;
var myFinalX = targ.x;
var myFinalY = targ.y;
clip.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
clip.addEventListener(MouseEvent.MOUSE_UP,stopDragging);
clip.addEventListener(MouseEvent.ROLL_OVER,rollOver);
clip.addEventListener(MouseEvent.ROLL_OUT,rollOut);
clip.addEventListener(Event.ENTER_FRAME,slide);
function startDragging(e:MouseEvent):void {
clip.startDrag();
clip.beingDragged = true;
clip.parent.setChildIndex(clip,clip.parent.numChildren - 1);
}
function stopDragging(e:MouseEvent):void {
clip.stopDrag();
clip.beingDragged = false;
var onTarget:MovieClip;
if (clip.dropTarget != null)
{
onTarget = e.target.dropTarget.parent;
}
else
{
onTarget = null;
}
if ((onTarget == targ))
{
clip.onTarget = true;
targ.gotoAndStop(2);
}
else
{
clip.onTarget = false;
targ.gotoAndStop(1);
}
}
function rollOver(e:MouseEvent) {
clipGlow.restart();
}
function rollOut(e:MouseEvent) {
clipGlow.reverse();
}
function slide(e:Event):void {
if (! clip.beingDragged && ! clip.onTarget)
{
clip.x -= clip.x - clip.myHomeX / 5;
clip.y -= clip.y - clip.myHomeY / 5;
clip.scaleX += (1 - clip.scaleX) / 5;
clip.scaleY += (1 - clip.scaleY) / 5;
}
else if (! clip.beingDragged && clip.onTarget)
{
clip.x -= clip.x - clip.myFinalX / 5;
clip.y -= clip.y - clip.myFinalY / 5;
clip.scaleX += (1.5 - clip.scaleX) / 5;
clip.scaleY += (1.5 - clip.scaleY) / 5;
}
}
}
}
}
提前感谢您的帮助。:)