as3 how to make movement smoother? - actionscript-3

I have a WASD key movement for keydown. I want it to play an animation always when walking and always move when key is pressed.
The issue is, if I press multiple keys at once, it stops moving or stops the animation. Making it look stiff and unsmooth.
private function onKeyDown(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A)
{
vx = -5;
play();
}
else if (event.keyCode == Keyboard.D)
{
vx = 5;
play();
}
else if (event.keyCode == Keyboard.W)
{
vy = -5;
play();
}
else if (event.keyCode == Keyboard.S)
{
vy = 5;
play();
}
}

code you are using right now means if keyboard is pressed, onKeyDown will be triggered and check if it key A or D or W or S is pressed and it only handles one of them because of else if
try change your code like this
private function onKeyDown(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A)
{
vx = -5;
play();
}
if (event.keyCode == Keyboard.D)
{
vx = 5;
play();
}
if (event.keyCode == Keyboard.W)
{
vy = -5;
play();
}
if (event.keyCode == Keyboard.S)
{
vy = 5;
play();
}
}
if this doesn't solve your problem, maybe it is something on your play() function

Related

Trying to add keyboard events to a child movieclip I call to the stage in code

So this is my first semester in school coding and right now I'm trying to make a little 2D game with a character who I need to move up and down.
So far, I've been able to create a titlescreen and then when you click start it goes to the next screen, where my main character is.
I add him to the stage manually through the code, and then I tried to make him move up down left and right with the arrow keys but he only appears, and does not move.
This is my code so far
package lib.fly
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class FlyGame extends MovieClip
{
public var mainCharacter:MovieClip;
public var vx:Number;
public var vy:Number;
public function FlyGame()
{
trace ("Initiate");
init();
}
private function init():void
{
vx = 0;
vy = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
stage.addEventListener(KeyboardEvent.KEY_UP, dontMove);
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
trace ("Keyboard Event Listeners");
}
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = - 5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
private function dontMove(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
vy = 0;
}
}
public function onEnterFrame(event:Event):void
{
mainCharacter = new BoyFlying();
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.x += vx;
mainCharacter.y += vy;
addChild(mainCharacter);
}
}
}
The output produces the trace statements up until my "Actual Keyboard Events"
Sorry, I'm brand new to this so any help would be appreciated. Thank you for your time
Try this logic below and see if it helps your program to work as expected...
Adjust function init()
private function init():void
{
vx = vy = 0; //chain them since same value
mainCharacter = new BoyFlying(); //create once here and control in other functions
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.addEventListener(Event.ENTER_FRAME, onEnterFrame);
addChild(mainCharacter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
//stage.addEventListener(KeyboardEvent.KEY_UP, dontMove); //what is it good for?
trace ("added... Keyboard Event Listeners");
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
}
Adjust function moveAround()
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{ vx -= 5; }
if (event.keyCode == Keyboard.RIGHT)
{ vx += 5; }
if (event.keyCode == Keyboard.UP)
{ vy -= 5; }
if (event.keyCode == Keyboard.DOWN)
{ vy += 5; }
}
Adjust function onEnterFrame()
public function onEnterFrame(event:Event):void
{
//# no need for += here since function moveAround() changes these vx and vy values on key press
//# infact your character could be moved (x/y) just by keyboard event instead of per FPS screen update (eg: not with EnterFrame)
mainCharacter.x = vx;
mainCharacter.y = vy;
}

Collision code stops working after adding to stage

So I'm using actionscript 3, and my collision code stops working after adding it to stage with code. the player just cant move at all when touching the wall. However if I add it to stage by dragging the movieclip to the stage, it works fine, the player doesn't freeze when touching the wall, instead it cant pass the wall but it can move back from it.
this is my code that adds it to stage.
public class Main extends MovieClip
{
var mountains: Mountains;
var homePage: HomePage;
var oneManager: OneManager;
public function Main()
{
mountains = new Mountains;
homePage = new HomePage;
oneManager = new OneManager;
addChild(homePage);
stage.displayState = "fullScreen";
homePage.playButtons.addEventListener(MouseEvent.CLICK, onPlayButtonsClick);
}
function onPlayButtonsClick(event:MouseEvent):void
{
stage.focus = stage;
parent.addChild(oneManager);
homePage.parent.removeChild(homePage);
}
}
this is my code that uses the collision detection to stop the player from crossing the wall
private function onKeyDown(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A)
{
vx = -5;
}
else if (event.keyCode == Keyboard.D)
{
vx = 5;
}
else if (event.keyCode == Keyboard.W)
{
vy = -5;
}
else if (event.keyCode == Keyboard.S)
{
vy = 5;
}
}
private function onKeyUp(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D)
{
vx = 0;
}
else if (event.keyCode == Keyboard.S || event.keyCode == Keyboard.W)
{
vy = 0;
}
}
private function onEnterFrame(event: Event): void
{
if (player.collisionArea.hitTestObject(wall))
{
player.y -= vy;
}
}

