KtxGame

open class KtxGame<ScreenType : Screen>(firstScreen: ScreenType? = null, clearScreen: Boolean = true) : KtxApplicationAdapter

An equivalent of com.badlogic.gdx.Game delegating game events to a Screen. On contrary to Game, KtxGame maintains a collection of screens mapped by their type and allows to change screens knowing only their type with setScreen. Thanks to this, its easier to cache screens without maintaining a singleton with all Screen instances manually. ScreenType generic type allows to users to use an extended specific base class (or interface) for all screens, without locking into Screen.

Parameters

firstScreen

will be immediately used by the application. Note that it cannot use any resources initiated by the libGDX (like the OpenGL context) in the constructor, as the screen will be created before the application is launched. Defaults to an empty, mock-up screen implementation that should be replaced with the first setScreen method call in create. Note: firstScreen still has to be explicitly registered with addScreen if you want it to be accessible with getScreen.

clearScreen

if true (the default), clearScreen will be called before screen rendering.

ScreenType

common base interface or class of all screens. Allows to use custom extended Screen API.

See also

Constructors

Link copied to clipboard
constructor(firstScreen: ScreenType? = null, clearScreen: Boolean = true)

Properties

Link copied to clipboard

Provides direct access to current Screen instance handling game events.

Functions

Link copied to clipboard
inline fun <Type : ScreenType> addScreen(screen: Type)

Registers an instance of Screen.

open fun <Type : ScreenType> addScreen(type: Class<Type>, screen: Type)

Registers an instance of Screen. Override this method to change the way screens are registered.

Link copied to clipboard

Checks if screen of the given type is registered.

Link copied to clipboard
open override fun create()

By default, this method resizes (Screen.resize) and shows (Screen.show) initial view. You do not have to call super if you used setScreen in the overridden create method or the selected first view does not need to be resized and showed before usage.

Link copied to clipboard
open override fun dispose()

Disposes of all registered screens with Screen.dispose. Catches thrown errors and logs them with libGDX application API by default. Override onScreenDisposalError method to change error handling behavior. Should be called automatically by libGDX application lifecycle handler.

Link copied to clipboard
inline fun <Type : ScreenType> getScreen(): Type

Returns cached instance of Screen of the selected type.

open fun <Type : ScreenType> getScreen(type: Class<Type>): Type

Returns cached instance of Type of the selected type.

Link copied to clipboard
open override fun pause()
Link copied to clipboard
inline fun <Type : ScreenType> removeScreen(): Type?
open fun <Type : ScreenType> removeScreen(type: Class<Type>): Type?

Removes cached instance of Screen of the selected type. Note that this method does not dispose of the screen and will not affect shownScreen.

Link copied to clipboard
open override fun render()
Link copied to clipboard
open override fun resize(width: Int, height: Int)
Link copied to clipboard
open override fun resume()
Link copied to clipboard
inline fun <Type : ScreenType> setScreen()

Replaces current screen with the registered screen instance of the passed type.

open fun <Type : ScreenType> setScreen(type: Class<Type>)

Replaces current screen with the registered screen instance of the passed type. Calls hide method of the previous screen and show method of the current screen. Override this method to control screen transitions.