Page 1 of 1

MouseEvent.MouseDown override move

Posted: 18 Dec 2014, 10:57
by Nigey
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

Re: MouseEvent.MouseDown override move

Posted: 22 Dec 2014, 10:33
by Bax
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?

Re: MouseEvent.MouseDown override move

Posted: 22 Dec 2014, 11:04
by Nigey
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?

Re: MouseEvent.MouseDown override move

Posted: 22 Dec 2014, 11:11
by Bax
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)

Re: MouseEvent.MouseDown override move

Posted: 22 Dec 2014, 12:49
by Nigey
Works perfect, thanks!