如何在拖动动画片段之后对其进行克隆,并使克隆在拖动前显示拖动的动画片段的位置?
这是我到目前为止所做的:
movieClip_1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
movieClip_1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
movieClip_1.stopDrag();
}
此外,我还想知道如何制作重置按钮,重置舞台上拖放的对象
答案 0 :(得分:0)
您可以在图书馆面板中选择要复制的符号,转到符号属性(右键单击符号并选择属性)并启用导出for ActionScript
作为一个符号示例,我创建了一个加号,并将其命名为Plus。
完成后,您可以轻松创建此符号的新实例:
addChild(new Plus());
请注意,Plus
与您在符号属性中给出的符号相同,
因此,如果您在库中命名符号MovieClip_1并将其导出为actionscript,则代码将如下所示:
addChild(new MovieClip_1());
如果你这样做,你会注意到符号将被放置在0,0所以接下来要做的是设置新实例的x,y位置。首先,您需要创建一个可以为此实例重用的变量:
var newPlus:MovieClip = new Plus();
然后您可以访问它的x
和y
属性,就像任何其他MovieClip一样。
由于您使用MOUSE_DOWN
事件创建副本,因此可以使用MouseEvent's stageX and stageY属性:
function fl_ClickToDrag_2(event:MouseEvent):void
{
var newPlus:MovieClip = new Plus();
newPlus.x = event.stageX;
newPlus.y = event.stageY;
addChild(newPlus);
}
只需要开始拖动:
function fl_ClickToDrag_2(event:MouseEvent):void
{
var newPlus:MovieClip = new Plus();
newPlus.x = event.stageX;
newPlus.y = event.stageY;
addChild(newPlus);
newPlus.startDrag();
}
当然,需要监听阶段MOUSE_UP
事件以停止拖动。
为此,我们可以使变量存储对MOUSE_DOWN
处理程序之外的最新重复剪辑的引用。这样,无论何时添加新剪辑,它都可以从MOUSE_UP
处理程序访问/可见,因此拖动可以停止。问题是检查是否首先添加了新剪辑,因为用户可以点击舞台而不创建副本。
这是一个评论片段:
import flash.display.MovieClip;
//a reference to the latest/most recently cloned movie clip
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
//make a new instance using the linkage/Export for ActionScript name
latestClone = new Plus();
//copy mouse position to movie clip position
latestClone.x = event.stageX;
latestClone.y = event.stageY;
//add it to the stage
addChild(latestClone);
//start dragging it, instead of the symbol manually placed on stage
latestClone.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
//if there was a clone already made/dragged
if(latestClone != null){
//then release it
latestClone.stopDrag();
}
}
请注意,我在舞台plus
上手动放置了一个加号符号。
因此,舞台上的实例为plus
,符号/类名称为Plus
:
您可以运行此here并下载zipped xfl Flash source here
更新根据您的评论,事实证明,重复的实例可以拖动并不复杂。这可以通过3个步骤进行设置:
MOUSE_DOWN
事件处理程序。从理论上讲,您可以重用现有的处理程序,但是您需要有一种区分影片剪辑克隆源和克隆实例的方法。拥有一个单独的处理程序(仅由克隆实例使用latestClone
引用:这样可以重用onStageReleased
处理程序来停止拖动克隆的实例根据前面使用Plus符号的示例代码,更新后的代码段如下所示:
import flash.display.MovieClip;
import flash.events.MouseEvent;
//a reference to the latest/most recently cloned movie clip
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
//make a new instance using the linkage/Export for ActionScript name
latestClone = new Plus();
//copy mouse position to movie clip position
latestClone.x = event.stageX;
latestClone.y = event.stageY;
//add it to the stage
addChild(latestClone);
//start dragging it, instead of the symbol manually placed on stage
latestClone.startDrag();
//add a separate MOUSE_DOWN event handler for the cloned instance
latestClone.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
}
//event handler for a cloned instance
function onClonedPlusPressed(event:MouseEvent):void{
//set the latest clone to be what's currently clicked (MouseEvent's current target)
//for more details on target/currentTarget and event bubbling see http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
latestClone = MovieClip(event.currentTarget);//event.currentTarget return an Object and latestClone is a MovieClip, therefore we cast the currentTarget Object to a MovieClip type
//start dragging
latestClone.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
//if there was a clone already made/dragged
if(latestClone != null){
//then release it
latestClone.stopDrag();
}
}