Drag works fine, drop does not.
hello,
i new as3 , having issue drag , drop, more accurately drop part. have tried few different tutorials , have tried having objects called stage library on stage when starts. dragging works fine object never released when mouse button is.
if wouldn't mind taking , perhaps pointing out going wrong appreciate it.
code below.
//co-ords of objects on stage sky.x = 0 sky.y = 0 ground.x = -14 ground.y = 252 seeds.x = 760 seeds.y = 30 wateringcan.x = 550 wateringcan.y = 20 // function determine hole spawn - 100% wont spawn above ground randomlyplaceobject(hole); // random placement function function randomlyplaceobject(objecttoplace: movieclip): void { // controls spawn can var xmin: number = 50; var xmax: number = stage.stagewidth - 50; var ymin: number = 350; var ymax: number = stage.stageheight - 50; //math determin location of hole spawn objecttoplace.x = xmin + (xmax - xmin) * math.random(); objecttoplace.y = ymin + (ymax - ymin) * math.random(); } //drag , drop time var circles: array = [seeds, wateringcan]; each(var seeds: movieclip in circles) { seeds.addeventlistener(mouseevent.mouse_down, pickup); seeds.addeventlistener(mouseevent.mouse_up, dropit); seeds.startx = seeds.x; seeds.starty = seeds.y; wateringcan.addeventlistener(mouseevent.mouse_down, pickup); wateringcan.addeventlistener(mouseevent.mouse_up, dropit); wateringcan.startx = wateringcan.x; wateringcan.starty = wateringcan.y; } function pickup(event: mouseevent): void { // no longer need keep track of startx & starty here because that's been done above event.target.startdrag(true); event.target.parent.addchild(event.target); } function dropit(event: mouseevent): void { event.target.stopdrag(); // check see if event target touching hole using hittestobject if (event.target.hittestobject(hole)) { event.target.buttonmode = false; event.target.x = hole.x; event.target.y = hole.y; } }
use:
- //co-ordsofobjectsonstage
- sky.x=0
- sky.y=0
- ground.x=-14
- ground.y=252
- seeds.x=760
- seeds.y=30
- wateringcan.x=550
- wateringcan.y=20
- //functiontodeterminewheretheholewillspawn-almost100%certainitwontspawnabovetheground
- randomlyplaceobject(hole);
- //randomplacementfunction
- functionrandomlyplaceobject(objecttoplace:movieclip):void{
- //controlsforwherespawncanbe
- varxmin:number=50;
- varxmax:number=stage.stagewidth-50;
- varymin:number=350;
- varymax:number=stage.stageheight-50;
- //mathtodeterminlocationofholespawn
- objecttoplace.x=xmin+(xmax-xmin)*math.random();
- objecttoplace.y=ymin+(ymax-ymin)*math.random();
- }
- //draganddroptime
- var dragged_mc:movieclip;
- varcircles:array=[seeds,wateringcan];
- foreach(varseeds:movieclipincircles){
- seeds.addeventlistener(mouseevent.mouse_down,pickup);
- seeds.startx=seeds.x;
- seeds.starty=seeds.y;
- wateringcan.addeventlistener(mouseevent.mouse_down,pickup);
- wateringcan.addeventlistener(mouseevent.mouse_up,dropit);
- wateringcan.startx=wateringcan.x;
- wateringcan.starty=wateringcan.y;
- }
- function pickup(event:mouseevent):void{
- //nolongerneedtokeeptrackofstartx&startyherebecausethat'salreadybeendoneupabove
- stage.addeventlistener(mouseevent.mouse_up,dropit);
- dragged_mc=movieclip(event.currenttarget);
- event.currenttarget.startdrag(true);
- event.currenttarget.parent.addchild(event.currenttarget);
- }
- function dropit(event:mouseevent):void{
- stage.removeeventlistener(mouseevent.mouse_up,dropit);
- dragged_mc.stopdrag();
- //checktoseeiftheeventtargetistouchingholeusinghittestobject
- if(event.target.hittestobject(hole)){
- event.target.buttonmode=false;
- event.target.x=hole.x;
- event.target.y=hole.y;
- }
- }
More discussions in ActionScript 3
adobe
Comments
Post a Comment