您可以在 ActionScript 中使用两种技术使用户可以使用鼠标来移动显示对象。在这两种情况下,会使用两个鼠标事件:按下鼠标按键时,通知对象跟随鼠标光标;松开鼠标按键时,通知对象停止跟随鼠标光标。
注:
Flash Player 11.3 及更高版本,AIR 3.3 及更高版本:您也可以使用 MouseEvent.RELEASE_OUTSIDE 事件涵盖用户在包含 Sprite 的边界外释放鼠标按钮的情况。
第一种技术使用
startDrag()
方法,比较简单,但限制较多。按下鼠标按键时,将调用要拖动的显示对象的
startDrag()
方法。松开鼠标按键时,将调用
stopDrag()
方法。Sprite 类定义这两个功能,因此移动的对象必须是 Sprite 或它的子类。
// This code creates a mouse drag interaction using the startDrag()
// technique.
// square is a MovieClip or Sprite instance).
import flash.events.MouseEvent;
// This function is called when the mouse button is pressed.
function startDragging(event:MouseEvent):void
{
square.startDrag();
}
// This function is called when the mouse button is released.
function stopDragging(event:MouseEvent):void
{
square.stopDrag();
}
square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
square.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
这种方法有一个非常大的限制:使用
startDrag()
时,每次只能拖动一个项目。如果正在拖动一个显示对象,然后对另一个显示对象调用了
startDrag()
方法,则第一个显示对象会立即停止跟随鼠标。例如,如果
startDragging()
函数如下发生了更改,则只拖动
circle
对象,而不管
square.startDrag()
方法调用:
function startDragging(event:MouseEvent):void
{
square.startDrag();
circle.startDrag();
}
由于每次只能使用
startDrag()
拖动一个对象,因此,可以对任何显示对象调用
stopDrag()
方法,这会停止当前正在拖动的任何对象。
如果需要拖动多个显示对象,或者为了避免因多个对象可能使用
startDrag()
而发生冲突,最好使用鼠标跟随方法来创建拖动效果。通过这种技术,当按下鼠标按键时,会将函数作为舞台的
mouseMove
事件的侦听器来订阅。然后,每次鼠标移动时都会调用此函数,它将使所拖动的对象跳到鼠标所在的 x,y 坐标。松开鼠标按键后,取消此函数作为侦听器的订阅,这意味着鼠标移动时不再调用该函数且对象停止跟随鼠标光标。下面是演示说明此技术的一些代码:
// This code moves display objects using the mouse-following
// technique.
// circle is a DisplayObject (e.g. a MovieClip or Sprite instance).
import flash.events.MouseEvent;
var offsetX:Number;
var offsetY:Number;
// This function is called when the mouse button is pressed.
function startDragging(event:MouseEvent):void
{
// Record the difference (offset) between where
// the cursor was when the mouse button was pressed and the x, y
// coordinate of the circle when the mouse button was pressed.
offsetX = event.stageX - circle.x;
offsetY = event.stageY - circle.y;
// tell Flash Player to start listening for the mouseMove event
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCircle);
}
// This function is called when the mouse button is released.
function stopDragging(event:MouseEvent):void
{
// Tell Flash Player to stop listening for the mouseMove event.
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCircle);
}
// This function is called every time the mouse moves,
// as long as the mouse button is pressed down.
function dragCircle(event:MouseEvent):void
{
// Move the circle to the location of the cursor, maintaining
// the offset between the cursor's location and the
// location of the dragged object.
circle.x = event.stageX - offsetX;
circle.y = event.stageY - offsetY;
// Instruct Flash Player to refresh the screen after this event.
event.updateAfterEvent();
}
circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
除使显示对象跟随鼠标光标之外,经常需要将拖动的对象移动到显示的前方,以使其像是浮动在所有其他对象上。例如,假设您有两个对象(一个圆和一个正方形),都可以跟随鼠标移动。如果圆在显示列表中出现在正方形之下,您单击并拖动圆时光标会出现在正方形之上,圆好像在正方形之后滑动,这样中断了拖放视觉效果。您可以使用拖放交互组件避免这一点,以便在单击圆时圆移到显示列表的顶部,使圆会始终出现在其他任何内容的顶部。
以下代码(根据上一示例改写)使两个显示对象(一个圆和一个正方形)可跟随鼠标移动。只要在任一个显示对象上按下鼠标按键,该显示对象就会移到舞台显示列表的顶部,所以拖动的项目始终出现在顶部。(新代码或根据以前代码更改的代码显示为粗体。)
// This code creates a drag-and-drop interaction using the mouse-following
// technique.
// circle and square are DisplayObjects (e.g. MovieClip or Sprite
// instances).
import flash.display.DisplayObject;
import flash.events.MouseEvent;
var offsetX:Number;
var offsetY:Number;
var draggedObject:DisplayObject;
// This function is called when the mouse button is pressed.
function startDragging(event:MouseEvent):void
{
// remember which object is being dragged
draggedObject = DisplayObject(event.target);
// Record the difference (offset) between where the cursor was when
// the mouse button was pressed and the x, y coordinate of the
// dragged object when the mouse button was pressed.
offsetX = event.stageX - draggedObject.x;
offsetY = event.stageY - draggedObject.y;
// move the selected object to the top of the display list
stage.addChild(draggedObject);
// Tell Flash Player to start listening for the mouseMove event.
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
// This function is called when the mouse button is released.
function stopDragging(event:MouseEvent):void
{
// Tell Flash Player to stop listening for the mouseMove event.
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
// This function is called every time the mouse moves,
// as long as the mouse button is pressed down.
function dragObject(event:MouseEvent):void
{
// Move the dragged object to the location of the cursor, maintaining
// the offset between the cursor's location and the location
// of the dragged object.
draggedObject.x = event.stageX - offsetX;
draggedObject.y = event.stageY - offsetY;
// Instruct Flash Player to refresh the screen after this event.
event.updateAfterEvent();
}
circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
square.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
若要进一步扩展这种效果,如在几副纸牌(或几组标记)之间移动纸牌(或标记)的游戏中,您可以在“拿出”拖动对象时将拖动对象添加到舞台的显示列表中,然后在“放入”拖动对象时(通过松开鼠标按键)将拖动对象添加到另一个显示列表中(如“那副纸牌”或“那组标记”)。
最后,要增强效果,您可以在单击显示对象时(开始拖动显示对象时)对显示对象应用投影滤镜,然后在松开对象时删除投影。有关在 ActionScript 中使用投影滤镜和其他显示对象滤镜的详细信息,请参阅
过滤显示对象
。