This is the basic game "state" object - e.g. in a simple game you might have a menu state and a play state. It is for all intents and purpose a fancy FlxGroup. And really, it's not even that fancy.

Constructor

new(?MaxSize:Int)

Variables

bgColor:FlxColor

The natural background color the cameras default to. In AARRGGBB format.

destroySubStates:Bool = true

If substates get destroyed when they are closed, setting this to false might reduce state creation time, at greater memory cost.

persistentDraw:Bool = true

Determines whether or not this state is updated even when it is not the active state. For example, if you have your game state first, and then you push a menu state on top of it, if this is set to true, the game state would continue to be drawn behind the pause state. By default this is true, so background states will continue to be drawn behind the current state. If background states are not visible when you have a different state on top, you should set this to false for improved performance.

persistentUpdate:Bool = false

Determines whether or not this state is updated even when it is not the active state. For example, if you have your game state first, and then you push a menu state on top of it, if this is set to true, the game state would continue to update in the background. By default this is false, so background states will be "paused" when they are not active.

read onlysubState:FlxSubState

Current substate. Substates also can be nested.

Methods

closeSubState():Void

Closes the substate of this state, if one exists.

create():Void

This function is called after the game engine successfully switches states. Override this function, NOT the constructor, to initialize or set up your game state. We do NOT recommend overriding the constructor, unless you want some crazy unpredictable things to happen!

onFocus():Void

This method is called after the game receives focus. Can be useful for third party libraries, such as tweening engines.

onFocusLost():Void

This method is called after the game loses focus. Can be useful for third party libraries, such as tweening engines.

onResize(Width:Int, Height:Int):Void

This function is called whenever the window size has been changed.

Parameters:

Width

The new window width

Height

The new window Height

resetSubState():Void

Load substate for this state

switchTo(nextState:FlxState):Bool

Called from FlxG.switchState(). If false is returned, the state switch is cancelled - the default implementation returns true.

Useful for customizing state switches, e.g. for transition effects.

Inherited Variables

Defined by FlxTypedGroup

read onlylength:Int = 0

The number of entries in the members array. For performance and safety you should check this variable instead of members.length unless you really know what you're doing!

maxSize:Int

The maximum capacity of this group. Default is 0, meaning no max capacity, and the group can just grow.

read onlymembers:Array<T>

Array of all the members in this group.

Defined by FlxBasic

ID:Int = -1

IDs seem like they could be pretty useful, huh? They're not actually used for anything yet though.

active:Bool = true

Controls whether update() is automatically called by FlxState/FlxGroup.

alive:Bool = true

Useful state for many game objects - "dead" (!alive) vs alive. kill() and revive() both flip this switch (along with exists, but you can override that).

camera:FlxCamera

Gets ot sets the first camera of this object.

cameras:Array<FlxCamera>

This determines on which FlxCameras this object will be drawn. If it is null / has not been set, it uses FlxCamera.defaultCameras, which is a reference to FlxG.cameras.list (all cameras) by default.

exists:Bool = true

Controls whether update() and draw() are automatically called by FlxState/FlxGroup.

visible:Bool = true

Controls whether draw() is automatically called by FlxState/FlxGroup.

Inherited Methods

Defined by FlxTypedGroup

add(Object:T):T

Adds a new FlxBasic subclass (FlxBasic, FlxSprite, Enemy, etc) to the group. FlxGroup will try to replace a null member of the array first. Failing that, FlxGroup will add it to the end of the member array. WARNING: If the group has a maxSize that has already been met, the object will NOT be added to the group!

Parameters:

Object

The object you want to add to the group.

Returns:

The same FlxBasic object that was passed in.

clear():Void

Remove all instances of FlxBasic subclass (FlxSprite, FlxBlock, etc) from the list. WARNING: does not destroy() or kill() any of these objects!

countDead():Int

Call this function to find out how many members of the group are dead.

Returns:

The number of FlxBasics flagged as dead. Returns -1 if group is empty.

countLiving():Int

Call this function to find out how many members of the group are not dead.

Returns:

The number of FlxBasics flagged as not dead. Returns -1 if group is empty.

forEach(Function:T‑>Void, Recurse:Bool = false):Void

Applies a function to all members

Parameters:

Function

A function that modifies one element at a time

Recurse

Whether or not to apply the function to members of subgroups as well

forEachAlive(Function:T‑>Void, Recurse:Bool = false):Void

Applies a function to all alive members

Parameters:

Function

A function that modifies one element at a time

Recurse

Whether or not to apply the function to members of subgroups as well

forEachDead(Function:T‑>Void, Recurse:Bool = false):Void

Applies a function to all dead members

Parameters:

Function

A function that modifies one element at a time

Recurse

Whether or not to apply the function to members of subgroups as well

forEachExists(Function:T‑>Void, Recurse:Bool = false):Void

Applies a function to all existing members

Parameters:

Function

A function that modifies one element at a time

Recurse

Whether or not to apply the function to members of subgroups as well

