FlixelSpriteGroup
View sourceclass
org.flixelgdx.group.FlixelSpriteGroup
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()
Creates a sprite group with no member limit and default wheel radius 100.
FlixelSpriteGroup(int, float, float)
Creates a sprite group with the given maximum size, rotation radius, and rotation.
Parameters:
| Name | Description |
|---|---|
maxSize | The maximum size of the group (0 = unlimited). |
rotationRadius | The radius used by RotationMode.WHEEL. |
rotation | The group's initial angle in degrees. |
Fields
members
The members belonging to this group.
maxSize
Maximum members allowed. When 0, the group can grow without limit.
Methods
setX(float)
setY(float)
setPosition(float, float)
loadGraphic(Texture, int, int)
makeGraphic(int, int, Color)
setAngle(float)
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:
| Name | Description |
|---|---|
degrees | the new rotation in degrees. |
getRotationMode()
setRotationMode(RotationMode)
getRotationRadius()
setRotationRadius(float)
setAlpha(float)
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:
| Name | Description |
|---|---|
a | Alpha between 0 (fully transparent) and 1 (fully opaque). |
getAlpha()
setColor(Color)
Sets a color tint on the group and propagates it to all current members.
setColor(float, float, float, float)
Sets a color tint on the group and propagates it to all current members.
setColor(FlixelColor)
Sets a color tint on the group and propagates it to all current members.
setScale(float)
Sets a uniform scale on the group and propagates it to all current members.
setScale(float, float)
Sets a non-uniform scale on the group and propagates it to all current members.
flip(boolean, boolean)
Toggles the flip state on the X and/or Y axis for the group and all current members.
setFlipX(boolean)
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:
| Name | Description |
|---|---|
flipX | true to flip horizontally, false to un-flip. |
setFlipY(boolean)
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:
| Name | Description |
|---|---|
flipY | true to flip vertically, false to un-flip. |
setAntialiasing(boolean)
isAntialiasing()
getAntialiasing()
Returns whether antialiasing is enabled for sprites in this group.
getFacing()
setFacing(int)
setOrigin(float, float)
Sets the rotation and scale pivot point on every current member.
setOriginCenter()
Centers the origin on every current member based on each member's own dimensions.
add(FlixelSprite)
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:
| Name | Description |
|---|---|
sprite | The sprite to add. |
addAndReturn(FlixelSprite)
Adds a sprite to the group and returns it, allowing for method chaining.
Parameters:
| Name | Description |
|---|---|
sprite | The sprite to add. |
Returns: The added sprite.
insert(int, FlixelSprite)
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:
| Name | Description |
|---|---|
index | The index to insert the sprite at. |
sprite | The sprite to insert. |
remove(FlixelSprite)
Removes the sprite from the group and restores local coordinates. Does not call FlixelSprite.destroy().
detach(FlixelSprite)
Same as FlixelSpriteGroup.remove(FlixelSprite): restores local coordinates and unlinks the sprite.
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)
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:
| Name | Description |
|---|---|
oldSprite | The member to replace. |
newSprite | The replacement sprite. |
Returns: The replacement sprite.
clear()
Unlinks every member and restores local coordinates. Does not call FlixelSprite.destroy().
get(int)
Returns the member at the given index, or null if the index is out of bounds.
Parameters:
| Name | Description |
|---|---|
index | The index of the member to get. |
Returns: The member at the given index, or null if the index is out of bounds.
getLength()
countMembers()
Returns the number of non-null members, which may differ from FlixelSpriteGroup.getLength().
isEmpty()
getEmpty()
Returns whether this group contains no members.
getMaxSize()
setMaxSize(int)
getMembers()
getRandom()
Returns a random member, or null if the group is empty.
getRandom(int, int)
Returns a random member from the range [startIndex, startIndex + length).
Parameters:
| Name | Description |
|---|---|
startIndex | The first index (inclusive). |
length | The 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>)
Sorts the members of the group using the given comparator.
Parameters:
| Name | Description |
|---|---|
comparator | The comparator to use to sort the members. |
getFirstMatching(Predicate<FlixelSprite>)
Returns the first member that satisfies the predicate, or null if none match.
Parameters:
| Name | Description |
|---|---|
predicate | The predicate to test the members against. |
Returns: The first member that satisfies the predicate, or null if none match.
indexOf(FlixelSprite)
Returns the index of the given sprite in the group. If the sprite is not a member of the group, returns -1.
Parameters:
| Name | Description |
|---|---|
sprite | The 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)
Returns true if the group contains the given sprite, false otherwise.
Parameters:
| Name | Description |
|---|---|
sprite | The sprite to check. |
Returns: true if the group contains the given sprite, false otherwise.
bringToFront(FlixelSprite)
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:
| Name | Description |
|---|---|
sprite | The sprite to bring to the front of the group's list. |
sendToBack(FlixelSprite)
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:
| Name | Description |
|---|---|
sprite | The sprite to send to the back of the group's list. |
swapMembers(int, int)
Swaps the draw order of two members by their indices. Out-of-bounds indices are silently ignored.
Parameters:
| Name | Description |
|---|---|
index1 | The index of the first sprite to swap. |
index2 | The index of the second sprite to swap. |
getBounds(Rectangle)
Computes the axis-aligned bounding rectangle that encloses all members, taking each member's position, size, and scale into account.
Parameters:
| Name | Description |
|---|---|
out | An optional output rectangle. If null, a new one is created. |
Returns: The bounding rectangle.
getMidpoint(Vector2)
Returns the center point of the bounding rectangle that encompasses all members.
Parameters:
| Name | Description |
|---|---|
out | An optional output vector. If null, a new one is created. |
Returns: The midpoint of the group's bounds.
update(float)
draw(FlixelBatch)
destroy()
RotationMode
enum
org.flixelgdx.group.FlixelSpriteGroup.RotationMode
Controls how a FlixelSpriteGroup's rotation affects its members.
Fields
INDIVIDUAL
Rotation delta is applied to each sprite's individual rotation. No positional changes occur.
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
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.