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
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");
}
//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
im not sure what you mean, sorry...within the terrymc, there is two positions, the first one is called stand and the second stand_forward....if you double-click into any of them movieclips placed in the keyframe, there will be a animation within that mc
// 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;
if (player_mc.currentLabel!="stand_forward") {
player_mc.gotoAndStop("stand_forward");
}
}
// if(right is true and left is not true)
if (right) {
player_mc.x+=speed;
player_mc.scaleX=1;
if (player_mc.currentLabel!="stand_forward") {
player_mc.gotoAndStop("stand_forward");
}
}
// if(up is true and down is not true)
if (up) {
player_mc.y-=speed;
if (player_mc.currentLabel!="stand_forward") {
player_mc.gotoAndStop("stand_forward");
}
}
// if(down is true and up is not true)
if (down) {
player_mc.y+=speed;
if (player_mc.currentLabel!="stand_forward") {
player_mc.gotoAndStop("stand_forward");
}
}
if (! left&&! right&&! up&&! down) {
if (player_mc.currentLabel!="stand") {
player_mc.gotoAndStop("stand");
}
}
it works!, thank you soooo much!! ...back to my other point, is there a easier way of implementing this...also how would i go around making a double key press feature e.g. the player presses right arrow twice and the character runs
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 var bSpeedVal:int=5;
private var bSpeed:int=0;
private var fLabel:String;
private var kLast:int=0;
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) {
player_mc.x-=speed;
player_mc.scaleX=-1;
fLabel="stand_forward";
} else if (right) {
player_mc.x+=(speed + bSpeed);
player_mc.scaleX=1;
// need to add frame label and movie clip for 'run_forward'
//fLabel= (bSpeed == 0) ? "stand_forward" : "run_forward";
fLabel="stand_forward";
}
if (up) {
player_mc.y-=speed;
fLabel="stand_forward";
} else if (down) {
player_mc.y+=speed;
fLabel="stand_forward";
}
if (! left&&! right&&! up&&! down) {
fLabel="stand";
}
if (player_mc.currentLabel!=fLabel) {
player_mc.gotoAndStop(fLabel);
}
}
//this is the function the KeyboardEvent.KEY_DOWN listener uses
public function OnPress(event:KeyboardEvent):void {
bSpeed=0;
switch ( event.keyCode ) {
case Keyboard.UP :
up=true;
break;
case Keyboard.DOWN :
down=true;
break;
case Keyboard.LEFT :
left=true;
break;
case Keyboard.RIGHT :
bSpeed = (kLast == Keyboard.RIGHT) ? bSpeedVal : 0;
right=true;
break;
default :
break;
}
}
//this is the function the KeyboardEvent.KEY_UP listener uses
public function OnRelease(event:KeyboardEvent):void {
kLast=event.keyCode;
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
would it be more better to use a timer?, e.g. you the user presses the right key and the timer checks to see if the key is pressed within say 2 seconds or sumthing, if this condition is true then the player runs...thank you yet again dawsonk, however when i press the right key, then stop and then press the right key, the player is still running