简体   繁体   English

Slick2D:构造函数与初始化

[英]Slick2D: constructor vs. init

What is the difference between the constructor of a BasicGameState and the init method? BasicGameState的构造BasicGameStateinit方法之间有什么区别? It appears that they are both called at the same time when the BasicGameState is registered in StateBasedGame , so what makes them different? 看起来在StateBasedGameBasicGameState中注册时,它们同时被调用了,那么有什么区别呢?

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. 最初,我认为只有在新状态变为活动状态时才调用init方法,但这不是事实。 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. 请注意,我只想在新状态的开头一次调用此方法,因此将其放在update方法中似乎是错误的地方。

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. 对于第一个问题,我通常在构造函数中初始化所有java对象(其他游戏对象,集合,套接字等),并且init()方法加载图像,声音和其他资源。

And the 2nd question, there is a method called enter() which is called upon entering the GameState. 第二个问题是,有一个名为enter()的方法,该方法在进入GameState时被调用。 Also there is a leave() method which is called when leaving the state. 另外,还有一个离开状态时会调用的Leave()方法。

More on the GameState is found here http://slick.ninjacave.com/javadoc/org/newdawn/slick/state/GameState.html 有关GameState的更多信息,请参见http://slick.ninjacave.com/javadoc/org/newdawn/slick/state/GameState.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM