3
\$\begingroup\$

I've created a SpriteKit Mac OS X app and I am using a simple finite state machine to manage the game states. I've seen some solutions, especially for C++ games, where a GameState class and a GameStateManager class are used to regulate states of the game through the use of a stack of GameState objects. What I don't understand is what these GameStates objects would be in SpriteKit. Would they be separate SKScene nodes? How would I implement this into a SpriteKit game?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

iOS 9 actually introduces a GameplayKit framework that includes State objects. This link shows you the implementation of their GKState class. It inherits from NSObject, but not from any SpriteKit node class, or any SpriteKit class at all.

The thing to remember is that SpriteKit is all about drawing and physics simulation. SpriteKit tends to confuse the logic and rendering of objects, but nothing outside of physics or rendering is inherent to the framework. Nodes are great for anything in a scene that needs a position, or a physics body, or somehow organizes rendered or physics objects. States, however, are purely logical, do not need positions or velocities or physics, and can therefore be purely separate from SpriteKit, SKNodes, and SKScenes.

\$\endgroup\$
6
  • \$\begingroup\$ I literally was just watching that video when you posted this. So each SKNode or SKScene would have an instance of GKState and the methods associated with changing that state then? For instance, a menu scene would have a GKState object. In the update method of the scene, this is where the state for the GKState object would be altered. Is this the general idea? \$\endgroup\$
    – 02fentym
    Commented Jun 13, 2015 at 6:03
  • \$\begingroup\$ developer.apple.com/library/prerelease/ios/documentation/… \$\endgroup\$
    – Attackfarm
    Commented Jun 13, 2015 at 8:17
  • \$\begingroup\$ It sounds like that's pretty much it, except that each would have a GKStateMachine which holds the possible GKStates. If you want to design your own, it doesn't have to be exactly like this, but this does show that the States can be their own encapsulated paradigm separate from SpriteKit \$\endgroup\$
    – Attackfarm
    Commented Jun 13, 2015 at 8:21
  • \$\begingroup\$ Yeah, I don't think I'll be able to follow theirs exactly...it's quite complex! Should I be using a stack to control which state the state machine is in or should I just set a variable that holds the current state? \$\endgroup\$
    – 02fentym
    Commented Jun 13, 2015 at 17:16
  • \$\begingroup\$ I don't know (not sure why the stack moght be necessary?), but you'll need some way to tell the machine which state can move to which state. You could store that in the state, or the machine, or the data structure to hold the states, probably depending on the specifics of what you're gunning for. Or maybe it doesn't matter,and you can choose any of those three. Unsure at the moment \$\endgroup\$
    – Attackfarm
    Commented Jun 13, 2015 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .