Copy link to clipboard
Copied
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;
}
}
}
If you only want to move straight at all times based on the last key pressed, try the following. It is just a quick thought I had based on what you say you want...
Keep that small function and modify the onKeysDown function to add the one line shown in red:
function onKeysDown(event:KeyboardEvent):void
{
vx = vy = 0;
if (event.keyCode == Keyboard.LEFT)
{
...Copy link to clipboard
Copied
Yes, that small function will have you constantly travelling in a diagonal since it changes both x and y at the same time. Without your onKeyUp function you have nothing the stop the progress via making vx/vy = 0.
Copy link to clipboard
Copied
I tried deleting it and then nothing moves. Any ideas how to make it so the player is constantly moving in the direction (STRAIGHT) so that I don't need to hold a key down?
Copy link to clipboard
Copied
If you only want to move straight at all times based on the last key pressed, try the following. It is just a quick thought I had based on what you say you want...
Keep that small function and modify the onKeysDown function to add the one line shown in red:
function onKeysDown(event:KeyboardEvent):void
{
vx = vy = 0;
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;
}
}
Copy link to clipboard
Copied
That wouldn't let me do it... but it got me thinking... and figured it out I added code similar to what is underlined in each else if . That did the trick. No more Diagonal shuffle. Thank you for your help Ned.
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
vy = 0;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
vy = 0;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
vx = 0;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
vx = 0;
}
Copy link to clipboard
Copied
It should have worked. I just tried it and it works fine as I showed. At least the problem is solved for now.
Copy link to clipboard
Copied
hmmm That's odd. I think I did something else that messed it up. I will try that again later. Thanks again. Time to call it a night.
Copy link to clipboard
Copied
AHA! It did work. Thank you
Copy link to clipboard
Copied
You're welcome. I just had a realized that the way you had it might be better because any keypress other than one of the arrow keys will stop the motion - I don't know if that is something you want or not.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now