Skip to main content

FlixelSpriteGroup

View source

class

org.flixelgdx.group.FlixelSpriteGroup

public class FlixelSpriteGroup extends FlixelSprite implements FlixelBasicGroupable<FlixelSprite>

A special FlixelSprite that can be treated like a single sprite even when made up of several member sprites. It implements FlixelBasicGroupable for managing members while inheriting all sprite properties from FlixelSprite.

Because FlixelSpriteGroup extends FlixelSprite, groups can be nested inside other groups, enabling complex hierarchical sprite compositions. Any property change on the group (position, alpha, color, scale, rotation, flip) automatically propagates to all members.

Sprites added to the group are automatically offset by the group's current position and have their alpha multiplied by the group's alpha. When a sprite is removed, its position offset is subtracted to restore local coordinates.

Rotation behavior is controlled by RotationMode:

  • RotationMode.INDIVIDUAL (default): the rotation delta is applied to each sprite's own rotation; no positional changes occur.
  • RotationMode.WHEEL: sprites are arranged radially around the center like a wheel, repositioned absolutely each frame in FlixelSpriteGroup.update(float).
  • RotationMode.ORBIT: sprites orbit around the group origin as a rigid body; both position and individual rotation are adjusted by the delta.

FlixelSpriteGroup.remove(FlixelSprite) and FlixelSpriteGroup.detach(FlixelSprite) restore local coordinates and unlink the sprite; they do not call FlixelSprite.destroy(). Use FlixelBasic.kill() / FlixelBasic.revive() or FlixelSpriteGroup.recycle() for reuse. FlixelSpriteGroup.clear() unlinks all members without destroying them. FlixelSpriteGroup.destroy() on this group destroys every member (releases graphics) and resets group state.

Constructors

FlixelSpriteGroup()

public FlixelSpriteGroup()

Creates a sprite group with no member limit and default wheel radius 100.


FlixelSpriteGroup(int, float, float)

public FlixelSpriteGroup(int maxSize, float rotationRadius, float rotation)

Creates a sprite group with the given maximum size, rotation radius, and rotation.

Parameters:

NameDescription
maxSizeThe maximum size of the group (0 = unlimited).
rotationRadiusThe radius used by RotationMode.WHEEL.
rotationThe group's initial angle in degrees.

Fields

members

protected final SnapshotArray<FlixelSprite> members

The members belonging to this group.


maxSize

protected int maxSize

Maximum members allowed. When 0, the group can grow without limit.


Methods

setX(float)

public void setX(float x)

setY(float)

public void setY(float y)

setPosition(float, float)

public void setPosition(float x, float y)

loadGraphic(Texture, int, int)

public final FlixelSprite loadGraphic(Texture texture, int frameWidth, int frameHeight)

makeGraphic(int, int, Color)

public final FlixelSprite makeGraphic(int width, int height, Color color)

setAngle(float)

public void setAngle(float degrees)

Sets the group's rotation in degrees. The behavior depends on the current RotationMode:

  • RotationMode.INDIVIDUAL: the delta is applied to each sprite's own rotation.
  • RotationMode.WHEEL: the value is stored and applied during the next FlixelSpriteGroup.update(float) call.
  • RotationMode.ORBIT: each sprite's position is rotated around the group origin by the delta, and its individual rotation is adjusted by the same amount.

Parameters:

NameDescription
degreesthe new rotation in degrees.

getRotationMode()

public RotationMode getRotationMode()

setRotationMode(RotationMode)

public void setRotationMode(RotationMode rotationMode)

getRotationRadius()

public float getRotationRadius()

setRotationRadius(float)

public void setRotationRadius(float rotationRadius)

setAlpha(float)

public void setAlpha(float a)

Sets the opacity of the group and all of its current members. Members added later via FlixelSpriteGroup.add(FlixelSprite) will have their alpha multiplied by this value rather than overwritten.

Parameters:

NameDescription
aAlpha between 0 (fully transparent) and 1 (fully opaque).

getAlpha()

public float getAlpha()

setColor(Color)

public void setColor(Color tint)

Sets a color tint on the group and propagates it to all current members.


setColor(float, float, float, float)

public void setColor(float r, float g, float b, float a)

Sets a color tint on the group and propagates it to all current members.


