Be a Supporter!

Quick Q: Stage boundary test

  • 115 Views
  • 2 Replies
New Topic Respond to this Topic
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Quick Q: Stage boundary test 2013-08-21 01:09:17 Reply

Odd thing happening to me that I'm sure is me overlooking something small. Just doing a simple boundary test to stop player from leaving sides of stage. I've done this before in other little test games but something funky is happening this time.

The player (which is just a box now as a placeholder) is managing to get a few frames outside of stage before bouncing back after letting go of the movement key.

package
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	
	public class Main extends MovieClip
	{
		var vx:int;
		var playerHalfWidth:uint;

		
		public function Main()
		{
			init();
		}
		function init():void
		{
			//Initialize variables
			vx = 0;
			playerHalfWidth = player.width / 2;

			//Add event Listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.LEFT)
			{
				vx = -5;
			}
			else if (event.keyCode == Keyboard.D || event.keyCode == Keyboard.RIGHT)
			{
				vx = 5;
			}
		}
		function onKeyUp(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D)
			{
				vx = 0;
			}
			if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
			{
				vx = 0;
			}
		}
		function onEnterFrame(event:Event):void
		{
			//Stop player at edge of stage
			if (player.x + playerHalfWidth > stage.stageWidth)
			{
				player.x = stage.stageWidth - playerHalfWidth;
			}
			else if (player.x - playerHalfWidth < 0)
			{
				player.x = 0 + playerHalfWidth
			}
			
			//Move the player
				player.x += vx;
		}
	}
}

All of this is just inside of the Document Class. Just move left or right with Arrows or A/D. Move to the edge and you will see box nearly stop at edge but when letting go of movement key the player slightly bounces back into stage.

Here is test:
<a>
http://www.newgrounds.com/dump/item/c3b9dff5a781f03eff01ed5604c71ab8
</a)

What am I missing?

Thanks in advance guys


BBS Signature
Aka-Star
Aka-Star
  • Member since: Nov. 14, 2003
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Quick Q: Stage boundary test 2013-08-21 01:59:19 Reply

the problem is your moving the player after you've set it the edge of the bounds, you want to set the player.x +=vx before you do the boundary test otherwise you might move it to 0-half Width and then +5 it.

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to Quick Q: Stage boundary test 2013-08-21 02:24:43 Reply

At 8/21/13 01:59 AM, Aka-Star wrote: you want to set the player.x +=vx before you do the boundary test

Ahhh thank you dude. I knew it was something simple. I will have to really start paying more attention to the order of things in my coding.

Really appreciate the help! :D


BBS Signature