A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: start/stopDrag() vs. MOUSE_MOVE?

  1. #1
    Senior Member
    Join Date
    Jan 2006
    Posts
    133

    Question start/stopDrag() vs. MOUSE_MOVE?

    Quick efficiency question.
    I want to change the default mouse pointer to a cool whizz-bang pointer. There's several ways I understand to do it... my question is which makes the most logical sense in relation to processing power, common sense, etc.?

    1) I could use the start and stopDrag methods to simply drag the myCursor_mc around the stage.

    2) I could use the MOUSE_MOVE event to basically set the x/y of myCursor_mc to the x/y of the mouse as the MOUSE_MOVE event triggers.
    Code:
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    
    function mouseMoveHandler(e:MouseEvent):void {
    	myCursor_mc.x = e.localX;
    	myCursor_mc.y = e.localY;
    	e.updateAfterEvent();
    	myCursor_mc.visible = true;
    }
    My first inclination was to just call the startDrag(); and be done with it; however, I'm assuming that the startDrag() method probably has it's own mousemove or enterframe events that are constantly processing "behind the scenes". So, I started thinking that, if the mouse IS NOT actively moving (dragging) then the startDrag() might still be using processing power to "drag" myCursor_mc to the same spot over and over again... as opposed to using the MOUSE_MOVE event to only "move/drag" myCursor_mc when needed?

    Anyway, I thought maybe someone who has a more intimate knowledge of the goings on in Flash might have some input as to a "preferred" method?

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    I'd lean toward startDrag for a couple reasons.

    The first is that mouseMoves can lag or get stuck when you move quickly, or the swf is doing something intensive.

    Secondly, since mouseMove is an event, you have a two frame delay - on frame zero you move your mouse and it get's caught, in frame one, the eventhandler is processed, and in frame two the graphics are redrawn to show the change.

    Lastly - and this one is just my speculation - there's a good chance that startDrag will be faster because it's a built in method of the language. When you write your AS3, it's operating on the most abstracted level of the AVM so you don't have access to low-level commands or tricks - because of that there are a lot of methods in the language that run much better than equivalent script would.

    Long story short - you should just test both and see which one feels better. And make sure you turn on bitmap caching for your alternate cursor - since your mouse can only move by even pixels it shouldn't lose quality and it will give you a good performance boost.

  3. #3
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    Thanks for the input, I was kind of thinking the same things... but Adobe's examples always use MOUSE_MOVE when "Customizing the Cursor":

    http://help.adobe.com/en_US/ActionSc...8a9b8f499-7ff5

    So, I thought maybe there was a reason.

    I'm actually going to be changing the cursor several times, depending on where the mouse is on the screen ... so should I still cacheAsBitmap?

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    MOUSE_MOVE is the worst. I've always been a big hater of startDrag, though... it does lock things quickly but if anything the motion is too sharp. What I mean is, it sticks out and looks kind of weird. My preferred method is:
    Code:
    var parallax:Array = [];
    something.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
    var _dragging:*;
    function beginDrag(evt:MouseEvent):void {
      var targ:DisplayObject = evt.target as DisplayObject;
      _dragging = targ;
      parallax = [mouseX-targ.x,mouseY-targ.y];
      targ.removeEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
      addEventListener(Event.ENTER_FRAME,doDrag,false,0,true);
      stage.addEventListener(MouseEvent.MOUSE_UP,killDrag);
    }
    function doDrag(evt:Event):void {
      _dragging.x = mouseX-parallax[0];
      _dragging.y = mouseY-parallax[1];
    }
    function killDrag(evt:Event):void {
      _dragging.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
      removeEventListener(Event.ENTER_FRAME,doDrag);
      stage.removeEventListener(MouseEvent.MOUSE_UP,killDrag);
    }
    I know it's more work, but it looks a lot better, partly because the dragging object does not lock to the position of the mouse, but rather maintains its relative position to the mouse from the moment the drag starts...

    Just my 2¢...

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    edit: Not sure if the I got doDrag backwards, I'm a little too tipsy to tell you right now; you'll know to reverse the equation mouseX-parallax[0] if things start flying backwards from the mouse at an exponential rate...

  6. #6
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I actually lean towards MOUSE_MOVE. It's easier to adjust the placement of the icon.

    Also, I'm not sure if this is a problem w/ AS3 (I haven't tried it), but I seem to recall there being a conflict if you try to use startDrag on two objects simultaniously. So if you want to let a user drag something, then you're in trouble because they're already dragging the cursor icon. You can move as many objects as you want with MOUSE_MOVE. Oh, and startDrag tends to make stuff "blink" to the cursor location on click, not that it matters with your current project.

    But uh, don't take my word for it. I haven't used startDrag since AS2.

    I would move the "myCursor_mc.visible = true;" line outside of the function though. There's no need to tell the clip to be visible every time the mouse moves; unless you have a timer that makes it disapear after sitting idle.

    as for cacheAsBitmap... you usually only want to do it if you have a highly detailed vector art that stays static. Otherwise, if the object is moving, you're making flash work extra hard to render the vector as a bitmap every frame. If you have the debug version of flash installed, you can right-click and have it "Show Redraw Regions" to see what flash is drawing each frame.

    There is another use for cacheAsBitmap though. If you want to use alpha channels in a mask, you can make both the object and its mask cacheAsBitmap. Very handy, but again, very processor intensive.

    Now I've gone and said too much.

    ------
    Edit:

    Oh, and Joshstrike's code is good. He's using ENTER_FRAME instead, which is probably going to be the quickest response. I guess the best method ultimately depends on your projects frame rate and how processor intensive it is.
    Last edited by Ralgoth; 05-06-2009 at 11:19 PM. Reason: Read Joshstrike's code
    Search first, asked questions later.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center