setColor(FlixelColor)

public void setColor(FlixelColor tint)

Sets a color tint on the group and propagates it to all current members.


setScale(float)

public void setScale(float scaleXY)

Sets a uniform scale on the group and propagates it to all current members.


setScale(float, float)

public void setScale(float scaleX, float scaleY)

Sets a non-uniform scale on the group and propagates it to all current members.


flip(boolean, boolean)

public void flip(boolean x, boolean y)

Toggles the flip state on the X and/or Y axis for the group and all current members.


setFlipX(boolean)

public void setFlipX(boolean flipX)

Sets the X-axis flip state on the group and all members to the desired value. Unlike FlixelSpriteGroup.flip(boolean, boolean), which toggles, this method ensures a specific state.

Parameters:

NameDescription
flipXtrue to flip horizontally, false to un-flip.

setFlipY(boolean)

public void setFlipY(boolean flipY)

Sets the Y-axis flip state on the group and all members to the desired value. Unlike FlixelSpriteGroup.flip(boolean, boolean), which toggles, this method ensures a specific state.

Parameters:

NameDescription
flipYtrue to flip vertically, false to un-flip.

setAntialiasing(boolean)

public void setAntialiasing(boolean antialiasing)

isAntialiasing()

public boolean isAntialiasing()

getAntialiasing()

public boolean getAntialiasing()

Returns whether antialiasing is enabled for sprites in this group.


getFacing()

public int getFacing()

setFacing(int)

public void setFacing(int facing)

setOrigin(float, float)

public void setOrigin(float originX, float originY)

Sets the rotation and scale pivot point on every current member.


setOriginCenter()

public void setOriginCenter()

Centers the origin on every current member based on each member's own dimensions.


add(FlixelSprite)

public void add(FlixelSprite sprite)

Adds a sprite to the group. The sprite's position is automatically offset by the group's current position, and its alpha is multiplied by the group's alpha. If the group has a FlixelSpriteGroup.maxSize and is already at capacity, the sprite is not added.

Parameters:

NameDescription
spriteThe sprite to add.

addAndReturn(FlixelSprite)

public FlixelSprite addAndReturn(FlixelSprite sprite)

Adds a sprite to the group and returns it, allowing for method chaining.

Parameters:

NameDescription
spriteThe sprite to add.

Returns: The added sprite.


insert(int, FlixelSprite)

public void insert(int index, FlixelSprite sprite)

Inserts a sprite at the given index, offset by the group's current position. The index is clamped to the valid range [0, length].

Parameters:

NameDescription
indexThe index to insert the sprite at.
spriteThe sprite to insert.

remove(FlixelSprite)

public void remove(FlixelSprite sprite)

Removes the sprite from the group and restores local coordinates. Does not call FlixelSprite.destroy().


detach(FlixelSprite)

public void detach(FlixelSprite sprite)

Same as FlixelSpriteGroup.remove(FlixelSprite): restores local coordinates and unlinks the sprite.


recycle()

public FlixelSprite recycle()

Revives the first dead member, or creates a new sprite, applies FlixelSpriteGroup.preAdd(FlixelSprite), and adds it when under FlixelSpriteGroup.maxSize. When at capacity and no dead slot exists, returns null.


replace(FlixelSprite, FlixelSprite)

public FlixelSprite replace(FlixelSprite oldSprite, FlixelSprite newSprite)

Replaces an existing member with a new sprite. The new sprite is offset by the group's current position. If oldSprite is not found, newSprite is simply added to the end of the group instead.

Parameters:

NameDescription
oldSpriteThe member to replace.
newSpriteThe replacement sprite.

Returns: The replacement sprite.


clear()

public void clear()

Unlinks every member and restores local coordinates. Does not call FlixelSprite.destroy().


get(int)

public FlixelSprite get(int index)

Returns the member at the given index, or null if the index is out of bounds.

Parameters:

NameDescription
indexThe index of the member to get.

Returns: The member at the given index, or null if the index is out of bounds.


getLength()

public int getLength()

countMembers()

public int countMembers()

Returns the number of non-null members, which may differ from FlixelSpriteGroup.getLength().


isEmpty()

public boolean isEmpty()

getEmpty()

public boolean getEmpty()

Returns whether this group contains no members.


getMaxSize()

public int getMaxSize()

setMaxSize(int)

public void setMaxSize(int maxSize)

getMembers()

public SnapshotArray<FlixelSprite> getMembers()

getRandom()

public FlixelSprite getRandom()

Returns a random member, or null if the group is empty.


getRandom(int, int)

public FlixelSprite getRandom(int startIndex, int length)

Returns a random member from the range [startIndex, startIndex + length).

Parameters:

NameDescription
startIndexThe first index (inclusive).
lengthThe number of elements to consider. If <= 0, the entire group is used.

Returns: A random member from the range, or null if the range is empty.


sort(Comparator<FlixelSprite>)

public void sort(Comparator<FlixelSprite> comparator)

Sorts the members of the group using the given comparator.

Parameters:

NameDescription
comparatorThe comparator to use to sort the members.

getFirstMatching(Predicate<FlixelSprite>)

public FlixelSprite getFirstMatching(Predicate<FlixelSprite> predicate)

Returns the first member that satisfies the predicate, or null if none match.

Parameters:

NameDescription
predicateThe predicate to test the members against.

Returns: The first member that satisfies the predicate, or null if none match.


indexOf(FlixelSprite)

public int indexOf(FlixelSprite sprite)

Returns the index of the given sprite in the group. If the sprite is not a member of the group, returns -1.

Parameters:

NameDescription
spriteThe sprite to get the index of.

Returns: The index of the given sprite in the group, or -1 if the sprite is not a member of the group.


contains(FlixelSprite)

public boolean contains(FlixelSprite sprite)

Returns true if the group contains the given sprite, false otherwise.

Parameters:

NameDescription
spriteThe sprite to check.

Returns: true if the group contains the given sprite, false otherwise.


bringToFront(FlixelSprite)

public void bringToFront(FlixelSprite sprite)

Moves a member to the end of the draw list so that it renders on top of all other members. Has no effect if the sprite is not a member of this group.

Parameters:

NameDescription
spriteThe sprite to bring to the front of the group's list.

sendToBack(FlixelSprite)

public void sendToBack(FlixelSprite sprite)

Moves a member to the beginning of the draw list so that it renders behind all other members. Has no effect if the sprite is not a member of this group.

Parameters:

NameDescription
spriteThe sprite to send to the back of the group's list.

swapMembers(int, int)

public void swapMembers(int index1, int index2)

Swaps the draw order of two members by their indices. Out-of-bounds indices are silently ignored.

Parameters:

NameDescription
index1The index of the first sprite to swap.
index2The index of the second sprite to swap.

getBounds(Rectangle)

public Rectangle getBounds(Rectangle out)

Computes the axis-aligned bounding rectangle that encloses all members, taking each member's position, size, and scale into account.

Parameters:

NameDescription
outAn optional output rectangle. If null, a new one is created.

Returns: The bounding rectangle.


getMidpoint(Vector2)

public Vector2 getMidpoint(Vector2 out)

Returns the center point of the bounding rectangle that encompasses all members.

Parameters:

NameDescription
outAn optional output vector. If null, a new one is created.

Returns: The midpoint of the group's bounds.


update(float)

public void update(float delta)

draw(FlixelBatch)

public void draw(FlixelBatch batch)

destroy()

public void destroy()

RotationMode

enum

org.flixelgdx.group.FlixelSpriteGroup.RotationMode

public static final enum RotationMode

Controls how a FlixelSpriteGroup's rotation affects its members.

Fields

INDIVIDUAL

public static final RotationMode INDIVIDUAL

Rotation delta is applied to each sprite's individual rotation. No positional changes occur.


WHEEL

public static final RotationMode WHEEL

Sprites are arranged in a radial pattern around the group center. Each sprite is positioned at FlixelSpriteGroup.rotationRadius from the center, spaced evenly around 360 degrees. Positions and rotations are set absolutely each frame in FlixelSpriteGroup.update(float).

Individual sprite rotations cannot be changed independently in this mode.


ORBIT

public static final RotationMode ORBIT

All sprites orbit around the group origin as a rigid body. When the rotation changes, each sprite's position is rotated around the center by the delta, and its individual rotation is adjusted by the same amount.


Methods

values()

public static RotationMode[] values()

valueOf(String)

public static RotationMode valueOf(String name)