actionscript 3 - Cloning object (Drag and drop) Flash As3 -
how make clone of movieclip after has been dragged, , make clone appear dragged movieclip before being dragged?
this have far:
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(); }
also know how make reset button, reset dragged , dropped objects on stage
you can select symbol want make duplicate of in library panel, go symbol properties (by right clicking symbol , selecting properties) , enable export actionscript
as symbol example, i've created plus symbol , named symbol plus.
once you've done that, can create new instance of symbol:
addchild(new plus());
note plus
same gave symbol in symbol properties, if name symbol movieclip_1 in library , it's exported actionscript code this:
addchild(new movieclip_1());
if this, you'll notice symbol placed @ 0,0 next thing set x,y position of new instance. first need create variable can re-use instance:
var newplus:movieclip = new plus();
then can access it's x
, y
properties, other movieclip.
since you're using mouse_down
event create duplicate, can use mouseevent's stagex , stagey properties:
function fl_clicktodrag_2(event:mouseevent):void { var newplus:movieclip = new plus(); newplus.x = event.stagex; newplus.y = event.stagey; addchild(newplus); }
just need start dragging:
function fl_clicktodrag_2(event:mouseevent):void { var newplus:movieclip = new plus(); newplus.x = event.stagex; newplus.y = event.stagey; addchild(newplus); newplus.startdrag(); }
of course, need listen stage mouse_up
event stop dragging. that, can make variable storing reference latest duplicated clip outside of mouse_down
handler. way, whenever new clip added, accessible/visible mouse_up
handler too, dragging can stop. catch check if new clip has been aded first, because user may click on stage without creating duplicate.
here's commented snippet:
import flash.display.movieclip; //a reference latest/most cloned movie clip var latestclone:movieclip; plus.addeventlistener(mouseevent.mouse_down, onpluspressed); function onpluspressed(event:mouseevent):void { //make new instance using linkage/export actionscript name latestclone = new plus(); //copy mouse position movie clip position latestclone.x = event.stagex; latestclone.y = event.stagey; //add stage addchild(latestclone); //start dragging it, instead of symbol manually placed on stage latestclone.startdrag(); } stage.addeventlistener(mouseevent.mouse_up, onstagereleased); function onstagereleased(event:mouseevent):void { //if there clone made/dragged if(latestclone != null){ //then release latestclone.stopdrag(); } }
notice i've named single plus symbol placed manually on stage plus
. instance on stage plus
, symbol/class name plus
:
you can run here , download zipped xfl flash source here
update based on comment bellow, turns out it's not complicated make duplicated instances draggable. can setup in 3 steps:
- add
mouse_down
event handler cloned instance. in theory re-use existing handler, need have way of differentiating movie clip clone source , cloned instances. cleaner have separate handler, used cloned instances - set
latestclone
reference clicked cloned instance: wayonstagereleased
handler can re-used stop dragging of cloned instances - start dragging
based on previous example code using plus symbol, updated snippet looks this:
import flash.display.movieclip; import flash.events.mouseevent; //a reference latest/most cloned movie clip var latestclone:movieclip; plus.addeventlistener(mouseevent.mouse_down, onpluspressed); function onpluspressed(event:mouseevent):void { //make new instance using linkage/export actionscript name latestclone = new plus(); //copy mouse position movie clip position latestclone.x = event.stagex; latestclone.y = event.stagey; //add stage addchild(latestclone); //start dragging it, instead of symbol manually placed on stage latestclone.startdrag(); //add separate mouse_down event handler cloned instance latestclone.addeventlistener(mouseevent.mouse_down,onclonedpluspressed); } //event handler cloned instance function onclonedpluspressed(event:mouseevent):void{ //set latest clone what's clicked (mouseevent's current target) //for more details on target/currenttarget , event bubbling see http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html latestclone = movieclip(event.currenttarget);//event.currenttarget return object , latestclone movieclip, therefore cast currenttarget object movieclip type //start dragging latestclone.startdrag(); } stage.addeventlistener(mouseevent.mouse_up, onstagereleased); function onstagereleased(event:mouseevent):void { //if there clone made/dragged if(latestclone != null){ //then release latestclone.stopdrag(); } }
Comments
Post a Comment