Learn how to work with finite states machines inside cocos2d. Here is a sample project which you can download. Before watching this tutorial you might want to watch: Singleton secrets.
Thanks
Learn how to work with finite states machines inside cocos2d. Here is a sample project which you can download. Before watching this tutorial you might want to watch: Singleton secrets.
Thanks
You must be logged in to post a comment.
Hi Bob,
I’ve implemented your simple state machine and it’s working great. I now want to display the NSLog messages as character “speech” text on screen (like a speech bubble).
Your recommendation for how to do this?
Hey Bob, this is an awesome tutorial and gives me a lot to think about. One thing I am having trouble figuring out is how to implement transitions between States.
In a real game situation, the dwarf would have to actually leave the mine and that would take some time before you could switch to the next State. Another example would be a character has a Jump State and he needs to transition to the Fall State. Another might be a character is in a Running State and then wants to Throw a Punch State or Take Damage State. Any clues to implement this?
My idea was to have transition states, but this seems like it would add up. Start Jump State, Jump State, Fall State, Land State… This seems to build up way too fast and become way too complicated.
I was having issues getting Dopey up and running. I had three errors (all related) revolving around the Dwarf class. First was a lack of CCNode interface. Second was using super on Dwarf class. I tracked down the issues. The Defs.h file was not defined (or I missed it, very possible) in the tutorial. All it did was include cocos2d.h. Why the extra step?
Hi Jim,
Yes Defs.h is an unnecessary step so you do not really need it. Thank you for pointing it out. Take it away if you want to. CCNode is a part of cocos2d library. What exactly is missing? Could you also point out where the use of super in Dwarf class is causing the problem.
Thanks Bob
I had also some problems and here is my solution, maybe s. o. gets also an error and can fix it.
1. You have to change this in the Dwarf.m file like Truth Seeker said before :
self changeState:[Mine shared]]; // was: self.currentState=[Mine shared];
2. You have also to keep in mind, that you only #import the Files in the .h files of Bank, Home and Mine that you really need to compile the project. Files only using in the .m files to change the state you have to #import before the @implementation, otherwise you will have an not ending loop.
Bob, thnx for another great tutorial!
I’ve learned how to code with your videos, not just objective-c but coding in general,
so thank you..
Thank you Bob! That is a great idea indeed.
Playing with the code I discovered that the initialization of the Dwarf object
would be more precise in the following way:
-(id)initWithName:(NSString *)aName
{
if( (self=[super init]) ) {
diamonds=0;
money=0;
self.name=aName;
// To ensure full execution of the state:
[self changeState:[Mine shared]]; // was: self.currentState=[Mine shared];
}
return self;
}
Bob this is great tutorial!
As demonstrated:
1. “dopey” and “sneeze” live a parallel life. (Dopey lives faster since it has a faster “clock”).
2. They do not know about each other. They are self contained.
How to make them interact with each other?
Hi Truth Seeker,
You can use NSNotificationCenter to get the characters to communicate with each other. Here is a sample from a test I performed. The new rule is that the mine must not be empty. So when one dwarf is away the other must stay in the mine. Here’s a run with three dwarfs.
http://bobueland.com/cocos2d/images/log2.jpg
I also made a run with four dwarfs and included Snow White. Snow White can be in threee states. Making beds, resting or preparing blueberry pie for the dwarfs. I also made the dwarfs spend their money while livin’ the good life, so that they eventually must return to the mine. Here’s a run from the test.
Dopey: Pickin’ up diamond 1
Sneezy: Pickin’ up diamond 1
Doc: Pickin’ up diamond 1
Dopey: Pickin’ up diamond 2
Snow White: Making a bed. 7 beds left
Grumpy: Pickin’ up diamond 1
Sneezy: Pickin’ up diamond 2
Dopey: Pickin’ up diamond 3
Dopey: I must go away Sneezy
Sneezy: Seya Dopey
Dopey: I must go away Grumpy
Grumpy: Seya Dopey
Dopey: I must go away Doc
Doc: Seya Dopey
Dopey: Walkin’ to the bank with mah pockets full o’ precious stones
Doc: Pickin’ up diamond 2
Sneezy: Pickin’ up diamond 3
Sneezy: I must go away Grumpy
Grumpy: Seya Sneezy
Sneezy: I must go away Doc
Doc: Seya Sneezy
Sneezy: Walkin’ to the bank with mah pockets full o’ precious stones
Dopey: I have 3000$ in the bank. Huzzah!
Dopey: Walkin’ to the diamond mine
Dopey: Heya Grumpy
Grumpy: Ah’m so glad you’re back Dopey
Dopey: Heya Doc
Doc: Ah’m so glad you’re back Dopey
Snow White: Making a bed. 6 beds left
Grumpy: Pickin’ up diamond 2
Doc: Pickin’ up diamond 3
Doc: I must go away Dopey
Dopey: Seya Doc
Doc: I must go away Grumpy
Grumpy: Seya Doc
Doc: Walkin’ to the bank with mah pockets full o’ precious stones
Sneezy: I have 3000$ in the bank. Huzzah!
Sneezy: Walkin’ to the diamond mine
Sneezy: Heya Dopey
Dopey: Ah’m so glad you’re back Sneezy
Sneezy: Heya Grumpy
Grumpy: Ah’m so glad you’re back Sneezy
Dopey: Pickin’ up diamond 1
Sneezy: Pickin’ up diamond 1
Dopey: Pickin’ up diamond 2
Doc: I have 3000$ in the bank. Huzzah!
Doc: Walkin’ to the diamond mine
Doc: Heya Dopey
Dopey: Ah’m so glad you’re back Doc
Doc: Heya Sneezy
Sneezy: Ah’m so glad you’re back Doc
Doc: Heya Grumpy
Grumpy: Ah’m so glad you’re back Doc
Snow White: Making a bed. 5 beds left
Dopey: Pickin’ up diamond 3
Dopey: I must go away Sneezy
Sneezy: Seya Dopey
Dopey: I must go away Grumpy
Grumpy: Seya Dopey
Dopey: I must go away Doc
Doc: Seya Dopey
Dopey: Walkin’ to the bank with mah pockets full o’ precious stones
Grumpy: Pickin’ up diamond 3
Grumpy: I must go away Sneezy
Sneezy: Seya Grumpy
Grumpy: I must go away Doc
Doc: Seya Grumpy
Grumpy: Walkin’ to the bank with mah pockets full o’ precious stones
Sneezy: Pickin’ up diamond 2
Doc: Pickin’ up diamond 1
Dopey: I have 6000$ in the bank. Huzzah!
Dopey: Ah’m filthy rich. Time to go home.
Dopey: Hi snow white. Is there any blueberry pie left?
Snow White: All’s gon’ Dopey, but I’ll make some right away
Snow White: This stove is awfully hot
Sneezy: Pickin’ up diamond 3
Sneezy: I must go away Doc
Doc: Seya Sneezy
Sneezy: Walkin’ to the bank with mah pockets full o’ precious stones
Dopey: Liv’n the good life. Yes siree. 5000$ left in my bank account
Snow White: Making a bed. 4 beds left
Doc: Pickin’ up diamond 2
Grumpy: I have 3000$ in the bank. Huzzah!
Grumpy: Walkin’ to the diamond mine
Sneezy: I have 6000$ in the bank. Huzzah!
Sneezy: Ah’m filthy rich. Time to go home.
Sneezy: Hi snow white. Is there any blueberry pie left?
Snow White: The pie will soon be ready Sneezy
Grumpy: Heya Doc
Doc: Ah’m so glad you’re back Grumpy
Dopey: Liv’n the good life. Yes siree. 4000$ left in my bank account
Snow White: The pie is ready. Come and get a piece
Dopey: Thank’s Snow White. It’s delicious
Only 6 blueberry pieces left
Sneezy: Thank’s Snow White. It’s delicious
Only 5 blueberry pieces left
Sneezy: Liv’n the good life. Yes siree. 5000$ left in my bank account
Dopey: Liv’n the good life. Yes siree. 3000$ left in my bank account
Doc: Pickin’ up diamond 3
Doc: I must go away Grumpy
Grumpy: Seya Doc
Doc: Walkin’ to the bank with mah pockets full o’ precious stones
Snow White: Making a bed. 3 beds left
Grumpy: Pickin’ up diamond 1
Sneezy: Liv’n the good life. Yes siree. 4000$ left in my bank account
Dopey: Liv’n the good life. Yes siree. 2000$ left in my bank account
Doc: I have 6000$ in the bank. Huzzah!
Doc: Ah’m filthy rich. Time to go home.
Doc: Hi snow white. Is there any blueberry pie left?
Snow White: Sure is Doc. Here’s a piece
Doc: Thank’s Snow White. It’s delicious
Only 4 blueberry pieces left
Dopey: Liv’n the good life. Yes siree. 1000$ left in my bank account
Sneezy: Liv’n the good life. Yes siree. 3000$ left in my bank account
Snow White: Making a bed. 2 beds left
Dopey: Liv’n the good life. Yes siree. 0$ left in my bank account
Dopey: No mo’ money.
Dopey: Walkin’ to the diamond mine
Dopey: Heya Grumpy
Grumpy: Ah’m so glad you’re back Dopey
Doc: Liv’n the good life. Yes siree. 5000$ left in my bank account
Grumpy: Pickin’ up diamond 2
Sneezy: Liv’n the good life. Yes siree. 2000$ left in my bank account
Dopey: Pickin’ up diamond 1
Sneezy: Liv’n the good life. Yes siree. 1000$ left in my bank account
Doc: Liv’n the good life. Yes siree. 4000$ left in my bank account
Snow White: Making a bed. 1 bed left
Dopey: Pickin’ up diamond 2
Grumpy: Pickin’ up diamond 3
Grumpy: I must go away Dopey
Dopey: Seya Grumpy
Grumpy: Walkin’ to the bank with mah pockets full o’ precious stones
Sneezy: Liv’n the good life. Yes siree. 0$ left in my bank account
Sneezy: No mo’ money.
Sneezy: Walkin’ to the diamond mine
Sneezy: Heya Dopey
Dopey: Ah’m so glad you’re back Sneezy
Dopey: Pickin’ up diamond 3
Dopey: I must go away Sneezy
Sneezy: Seya Dopey
Dopey: Walkin’ to the bank with mah pockets full o’ precious stones
Doc: Liv’n the good life. Yes siree. 3000$ left in my bank account
Sneezy: Pickin’ up diamond 1
Dopey: I have 3000$ in the bank. Huzzah!
Dopey: Walkin’ to the diamond mine
Dopey: Heya Sneezy
Sneezy: Ah’m so glad you’re back Dopey
Snow White: Making a bed. All beds done
Snow White: It’s time to rest
Dopey: Pickin’ up diamond 1
Grumpy: I have 6000$ in the bank. Huzzah!
Grumpy: Ah’m filthy rich. Time to go home.
Grumpy: Hi snow white. Is there any blueberry pie left?
Snow White: Sure is Grumpy. Here’s a piece
Grumpy: Thank’s Snow White. It’s delicious
Only 3 blueberry pieces left
Doc: Liv’n the good life. Yes siree. 2000$ left in my bank account
Sneezy: Pickin’ up diamond 2
Dopey: Pickin’ up diamond 2
Sneezy: Pickin’ up diamond 3
Sneezy: I must go away Dopey
Dopey: Seya Sneezy
Sneezy: Walkin’ to the bank with mah pockets full o’ precious stones
Snow White: Zzz…
Doc: Liv’n the good life. Yes siree. 1000$ left in my bank account
Dopey: Pickin’ up diamond 3
Grumpy: Liv’n the good life. Yes siree. 5000$ left in my bank account
Sneezy: I have 3000$ in the bank. Huzzah!
Sneezy: Walkin’ to the diamond mine
Sneezy: Heya Dopey
Dopey: Ah’m so glad you’re back Sneezy
Dopey: Pickin’ up diamond 4
Dopey: I must go away Sneezy
Sneezy: Seya Dopey
Dopey: Walkin’ to the bank with mah pockets full o’ precious stones
Doc: Liv’n the good life. Yes siree. 0$ left in my bank account
Doc: No mo’ money.
Doc: Walkin’ to the diamond mine
Doc: Heya Sneezy
Sneezy: Ah’m so glad you’re back Doc
Sneezy: Pickin’ up diamond 1
Snow White: Zzz…
Dopey: I have 7000$ in the bank. Huzzah!
Dopey: Ah’m filthy rich. Time to go home.
Dopey: Hi snow white. Is there any blueberry pie left?
Snow White: Sure is Dopey. Here’s a piece
Dopey: Thank’s Snow White. It’s delicious
Only 2 blueberry pieces left
Grumpy: Liv’n the good life. Yes siree. 4000$ left in my bank account
Doc: Pickin’ up diamond 1
Sneezy: Pickin’ up diamond 2
Dopey: Liv’n the good life. Yes siree. 6000$ left in my bank account
Dopey: Liv’n the good life. Yes siree. 5000$ left in my bank account
Snow White: Zzz…
Sneezy: Pickin’ up diamond 3
Sneezy: I must go away Doc
Doc: Seya Sneezy
Sneezy: Walkin’ to the bank with mah pockets full o’ precious stones
Doc: Pickin’ up diamond 2
Dopey: Liv’n the good life. Yes siree. 4000$ left in my bank account
Grumpy: Liv’n the good life. Yes siree. 3000$ left in my bank account
Sneezy: I have 6000$ in the bank. Huzzah!
Sneezy: Ah’m filthy rich. Time to go home.
Sneezy: Hi snow white. Is there any blueberry pie left?
Snow White: Sure is Sneezy. Here’s a piece
Sneezy: Thank’s Snow White. It’s delicious
Only 1 blueberry pie piece left
Dopey: Liv’n the good life. Yes siree. 3000$ left in my bank account
Doc: Pickin’ up diamond 3
Snow White: Zzz…
Snow White: I feel rested
Snow White: It’s time to make beds
Sneezy: Liv’n the good life. Yes siree. 5000$ left in my bank account
Dopey: Liv’n the good life. Yes siree. 2000$ left in my bank account
Grumpy: Liv’n the good life. Yes siree. 2000$ left in my bank account
Doc: Pickin’ up diamond 4
Sneezy: Liv’n the good life. Yes siree. 4000$ left in my bank account
Dopey: Liv’n the good life. Yes siree. 1000$ left in my bank account
Snow White: Making a bed. 7 beds left
Dopey: Liv’n the good life. Yes siree. 0$ left in my bank account
Dopey: No mo’ money.
Dopey: Walkin’ to the diamond mine
Sneezy: Liv’n the good life. Yes siree. 3000$ left in my bank account
Dopey: Heya Doc
Doc: Ah’m so glad you’re back Dopey
Doc: Pickin’ up diamond 5
Doc: I must go away Dopey
Dopey: Seya Doc
Doc: Walkin’ to the bank with mah pockets full o’ precious stones
Dopey: Pickin’ up diamond 1
Grumpy: Liv’n the good life. Yes siree. 1000$ left in my bank account
Sneezy: Liv’n the good life. Yes siree. 2000$ left in my bank account
Doc: I have 5000$ in the bank. Huzzah!
Doc: Walkin’ to the diamond mine
Doc: Heya Dopey
Dopey: Ah’m so glad you’re back Doc
Snow White: Making a bed. 6 beds left
Dopey: Pickin’ up diamond 2
Sneezy: Liv’n the good life. Yes siree. 1000$ left in my bank account
Dopey: Pickin’ up diamond 3
Dopey: I must go away Doc
Doc: Seya Dopey
Dopey: Walkin’ to the bank with mah pockets full o’ precious stones
Grumpy: Liv’n the good life. Yes siree. 0$ left in my bank account
Grumpy: No mo’ money.
Grumpy: Walkin’ to the diamond mine
Doc: Pickin’ up diamond 1
Grumpy: Heya Doc
Doc: Ah’m so glad you’re back Grumpy
Sneezy: Liv’n the good life. Yes siree. 0$ left in my bank account
Sneezy: No mo’ money.
Sneezy: Walkin’ to the diamond mine
Sneezy: Heya Grumpy
Grumpy: Ah’m so glad you’re back Sneezy
Sneezy: Heya Doc
Doc: Ah’m so glad you’re back Sneezy
Dopey: I have 3000$ in the bank. Huzzah!
Dopey: Walkin’ to the diamond mine
Dopey: Heya Sneezy
Sneezy: Ah’m so glad you’re back Dopey
Dopey: Heya Grumpy
Grumpy: Ah’m so glad you’re back Dopey
Dopey: Heya Doc
Doc: Ah’m so glad you’re back Dopey
Snow White: Making a bed. 5 beds left
Sneezy: Pickin’ up diamond 1
Bob that was great suggestion! This is definitely one of the interesting solutions!
I have recreated conversations between dopey, sneezy and grumpy base on your condition that one dwarf has to stay in the mine.
I am posting the code for anyone interested:
http://dl.dropbox.com/u/31198855/fsm.zip
thnx man, I was going to ask bob, how to do the just that!
@emir Enjoy it! Credit for inspiration goes to Bob. I still have to add Snow White to catch up with him
I hope I coded clearly enough. If not ask questions.