Simultaneous keypress causing unwanted behaviour

I am making a shooter game with action script 3. I have a hero which moves when any of the arrow key is pressed
my code is as following
//some class level variables
private var vx :int = 0;
private var vy :int = 0;
//in the Main constructor
stage.addEventListener(KeyboardEvent.KEY_DOWN , moveHero);
stage.addEventListener(KeyboardEvent.KEY_UP , stopHero);
stage.addEventListener(Event.ENTER_FRAME , onEnter);
//and all the handlers
function moveHero(e:KeyboardEvent)
{
if (e.keyCode == 37)
{
vx = -5;
}
else if (e.keyCode == 38)
{
vy = -10;
}
else if (e.keyCode == 39)
{
vx = 5;
}
else if (e.keyCode == 40)
{
vy = 10;
}
}
function stopHero(e:KeyboardEvent)
{
//when key is up stop miving the hero
vx = 0;
vy = 0;
}
function onEnter(e:Event):void
{
//updtae hero position
hero.x += vx;
hero.y += vy;
}
Now my problem is when user have both up and down keys or left and right keys under his fingers and suddenly press them alternately then the hero shows a notable lag in making response to the key presses
You'll have to do exactly like BotMaster said in the comments, and here's how it looks like in AS3 code:
var keys:Array = new Array(255);
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP , keyUp);
stage.addEventListener(Event.ENTER_FRAME , loop);
function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
function loop(e:Event):void {
if(keys[Keyboard.LEFT]) {
trace("Moving left...");
} else if(keys[Keyboard.RIGHT]) {
trace("Moving right...");
}
}

AS3.0 if/else if evaluation behaviour for velocity control

Sorry if this is obvious to others, but I can't get my head around something in ActionScript 3.0 (huge n00b btw)
I have this code for controlling velocity:
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
When run, if I hold both LEFT and UP the Sprite moves Diagonally, but the fact that the two last conditionals (Keyboard.UP & Keyboard.DOWN) are elseifs should prevent them from being evaluated at all shouldnt it?
Is anyone able to shed some light on the behaviour?
When you press both buttons flash fires two independent events for each button. If you want to skip this case, you can make state flags (leftPressed, rightPressed, etc) for each button, change state in key handler and call the check motion method according to current states of each button.
If you don't want diagonal movement, set the velocity components to zero first like this:
public function keyDownHandler(event:KeyboardEvent):void
{
vx = vy = 0; ////
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}

Some Problems with my AS3 code

Some troubles to make my new flash game(i used AS3,CS5.5):
I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground.
And at last this thing here:
"Scene 1, Layer 'hatt', Frame 1, Line 6 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler)."
Plz help me and give some hints plz... Thanks.
Here's the code:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
function update(evt:Event):void
{
if (jumping)
{
hatt.y += jumpPow;
jumpPow += grav;
if (hatt.y >= stage.stageHeight)
{
jumping = false;
hatt.y = stage.stageHeight;
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
function myFunction (event: KeyboardEvent){
if(event.keyCode == Keyboard.LEFT) {
hatt.x -= 5
}
if(event.keyCode == Keyboard.RIGHT) {
hatt.x += 5
}
}
remove this line:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
remove function onKeyDown:
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
replace myFunction with this:
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
hatt.x -= 5;
if(event.keyCode == Keyboard.RIGHT)
hatt.x += 5;
if(event.keyCode == Keyboard.UP && jumping != true)
{
jumpPow = -25;
jumping = true;
}
}