flash - How drag a Sprite smoothly on-screen in actionscript -


first of all, question same one:

how drag image move smoothly on screen, actionscript?

i want dragged sprites keep mouse on-screen, smoothly, without lagging behind.

and notice in old actionscript 3 cookbook way-back-when used similar solution draggablesprite used in above link. namely, use stage instance listen mousemove event , read event.stagex , stagey properties.

i've done that.

but sprite still doesn't stay locked mouse cursor. lags behind. feel must missing something. however, if solution posted above (ie listen stage's mousemove , use event.stagex/y) still current , problem i'm describing should not occurring, please let me know. though it's not supposed work, i've tried event.updateafterevent() , doesn't seem have positive effect.

any or advice appreciated.

here's simple example of how i've written handlers. should work as-is if pasted new project.

i should add i'm compiling desktop application using adobe air. run time factor???

package {   import flash.display.sprite;   import flash.events.mouseevent;    [swf(width="1280", height="720", framerate="30")]   public class test_drag extends sprite {      private var testdragsprite:testdragsprite;      public function test_drag() {       super();        graphics.clear();       graphics.beginfill(0x0000ff);       graphics.drawrect(0, 0, 1280, 720);       graphics.endfill();        testdragsprite = new testdragsprite();       addchild(testdragsprite);        testdragsprite.addeventlistener(mouseevent.mouse_down, testdragsprite_mousehandler);       testdragsprite.addeventlistener(mouseevent.mouse_up, testdragsprite_mousehandler);     }      private function testdragsprite_mousehandler(e:mouseevent):void {       switch (e.type) {         case mouseevent.mouse_down: {           stage.addeventlistener(mouseevent.mouse_move, mousemovehandler);           break;         }         case mouseevent.mouse_up: {           stage.removeeventlistener(mouseevent.mouse_move, mousemovehandler);           break;         }       }     }      private function mousemovehandler(e:mouseevent):void {       //-20 keep sprite centered on mouse       testdragsprite.x = e.stagex - 20;       testdragsprite.y = e.stagey - 20;       //e.updateafterevent(); //strange effect, doesn't solve problem.     }   } } import flash.display.sprite; internal class testdragsprite extends sprite {   public function testdragsprite() {     super();     graphics.linestyle(1, 0xdddddd);     graphics.beginfill(0xff0000);     graphics.drawroundrect(0, 0, 40, 40, 12);     graphics.endfill();   }       } 

there going little lag, but:

the first 2 suggestions make noticeable change code/performance.

  • enable hardware graphics acceleration; edit air application descriptor file (xml) , set rendermode direct (or gpu). consult adobe air details.

    <!-- render mode app (either auto, cpu, gpu, or direct). optional. default auto --> <rendermode>direct</rendermode> 
  • use startdrag , enddrag bypass manual assignments in mousemovehandler.

replace :

testdragsprite.addeventlistener(mouseevent.mouse_down, testdragsprite_mousehandler); testdragsprite.addeventlistener(mouseevent.mouse_up, testdragsprite_mousehandler); 

with :

testdragsprite.addeventlistener(mouseevent.mouse_down, mousedown); testdragsprite.addeventlistener(mouseevent.mouse_up, mouseup); 

add new handlers:

private function mousedown(e:mouseevent):void {     testdragsprite.startdrag(); } private function mouseup(e:mouseevent):void {     testdragsprite.stopdrag(); } 
  • increase frame rate, watch cpu usage application/game becomes more complicated may need lower allow game logic enough time complete between frames (otherwise end called long frames, can use free adobe scout profiling tool watch these , lots of other things).

  • add frame rate hud display can monitor when actual framerate lower requested framerate (this debugging)

  • if game 2d based, consider using open-source starling framework content rendered directly gpu vs flash's standard display list objects.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -