Game programming in Xcode and Sprite-builder. Display while computer "thinks"

79 views Asked by At

I am working on an app for a physical boardgames I have produced in the past. Everything is working fine (for now). But the app "freezes up" when the code for the computer "thinking" kicks in.

This is understandable, the computer needs time to make it's turn.

My question is. Is there any way I can simultaneously set an animation to run (like a progress icon), while the computer continues to "think" over it's turn?

Thank You.

2

There are 2 answers

2
Otávio Monteagudo On BEST ANSWER

You can't make something like a progress bar which indicates exactly when the computer will finish 'thinking' exactly because the computer does not know when it will be done.

However, you can create an animation that will run as long as the computer 'thinks'. In your Gameplay file in Spritebuilder, create the animation that you want shown while the user waits. Chain this animation timeline to itself. Let's call the default timeline animation 'default' and the 'computer is thinking' timeline animation 'think'.

Inside the method you use to start the computer's turn, add a line of code that warns cocos2d to run the 'think' animation sequence; it would also be good to make any user interaction with the game impossible at this time (example below is given in Swift):

animationManager.runAnimationsForSequenceNamed("think")
self.userInteractionEnabled = false

Since the 'think' timeline was chained to itself, it will run in an infinite loop, only stopping once cocos2d receives an instruction to break that timeline and start executing another one, so, just inside the method you execute once the computer's turn is over, add these two lines in order to stop the 'think' animation from running and to enable user interaction once again:

animationManager.runAnimationsForSequenceNamed("default")
self.userInteractionEnabled = true
1
Kristof On

Not knowing where you are building your actual game in, using different threads is the way to go. What are you building your game in? Which language, framework, platform?