PDA

View Full Version : [AS3] Arrgghhh Movement



FMSFS
June 29th, 2010, 07:48 AM
hey, I am new to AS3, and i am trying to get the character to move (yh i know i am a noob), ive got the character to move in all four directions using the arrow keys but i want him to play a animation while his walking for example when i press the right arrow key, he moves to the right while doing a walking animation...currently when he moves he stops at the start of the walking animation frame



package code
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

public class GameEngine extends MovieClip
{

public var up:Boolean = false;
public var down:Boolean = false;
public var left:Boolean = false;
public var right:Boolean = false;
public var speed:Number = 5;

public function GameEngine()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, OnPress);
stage.addEventListener(KeyboardEvent.KEY_UP, OnRelease);
stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
}

public function OnEnterFrame(event:Event):void
{
// Move up, down, left, or right
// if(left is true and right is not true)
if ( left) {
player_mc.x -= speed;
player_mc.scaleX = -1;
player_mc.gotoAndStop("stand_forward");
}
// if(right is true and left is not true)
if( right ) {
player_mc.x += speed;
player_mc.scaleX = 1;
player_mc.gotoAndStop("stand_forward");
}
// if(up is true and down is not true)
if( up ) {
player_mc.y -= speed;
player_mc.gotoAndStop("stand_forward");
}
// if(down is true and up is not true)
if( down) {
player_mc.y += speed;
player_mc.gotoAndStop("stand_forward");
}

if(!left && !right && !up && !down) {
player_mc.gotoAndStop("stand")
}
}

//this is the function the KeyboardEvent.KEY_DOWN listener uses
public function OnPress(event:KeyboardEvent):void
{

switch( event.keyCode )
{
case Keyboard.UP:
up = true;
break;
case Keyboard.DOWN:
down = true;
break;
case Keyboard.LEFT:
left = true;
break;
case Keyboard.RIGHT:
right = true;
break;
default:
break;
}
}
//this is the function the KeyboardEvent.KEY_UP listener uses
public function OnRelease(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
up = false;
break;
case Keyboard.DOWN:
down = false;
break;
case Keyboard.LEFT:
left = false;
break;
case Keyboard.RIGHT:
right = false;
break;
default:
break;
}
}
}
}
//the end


thank you in advance :)

nathan99
June 29th, 2010, 08:12 AM
I think you want gotoAndPlay rather than gotoAndStop

FMSFS
June 29th, 2010, 08:19 AM
ive tried that and im still getting that problem :(

nathan99
June 29th, 2010, 10:02 PM
I'd suggest putting animations on Key frames before telling it to animate. I think you'll find as it is, every frame the code repeatedly tells the character to play the same frame (thus it appears not to be working)