State Transitions

Subscribe to State Transitions 20 posts

avatar for FlashGrenade FlashGrenade 244 posts
Flag Post

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();	

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

That’s an impressive state machine. I can’t pick out anything wrong at a glance, but as a tip, I wouldn’t debug something like that with trace statements. Try setting a breakpoint where things should be happening and figuring out what isn’t quite right with the debugger.

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post
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) 

what exactly are you expecting this code to do? From looking at the variable names it looks to me like you are setting the speed at which your character should move in the yAxis 9 times(without actually moving it) then performing a while loop(which will never trigger its inner logic as your character isn’t actually moving)

There will likely be an update function in the actor class that needs to be overridden in your hero class to perform the actual movement.

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post

What is “arguments;”?

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post

had to pop in arguments to prevent an error

and there is an updateLocation function in my actor class otherwise i wouldnt be able to move.

the for loop makes you jump in the air until the jumpcounter hits 9 (11 if you jump as you run) and the while loop lets gravity kick in.

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post
Originally posted by FlashGrenade:

had to pop in arguments to prevent an error

Which error?

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post
Originally posted by FlashGrenade:

had to pop in arguments to prevent an error

and there is an updateLocation function in my actor class otherwise i wouldnt be able to move.

the for loop makes you jump in the air until the jumpcounter hits 9 (11 if you jump as you run) and the while loop lets gravity kick in.

This is what i’m saying……super.yVelo = -speed; for example does not by itself move the object on the y axis it just changes the speed at which the updateLocation function should move the object in the y axis each frame.

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post
Originally posted by NineFiveThree:
Originally posted by FlashGrenade:

had to pop in arguments to prevent an error

Which error?

ArgumentError: Error #1063: Argument count mismatch on Hero/standStill(). Expected 0, got 1.

when i looked this problem up on the interwebs the main answer was to type in arguments. If anyone knows the answer to this problem, please let me know.

now this is what the trace turns up with arguments typed in.

[StateMachine] null State Changed to idle
[StateMachine] null State Changed to walking

but mostly this
[StateMachine] null Transition to idle denied
[StateMachine] null Transition to walking denied

the buttons work but i dont think im supposed to have null in there

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

The error means that you have function standStill():void{} and you’re calling standStill(arg);

E: The null is because it tries to trace some id property of heroSM which is never defined. I don’t understand its purpose; it’s declared at the start of the class and it’s never used, except for those traces.

E2: i think you’re getting the error because of parentState.exit.call(null,_exitCallbackEvent);

As you see, it passes stuff to the function.

Conclusion: Stop using that crap.

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post

which crap? the statemachine or the arguements?

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

The StateMachine.

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post

but how am i supposed to get my character to move and jump and hit stuff?

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

By using a state machine :P

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

By making your own system?

Moving an object must be one of the must simple tasks, I don’t get why you’re using an external library for such simple things to start off.

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post
Originally posted by Senekis93:

By making your own system?

Moving an object must be one of the must simple tasks, I don’t get why you’re using an external library for such simple things to start off.

moving an object may be childs play for you, but a guy trying to learn on his own is going to twist in the wind for a while until he gets the help he needs.

without the statemachine im going to be using a whole lot of booleans and probably a bunch of if statements – again.

 
avatar for Aesica Aesica 951 posts
Flag Post

What better way to learn than to make it yourself? Doing it on your own has the added benefit of helping you learn how it works while being built to your exact specifications. Using someone else’s system is more like plugging it in, turning it on, and hoping that the witchcraft works.

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

Note to self: Write my own swf compiler or I’m not doing it right.

Also never use libraries.

This thread.

Favorite ever.

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

You won’t learn much by downloading someone else’s code and using it as the core of your game.

In my opinion, external libraries, frameworks or whatever are used when you already understand what needs to be done and how to do it. Then sometimes you’d rather grab some files and write a couple of lines instead of wasting time writing your own, especially when it’s not a central part of your application (tweens, collisions, keyboard input, etc.).

On the other hand, if your application is about moving around, jumping and killling bad guys, I don’t think you’ll learn a thing if you build it around someone else’s way to make the player jump, kick or walk.

If you want to keep using it and actually learn, you should at least read and understand what it does and why, and then ask yourself if that’s the right tool. That alone would allow you to fix the issues that you’re having.

E: Ninja’d 7 minutes ago… that happens when you start typing and then go do something else before finishing your post. >_>

 
avatar for FlashGrenade FlashGrenade 244 posts
Flag Post
Originally posted by Senekis93:

The error means that you have function standStill():void{} and you’re calling standStill(arg);

i changed the functions and now i don’t need arguments. thanks sen.

and yes i got my character to move and jump before without statemachines.

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post

The issue is that you have misunderstood the purpose of xVelo + yVelo. The updateLocation function in the actor class will be linked to the onEnterFrame event. In that function it updates the position of the object by adding the value of xVelo to x and yVelo to y.

In your code before the updateLocation function is ever called you:

1. set the value of yVelo to -speed 9 times.
2. run a while loop that runs until the object is at the same location in y as it was before the jump started(which it will be because updateLocation has not been called yet)
3. reset the value of yVelo to 0.

So….

regardless of the fact you have a for loop and a while loop; by the time the code that actually moves objects(the code in the updateLocation function) is called the value of yVelo is 0 and therefore the object does not move on the y axis. The same will be true in respect to xVelo + no movement in the x axis when attempting to run and jump.

The quickest way for you to get the jump working(though not the best + not as well designed as the rest of the code) is to simply replace

super.yVelo = -speed; and super.yVelo = speed;

with

this.y -= speed; and this.y += speed;

then in addition to these in the running jump change

super.xVelo = (speed * this.scaleX);

to

this.x += speed * this.scaleX;

I would definately recommend you invest a little more time figuring out what these classes you are “Borrowing” actually do though.