Domanda

What is the difference between the constructor of a BasicGameState and the init method? It appears that they are both called at the same time when the BasicGameState is registered in StateBasedGame, so what makes them different?

A follow up question to this is how do I run a method only when a state becomes active? Initially I thought the init method was only called when a new state becomes active, but this is not true. Note that I'd like to call this method only once at the beginning of the new state, so putting it in the update method seems like the wrong place.

È stato utile?

Soluzione

As for the first question, I generally initialize all my java objects in the constructor (other game objects, collections, sockets etc.) and the init() method loads images, sounds and other resources.

And the 2nd question, there is a method called enter() which is called upon entering the GameState. Also there is a leave() method which is called when leaving the state.

More on the GameState is found here http://slick.ninjacave.com/javadoc/org/newdawn/slick/state/GameState.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top