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:

  1. //co-ordsofobjectsonstage
  2. sky.x=0
  3. sky.y=0
  4. ground.x=-14
  5. ground.y=252
  6. seeds.x=760
  7. seeds.y=30
  8. wateringcan.x=550
  9. wateringcan.y=20

 

  1. //functiontodeterminewheretheholewillspawn-almost100%certainitwontspawnabovetheground
  2. randomlyplaceobject(hole);
  3. //randomplacementfunction
  4. functionrandomlyplaceobject(objecttoplace:movieclip):void{
  5. //controlsforwherespawncanbe
  6. varxmin:number=50;
  7. varxmax:number=stage.stagewidth-50;
  8. varymin:number=350;
  9. varymax:number=stage.stageheight-50;
  10. //mathtodeterminlocationofholespawn
  11. objecttoplace.x=xmin+(xmax-xmin)*math.random();
  12. objecttoplace.y=ymin+(ymax-ymin)*math.random();
  13. }
  14. //draganddroptime
  15. var dragged_mc:movieclip;
  16. varcircles:array=[seeds,wateringcan];
  17. foreach(varseeds:movieclipincircles){
  18. seeds.addeventlistener(mouseevent.mouse_down,pickup);
  19. seeds.startx=seeds.x;
  20. seeds.starty=seeds.y;
  21. wateringcan.addeventlistener(mouseevent.mouse_down,pickup);
  22. wateringcan.addeventlistener(mouseevent.mouse_up,dropit);
  23. wateringcan.startx=wateringcan.x;
  24. wateringcan.starty=wateringcan.y;
  25. }
  26. function pickup(event:mouseevent):void{
  27. //nolongerneedtokeeptrackofstartx&startyherebecausethat'salreadybeendoneupabove
  28. stage.addeventlistener(mouseevent.mouse_up,dropit);
  29. dragged_mc=movieclip(event.currenttarget);
  30. event.currenttarget.startdrag(true);
  31. event.currenttarget.parent.addchild(event.currenttarget);
  32. }
  33. function dropit(event:mouseevent):void{
  34. stage.removeeventlistener(mouseevent.mouse_up,dropit);
  35. dragged_mc.stopdrag();
  36. //checktoseeiftheeventtargetistouchingholeusinghittestobject
  37. if(event.target.hittestobject(hole)){
  38. event.target.buttonmode=false;
  39. event.target.x=hole.x;
  40. event.target.y=hole.y;
  41. }
  42. }


More discussions in ActionScript 3


adobe

Comments

Popular posts from this blog

Soustraire une selection

After Effects: could not find dvaeve_dialogs.txt

Illustrator cs6 "Invalid Serial Number"