So I’m working with a finite state machine created by cassiozen which is very easy to work with, but i’m running into problems completing my controls.
I have my wsad movement complete and i can double tap to run, but i cant jump or attack, even though my trace says the keys have been pressed. Can anybody please give me some advice?
heres the link to cassiozens state machine
https://github.com/cassiozen/AS3-State-Machine/blob/master/stateMachine/StateMachine.as
heres the code.
public class Hero extends Actor {
private var key:KeyObject;
var maxHealth:uint=100;
//var Health:int=maxHealth;
//var maxPower:uint=60;
//var Power:int=0;
var lives:int=2;
var wasWalking:Boolean=false;
var kickCount:int=0;//set these to 0 if the player moves in any direction
var punchCount:int = 0;//set these to 0 if the player moves in any direction
var grabTimer:Timer = new Timer(3000, 1);
var dashDelayTimer:Timer=new Timer(100,1);
var comboCountTimer:Timer=new Timer(2000,1);
var invincible:Boolean = false;
var SlashLOCKED, KickLOCKED, JumpLOCKED:Boolean = false;
var keyLEFT, keyRIGHT, keyUP, keyDOWN:Boolean = false;
var keyPUNCH, keyJUMP, keyKICK:Boolean = false;
//var kneeStrike:uint = 0; //while this is less than 5
var speed:Number=3;
var runspeed:Number = 6;
var HalfW:uint;
var HalfH:uint;
var heroSM:StateMachine;
public function Hero(xv:int, yv:int, stage_01:Stage ) {
this.x = xv;
this.y = yv;
HalfW=this.width/2;
HalfH=this.height/2;
super.stageRef = stage_01;
key = new KeyObject(super.stageRef);
super.zCoordinate=yv;
super.Health = maxHealth;
heroSM = new StateMachine();
heroSM.addState("idle", { enter:standStill, from:"*" } );
heroSM.addState("walking", { enter:walkTo, from:"idle" } );
heroSM.addState("urgent", { enter:spin2win, from:"*" } );
heroSM.addState("damageLight", { enter:damageLight, from:"*" } );
heroSM.addState("damageHeavy", { enter:damageHeavy, from:"*" } );
heroSM.addState("knockDown", { enter:knockDown, from:"*" } );
heroSM.addState("itBURNS", { enter:onFire, from:"*" } );
heroSM.addState("death", { enter:youDIED, from:"*" } );
//heroSM.addState("touchPowerUp", { enter:pickUp, from:"*" } );
heroSM.addState("jumpStraight", { enter:jumpUp, from:"idle" } );
heroSM.addState("jumpWalk", { enter:jumpFunction, from:"walking" } );
heroSM.addState("backFlip", { enter:backFlip, from:"jumpStraight" } );
heroSM.addState("running", { enter:runTo, from:"walking" } );
heroSM.addState("grabEnemy", { enter:grabEnemy, from:"walking" } );
heroSM.addState("throwEnemyA", { enter:throwEnemyA, from:"grabEnemy" } );
heroSM.addState("throwEnemyB", { enter:throwEnemyB, from:"grabEnemy" } );
//heroSM.addState("bashEnemy", { enter:Enemy, from:"grabEnemy" } );
heroSM.addState("flipping", { enter:flipJump, from:"running" } );
heroSM.addState("atkA", { enter:attackPhaseA, from:"idle" } );
heroSM.addState("atkB", { enter:attackPhaseB, from:"idle,walking" } );
heroSM.initialState = "idle";
super.stageRef.addEventListener(Event.ENTER_FRAME, PlayerControl);
//heroSM.addEventListener(StateMachineEvent.TRANSITION_DENIED,transitionDeniedFunction);
//heroSM.addEventListener(StateMachineEvent.TRANSITION_COMPLETE,transitionCompleteFunction);
}//function Hero
public function PlayerControl(Event){
if (this.x+HalfW>stage.stageWidth) {
this.x=stage.stageWidth-HalfW;
} else if (this.x - HalfW < 0) {
this.x=0+HalfW;
}
if (this.y>stage.stageHeight) {
this.y=stage.stageHeight;
this.zCoordinate=stage.stageHeight;
} else if (this.y - this.height< 0) {
this.y=0+this.height;
this.zCoordinate=0+this.height;
}
if (JumpLOCKED && ! key.isDown(32)) {
trace("UNLOCKING THE LOCK FOR SPACE KEY");
JumpLOCKED = false;
keyJUMP=false;
}
if (SlashLOCKED&&! key.isDown(74)) {
trace("UNLOCKING THE LOCK FOR J KEY");
SlashLOCKED = false;
keyPUNCH=false;
}
if (KickLOCKED&&! key.isDown(75)) {
trace("UNLOCKING THE LOCK FOR K KEY");
KickLOCKED=false;
}
if (key.isDown(65)) {keyLEFT = true;}else { keyLEFT = false; }
if (key.isDown(68)) {keyRIGHT= true;}else { keyRIGHT = false; }
if (key.isDown(87)) {keyUP = true;}else {keyUP = false; }
if (key.isDown(83)) {keyDOWN = true;}else {keyDOWN = false; }
if (!(keyLEFT) && !(keyRIGHT) && !(keyUP) && !(keyDOWN)) {
heroSM.changeState("idle");}
else {heroSM.changeState("walking");
}//if (!(keyLEFT) && !(keyRIGHT) && !(keyUP) && !(keyDOWN))
if (key.isDown(32) && !(JumpLOCKED)){
keyJUMP=true;
JumpLOCKED = true;
}
if (key.isDown(74) && !(SlashLOCKED)){
keyPUNCH = true;
SlashLOCKED = true;
}
if (key.isDown(75) && !(KickLOCKED)){
keyKICK = true;
KickLOCKED = true;
}
}//function PlayerControl
public function walkTo():void {
arguments;
//a key
if (! (keyLEFT && keyRIGHT)) {//this ensures that if both keys are pressed there is no conflict
if (keyLEFT) {
trace("A KEY PRESSED");
this.scaleX = -1;
punchCount = 0;
kickCount = 0;
if (!(dashDelayTimer.running)){
trace("Trying to walk");
wasWalking = true;
this.gotoAndStop('walk');
super.xVelo=- speed;
} else {
heroSM.changeState("running");
}//if (!(dashDelayTimer.running))
}//if (key.isDown(65))
else {
if (super.xVelo<0) {
super.xVelo=0; }
}//if (key.isDown(65))
//d key
if (keyRIGHT) {
trace("D KEY PRESSED");
this.scaleX = 1;
punchCount = 0;
kickCount = 0;
if (!(dashDelayTimer.running)){
trace("Trying to walk");
wasWalking = true;
this.gotoAndStop('walk');
super.xVelo=speed;
} else {
heroSM.changeState("running");
}//if (! dashDelayTimer.running||running)
}//if (key.isDown(68))
else {
if (super.xVelo>0) {
super.xVelo=0; }
}//if (key.isDown(68))
}//(! key.isDown(65)&& key.isDown(68))
if (! (key.isDown(87) && key.isDown(83))) {
// w key
if (keyUP) {
trace("W KEY PRESSED");
punchCount = 0;
kickCount = 0;
trace("Trying to walk");
super.yVelo=-speed;
this.gotoAndStop('walk');
}//if (key.isDown(87))
else {
if (super.yVelo<0) {
super.yVelo=0; }
}//if (key.isDown(87))
// s key
if (keyDOWN) {
trace("S KEY PRESSED");
punchCount = 0;
kickCount = 0;
trace("Trying to walk");
super.yVelo=speed;
this.gotoAndStop('walk');
}// if (key.isDown(83))
else {
if (super.yVelo>0) {
super.yVelo=0; }
}//if (key.isDown(83))
if (keyJUMP){heroSM.changeState("jumpWalk");}
if (keyPUNCH){ heroSM.changeState("atkA"); }
if (keyKICK){ heroSM.changeState("atkB"); }
}//(! key.isDown(87) && key.isDown(83))
}// function walkTo
public function runTo():void {
arguments;
trace(this.scaleX);
this.gotoAndStop('run');
super.yVelo = 0;
super.xVelo = (runspeed * this.scaleX);
if (key.isDown(32) && !(JumpLOCKED)) {heroSM.changeState("flipping");}
if (key.isDown(74) && !(SlashLOCKED)){
super.xVelo=0;
this.gotoAndStop('DashA'); }
if (key.isDown(75) && !(KickLOCKED)) {
super.xVelo=0;
this.gotoAndStop('DashB');}
}//function runTo
public function standStill():void {
arguments;
super.xVelo=0;
super.yVelo=0;
trace("I am idling");
this.gotoAndStop('idle');
if (wasWalking) {
wasWalking=false;
dashDelayTimer.start();
}//if (wasWalking)
if (keyJUMP) {heroSM.changeState("jumpStraight");}
if (keyPUNCH){heroSM.changeState("atkA");}
if (keyKICK) { heroSM.changeState("atkB"); }
//if (keyJUMP) { };
}//function standStill
public function jumpUp():void {
arguments;
this.gotoAndStop('jump');
trace("I am jumping from: "+super.zCoordinate);
for (var j:int = 0; j<= 9; j++) {
trace(j);
super.yVelo = -speed;
super.xVelo = 0;
if (key.isDown(74) && !(SlashLOCKED)){
this.gotoAndStop('jumpSlash');}
if (key.isDown(75) && !(KickLOCKED)) {
this.gotoAndStop('jumpKick'); }
if (key.isDown(32) && !(JumpLOCKED)) {
heroSM.changeState("backFlip");}
}//for (var j:int = 0; j<= 9; j++)
trace(this.y);
trace(this.zCoordinate);
while (this.y < this.zCoordinate) {
super.yVelo = speed;
super.xVelo = 0;
if (key.isDown(74) && !(SlashLOCKED)){
this.gotoAndStop('jumpSlash');}
if (key.isDown(75) && !(KickLOCKED)) {
this.gotoAndStop('jumpKick');}
}// while (this.y < this.zCoordinate)
super.xVelo=0;
super.yVelo=0;
trace("I have landed2");
heroSM.changeState("idle");
}//function jumpUp
public function jumpFunction():void {
arguments;
trace(this.scaleX);
for (var j:int = 0; j<= 9; j++) {
trace(j);
trace(this.scaleX);
super.yVelo = -speed;
super.xVelo = (speed * this.scaleX);
if (key.isDown(74) && !(SlashLOCKED)){
this.gotoAndStop('jumpSlash');}
if (key.isDown(75) && !(KickLOCKED)) {
this.gotoAndStop('jumpKick');}
}//for (var j:int = 0; j<= 9; j++)
trace(this.y);
trace(this.zCoordinate);
while (this.y < this.zCoordinate) {
trace(this.scaleX);
super.yVelo = speed;
super.xVelo = (speed * this.scaleX);
if (key.isDown(74) && !(SlashLOCKED)){
this.gotoAndStop('jumpSlash');}
if (key.isDown(75) && !(KickLOCKED)) {
this.gotoAndStop('jumpKick');}
}// while (this.y < this.zCoordinate)
super.xVelo=0;
super.yVelo=0;
trace("I have landed from a walking jump");
heroSM.changeState("idle");
}//function jumpFunction();