Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsOdd 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
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.
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