forEachOfType<K>(ObjectClass:Class<K>, Function:K‑>Void, Recurse:Bool = false):Void

Applies a function to all members of type Class

Parameters:

ObjectClass

A class that objects will be checked against before Function is applied, ex: FlxSprite

Function

A function that modifies one element at a time

Recurse

Whether or not to apply the function to members of subgroups as well

getFirstAlive():T

Call this function to retrieve the first object with dead == false in the group. This is handy for checking if everything's wiped out, or choosing a squad leader, etc.

Returns:

A FlxBasic currently flagged as not dead.

getFirstAvailable(?ObjectClass:Class<T>, Force:Bool = false):T

Call this function to retrieve the first object with exists == false in the group. This is handy for recycling in general, e.g. respawning enemies.

Parameters:

ObjectClass

An optional parameter that lets you narrow the results to instances of this particular class.

Force

Force the object to be an ObjectClass and not a super class of ObjectClass.

Returns:

A FlxBasic currently flagged as not existing.

getFirstDead():T

Call this function to retrieve the first object with dead == true in the group. This is handy for checking if everything's wiped out, or choosing a squad leader, etc.

Returns:

A FlxBasic currently flagged as dead.

getFirstExisting():T

Call this function to retrieve the first object with exists == true in the group. This is handy for checking if everything's wiped out, or choosing a squad leader, etc.

Returns:

A FlxBasic currently flagged as existing.

getFirstNull():Int

Call this function to retrieve the first index set to 'null'. Returns -1 if no index stores a null object.

Returns:

An Int indicating the first null slot in the group.

getRandom(StartIndex:Int = 0, Length:Int = 0):T

Returns a member at random from the group.

Parameters:

StartIndex

Optional offset off the front of the array. Default value is 0, or the beginning of the array.

Length

Optional restriction on the number of values you want to randomly select from.

Returns:

A FlxBasic from the members list.

insert(position:Int, object:T):T

Inserts a new FlxBasic subclass (FlxBasic, FlxSprite, Enemy, etc) into the group at the specified position. FlxGroup will try to replace a null member at the specified position of the array first. Failing that, FlxGroup will insert it at the position of the member array. WARNING: If the group has a maxSize that has already been met, the object will NOT be inserted to the group!

Parameters:

position

The position in the group where you want to insert the object.

object

The object you want to insert into the group.

Returns:

The same FlxBasic object that was passed in.

inlineiterator(?filter:T‑>Bool):FlxTypedGroupIterator<T>

Iterate through every member

kill():Void

Calls kill on the group's members and then on the group itself. You can revive this group later via revive() after this.

recycle(?ObjectClass:Class<T>, ?ObjectFactory:Void‑>T, Force:Bool = false, Revive:Bool = true):T

Recycling is designed to help you reuse game objects without always re-allocating or "newing" them. It behaves differently depending on whether maxSize equals 0 or is bigger than 0.

maxSize > 0 / "rotating-recycling" (used by FlxEmitter): - at capacity: returns the next object in line, no matter its properties like alive, exists etc. - otherwise: returns a new object.

maxSize == 0 / "grow-style-recycling" - tries to find the first object with exists == false - otherwise: adds a new object to the members array

WARNING: If this function needs to create a new object, and no object class was provided, it will return null instead of a valid object!

Parameters:

ObjectClass

The class type you want to recycle (e.g. FlxSprite, EvilRobot, etc).

ObjectFactory

Optional factory function to create a new object if there aren't any dead members to recycle. If null, Type.createInstance() is used which requires the class to have no constructor parameters.

Force

Force the object to be an ObjectClass and not a super class of ObjectClass.

Revive

Whether recycled members should automatically be revived (by calling revive() on them)

Returns:

A reference to the object that was created.

remove(Object:T, Splice:Bool = false):T

Removes an object from the group.

Parameters:

Object

The FlxBasic you want to remove.

Splice

Whether the object should be cut from the array entirely or not.

Returns:

The removed object.

replace(OldObject:T, NewObject:T):T

Replaces an existing FlxBasic with a new one. Does not do anything and returns null if the old object is not part of the group.

Parameters:

OldObject

The object you want to replace.

NewObject

The new object you want to use instead.

Returns:

The new object.

revive():Void

Calls revive on the group's members and then on the group itself.

inlinesort(Function:Int‑>T‑>T‑>Int, Order:Int = FlxSort.ASCENDING):Void

Call this function to sort the group according to a particular value and order. For example, to sort game objects for Zelda-style overlaps you might call myGroup.sort(FlxSort.byY, FlxSort.ASCENDING) at the bottom of your FlxState.update() override.

Parameters:

Function

The sorting function to use - you can use one of the premade ones in FlxSort or write your own using FlxSort.byValues() as a backend

Order

A FlxGroup constant that defines the sort order. Possible values are FlxSort.ASCENDING (default) and FlxSort.DESCENDING.

update(elapsed:Float):Void

Automatically goes through and calls update on everything you added.

Defined by FlxBasic