Jack3.0
12-09-2008, 01:42 PM
This code is for a platform game. When you press left or right for the character to move the animation clip only play once. How do I get it to loop through the animation while pressing one of those buttons?
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var spaceKeyDown:Boolean = false;
var stopper:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = jumpSpeedLimit;
//adding a listener to guy_mc which will make it move
//based on the key strokes that are down
guy_mc.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
guy_mc.x -= mainSpeed;
}
if(rightKeyDown){
guy_mc.x += mainSpeed;
}
if(spaceKeyDown || mainJumping){
mainJump();
}
}
//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, Pressed);
function Pressed(event:KeyboardEvent):void{
//making the booleans true based on the keycode
if(stopper == false){
switch (event.keyCode){
case 39:
rightKeyDown = true;
guy_mc.gotoAndPlay("run");
stopper=true;
break;
case 37:
leftKeyDown = true;
guy_mc.gotoAndPlay("runleft");
stopper=true;
break;
case 40:
downKeyDown = true;
guy_mc.gotoAndPlay("duckright");
break;
case 32:
spaceKeyDown = true;
break;
default:
guy_mc.gotoAndPlay("stand");
}
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, Released);
function Released(event:KeyboardEvent):void{
//making the booleans false based on the keycode
switch (event.keyCode){
case 39:
rightKeyDown = false;
stopper=false;
guy_mc.gotoAndPlay("stand");
break;
case 37:
leftKeyDown = false;
stopper=false;
guy_mc.gotoAndPlay("leftstand");
break;
case 40:
downKeyDown = false;
guy_mc.gotoAndPlay("stand");
break;
case 32:
spaceKeyDown = false;
stopper=false;
break;
}
}
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var spaceKeyDown:Boolean = false;
var stopper:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = jumpSpeedLimit;
//adding a listener to guy_mc which will make it move
//based on the key strokes that are down
guy_mc.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
guy_mc.x -= mainSpeed;
}
if(rightKeyDown){
guy_mc.x += mainSpeed;
}
if(spaceKeyDown || mainJumping){
mainJump();
}
}
//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, Pressed);
function Pressed(event:KeyboardEvent):void{
//making the booleans true based on the keycode
if(stopper == false){
switch (event.keyCode){
case 39:
rightKeyDown = true;
guy_mc.gotoAndPlay("run");
stopper=true;
break;
case 37:
leftKeyDown = true;
guy_mc.gotoAndPlay("runleft");
stopper=true;
break;
case 40:
downKeyDown = true;
guy_mc.gotoAndPlay("duckright");
break;
case 32:
spaceKeyDown = true;
break;
default:
guy_mc.gotoAndPlay("stand");
}
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, Released);
function Released(event:KeyboardEvent):void{
//making the booleans false based on the keycode
switch (event.keyCode){
case 39:
rightKeyDown = false;
stopper=false;
guy_mc.gotoAndPlay("stand");
break;
case 37:
leftKeyDown = false;
stopper=false;
guy_mc.gotoAndPlay("leftstand");
break;
case 40:
downKeyDown = false;
guy_mc.gotoAndPlay("stand");
break;
case 32:
spaceKeyDown = false;
stopper=false;
break;
}
}