MouseEvent.MouseDown override move

Post here your questions about the OpenSpace 2.x or notify bugs and suggestions.

Moderators: Lapo, Bax

Nigey
Posts: 35
Joined: 17 Dec 2014, 21:55

MouseEvent.MouseDown override move

Postby Nigey » 18 Dec 2014, 10:57

Hi Guys,

I'm trying to implement some simple 'draggable map' code so that instead of just clicking for where they want to move, they can hold down the click button and drag around the map. The issue is that once I start using this event listener in game it overrides the player's move click method.

Here is the code I have in game for this feature:

Code: Select all

      public var mPos_x;
      public var mPos_y;
      const MIN:Number = 5;
      const MAX:Number = 10;

      public function initializeOpenSpace():void
      {
         stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
      }      
      //Takes initial position and begins calling 'Tick' method on every frame
      function mouseDown(e:Event):void
      {
         mPos_x = mouseX;
         mPos_y = mouseY;
         
         stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
         addEventListener(Event.ENTER_FRAME,tick);
      }
      // Ends the 'Tick' method calling every frame
      function mouseUp(e:Event):void
      {
         removeEventListener(Event.ENTER_FRAME,tick);
         stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
      }
      // Set move values to slightly more normal ranges
      function clamp(val:Number, min:Number = MIN, max:Number = MAX)
      {
         return Math.max(min, Math.min(max, val))
      }
      // Performed on every frame the mouse is held down
      function tick(e:Event):void
      {
         var move_x = mouseX - mPos_x;
         var move_y = mouseY - mPos_y;
         
         move_x = clamp(move_x, 5, 20);
         move_y = clamp(move_y, 5, 20);
         
         openSpace.panView( move_x , move_y, false, true);
         
         mPos_x = mouseX;
         mPos_y = mouseY;
      }


The feature itself works well (although without linear interpolation), however it over rides the move function?

Where is the original '.addEventListener(MouseEvent.MOUSE_DOWN,OpenSpaceDoesAMethod);' code, and can I access it?

Many Thanks,
Nigel Clark
User avatar
Bax
Site Admin
Posts: 4337
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: MouseEvent.MouseDown override move

Postby Bax » 22 Dec 2014, 10:33

OpenSpace doesn't use the MOUSE_DOWN event to move the avatar. In fact it moves on the MOUSE_UP event.
You are free to add your listener directly on the OpenSpace instance: you will find out that it works.
Now, I would use a different strategy: when the mouse is down, start listening to the MOUSE_MOVE event and when the mouse is up again stop listening.
In the MOUSE_MOVE event handler you can then pan the map accordingly.
What do you think?
Paolo Bax
The SmartFoxServer Team
Nigey
Posts: 35
Joined: 17 Dec 2014, 21:55

Re: MouseEvent.MouseDown override move

Postby Nigey » 22 Dec 2014, 11:04

Hi Bax,

That's a great idea and really helpful, thank you! One question: I can't find MOUSE_UP at all in the OpenSpace API. In what method in which class is it?
User avatar
Bax
Site Admin
Posts: 4337
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: MouseEvent.MouseDown override move

Postby Bax » 22 Dec 2014, 11:11

It's not an event specific to OpenSpace. Just use this:

Code: Select all

openSpace.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

(the same with the MOUSE_DOWN event, or MOUSE_MOVE event)
Paolo Bax
The SmartFoxServer Team
Nigey
Posts: 35
Joined: 17 Dec 2014, 21:55

Re: MouseEvent.MouseDown override move

Postby Nigey » 22 Dec 2014, 12:49

Works perfect, thanks!

Return to “OpenSpace v2 discussions and help”

Who is online

Users browsing this forum: No registered users and 2 guests