Player moves diagonally and not straight. ???
Provided below is the code for my class.
It all works fine for controlling the player instance when I don't COMMENT the onKeysUp.
I wanted to create a constantly moving character so I removed the onKeysUp. The first time you press a direction it moves straight, but then it moves only diagonally.
I notice that this little guy could be causing me my newborn headache:
function onEntersFrame(event:Event):void
{
//Move the player
player.x += vx;
player.y += vy;
}
Any ideas of changing this do stop the diagonal mumbo jumbo?
(((((((((((FULL CODE BELOW)))))))))))))
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Main_Character_Two extends MovieClip
{
var vx:int;
var vy:int;
public function Main_Character_Two()
{
init();
}
function init():void
{
//initialize variables
vx = 0;
vy = 0;
//Add event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeysDown);
/*stage.addEventListener(KeyboardEvent.KEY_UP, onKeysUp);*/
addEventListener(Event.ENTER_FRAME, onEntersFrame);
}
function onKeysDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{ // v stands for velocity whichi sbetter than uising speed.
vx = -5; // vx is the velocity of the direction x
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
/*function onKeysUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
{
vy = 0;
}
}*/
function onEntersFrame(event:Event):void
{
//Move the player
player.x += vx;
player.y += vy;
}
}
}
