Class FlixelSpriteGroup
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable,com.badlogic.gdx.utils.Pool.Poolable,FlixelDebugDrawable,FlixelDestroyable,FlixelDrawable,FlixelUpdatable,FlixelBasicGroupable<FlixelSprite>,FlixelGroupable<FlixelSprite>
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 FlixelSpriteGroup.RotationMode:
FlixelSpriteGroup.RotationMode.INDIVIDUAL(default): the rotation delta is applied to each sprite's own rotation; no positional changes occur.FlixelSpriteGroup.RotationMode.WHEEL: sprites are arranged radially around the center like a wheel, repositioned absolutely each frame inupdate(float).FlixelSpriteGroup.RotationMode.ORBIT: sprites orbit around the group origin as a rigid body; both position and individual rotation are adjusted by the delta.
remove(me.stringdotjar.flixelgdx.FlixelSprite) and detach(me.stringdotjar.flixelgdx.FlixelSprite) restore local coordinates and unlink the sprite; they do not call
FlixelSprite.destroy(). Use FlixelBasic.kill() / FlixelBasic.revive() or
recycle() for reuse. clear() unlinks all members without destroying them.
destroy() on this group destroys every member (releases graphics) and resets group state.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumControls how aFlixelSpriteGroup's rotation affects its members. -
Field Summary
Fields inherited from class me.stringdotjar.flixelgdx.FlixelSprite
animation -
Constructor Summary
ConstructorsConstructorDescriptionCreates a sprite group with no member limit and default wheel radius100.FlixelSpriteGroup(int maxSize, float rotationRadius, float rotation) Creates a sprite group with the given maximum size, rotation radius, and rotation. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(@NotNull FlixelSprite sprite) Adds a sprite to the group.addAndReturn(@NotNull FlixelSprite sprite) Adds a sprite to the group and returns it, allowing for method chaining.voidbringToFront(FlixelSprite sprite) Moves a member to the end of the draw list so that it renders on top of all other members.voidclear()Unlinks every member and restores local coordinates.booleancontains(FlixelSprite sprite) Returnstrueif the group contains the given sprite,falseotherwise.intReturns the number of non-null members, which may differ fromgetLength().voiddestroy()Cleans up this object so it can be garbage-collected.voiddetach(FlixelSprite sprite) Same asremove(FlixelSprite): restores local coordinates and unlinks the sprite.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch) Draws all members in insertion order.voidflip(boolean x, boolean y) Toggles the flip state on the X and/or Y axis for the group and all current members.get(int index) Returns the member at the given index, ornullif the index is out of bounds.floatgetAlpha()com.badlogic.gdx.math.RectanglegetBounds(com.badlogic.gdx.math.Rectangle out) Computes the axis-aligned bounding rectangle that encloses all members, taking each member's position, size, and scale into account.intgetFirstMatching(Predicate<FlixelSprite> predicate) Returns the first member that satisfies the predicate, ornullif none match.intintReturns the maximum number of members allowed.@NotNull com.badlogic.gdx.utils.SnapshotArray<FlixelSprite>Returns the backing array, ornullif the implementation has not allocated it yet (FlixelGroup).com.badlogic.gdx.math.Vector2getMidpoint(com.badlogic.gdx.math.Vector2 out) Returns the center point of the bounding rectangle that encompasses all members.Returns a random member, ornullif the group is empty.getRandom(int startIndex, int length) Returns a random member from the range [startIndex,startIndex + length).floatintindexOf(FlixelSprite sprite) Returns the index of the given sprite in the group.voidinsert(int index, FlixelSprite sprite) Inserts a sprite at the given index, offset by the group's current position.booleanbooleanisEmpty()booleanfinal FlixelSpriteloadGraphic(com.badlogic.gdx.graphics.Texture texture, int frameWidth, int frameHeight) Loads a texture and automatically resizes the size ofthissprite.final FlixelSpritemakeGraphic(int width, int height, com.badlogic.gdx.graphics.Color color) Creates a solid color rectangular texture on the fly.@Nullable FlixelSpriterecycle()Revives the first dead member, or creates a new sprite, appliespreAdd(me.stringdotjar.flixelgdx.FlixelSprite), and adds it when undermaxSize.voidremove(FlixelSprite sprite) Removes the sprite from the group and restores local coordinates.replace(FlixelSprite oldSprite, FlixelSprite newSprite) Replaces an existing member with a new sprite.voidsendToBack(FlixelSprite sprite) Moves a member to the beginning of the draw list so that it renders behind all other members.voidsetAlpha(float a) Sets the opacity of the group and all of its current members.voidsetAngle(float degrees) Sets the group's rotation in degrees.voidsetAntialiasing(boolean antialiasing) voidsetColor(float r, float g, float b, float a) Sets a color tint on the group and propagates it to all current members.voidsetColor(com.badlogic.gdx.graphics.Color tint) Sets a color tint on the group and propagates it to all current members.voidsetFacing(int facing) voidsetFlipX(boolean flipX) Sets the X-axis flip state on the group and all members to the desired value.voidsetFlipY(boolean flipY) Sets the Y-axis flip state on the group and all members to the desired value.voidsetMaxSize(int maxSize) Sets the maximum number of members allowed.voidsetOrigin(float originX, float originY) Sets the rotation and scale pivot point on every current member.voidCenters the origin on every current member based on each member's own dimensions.voidsetPosition(float x, float y) Helper function to set the coordinates of this object.voidsetRotationMode(FlixelSpriteGroup.RotationMode rotationMode) voidsetRotationRadius(float rotationRadius) voidsetScale(float scaleXY) Sets a uniform scale on the group and propagates it to all current members.voidsetScale(float scaleX, float scaleY) Sets a non-uniform scale on the group and propagates it to all current members.voidsetVisible(boolean visible) voidsetX(float x) voidsetY(float y) voidsort(@NotNull Comparator<FlixelSprite> comparator) Sorts the members of the group using the given comparator.voidswapMembers(int index1, int index2) Swaps the draw order of two members by their indices.voidupdate(float delta) Updatesthissprite.Methods inherited from class me.stringdotjar.flixelgdx.FlixelSprite
applySparrowAtlas, ensureAnimation, getAtlasRegions, getColor, getCurrentFrame, getFrames, getGraphic, getOffsetX, getOffsetY, getOriginX, getOriginY, getRegion, getRegionHeight, getRegionWidth, getScaleX, getScaleY, getTexture, hasOwnedGraphic, isFlipX, isFlipY, loadGraphic, loadGraphic, loadGraphic, loadGraphic, loadGraphic, loadGraphic, loadGraphic, screenCenter, screenCenter, setAnimation, setCurrentFrameForAnimation, setGraphicSize, setOffset, setOffsetX, setOffsetY, setRegion, updateHitbox, updateHitboxMethods inherited from class me.stringdotjar.flixelgdx.FlixelObject
changeRotation, changeX, changeY, computeOverlapX, computeOverlapY, computeVelocity, getAccelerationX, getAccelerationY, getAllowCollisions, getAngle, getAngularAcceleration, getAngularDrag, getAngularVelocity, getDebugBoundingBoxColor, getDebugDrawX, getDebugDrawY, getDebugHeight, getDebugWidth, getDebugX, getDebugY, getDragX, getDragY, getElasticity, getHeight, getLastX, getLastY, getMass, getMaxAngularVelocity, getMaxVelocityX, getMaxVelocityY, getMidpointX, getMidpointY, getMoves, getScrollX, getScrollY, getTouching, getVelocityX, getVelocityY, getWasTouching, getWidth, getX, getY, inWorldBounds, isImmovable, isSolid, isTouching, justTouched, overlaps, overlapsPoint, reset, separate, separateX, separateY, setAcceleration, setAccelerationX, setAccelerationY, setAllowCollisions, setAngularAcceleration, setAngularDrag, setAngularVelocity, setDebugBoundingBoxColor, setDrag, setDragX, setDragY, setElasticity, setHeight, setImmovable, setMass, setMaxAngularVelocity, setMaxVelocity, setMaxVelocityX, setMaxVelocityY, setMoves, setScrollFactor, setSize, setSolid, setVelocity, setVelocityX, setVelocityY, setWidth, toString, updateMotion, updateTouchingFlagsMethods inherited from class me.stringdotjar.flixelgdx.FlixelBasic
dispose, kill, reset, reviveMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface me.stringdotjar.flixelgdx.group.FlixelBasicGroupable
getFirstDead, removeMemberMethods inherited from interface me.stringdotjar.flixelgdx.group.FlixelGroupable
forEachMember, forEachMemberType, getFirstNullIndex
-
Constructor Details
-
FlixelSpriteGroup
public FlixelSpriteGroup()Creates a sprite group with no member limit and default wheel radius100. -
FlixelSpriteGroup
public FlixelSpriteGroup(int maxSize, float rotationRadius, float rotation) Creates a sprite group with the given maximum size, rotation radius, and rotation.- Parameters:
maxSize- The maximum size of the group (0= unlimited).rotationRadius- The radius used byFlixelSpriteGroup.RotationMode.WHEEL.rotation- The group's initial angle in degrees.
-
-
Method Details
-
setX
public void setX(float x) - Overrides:
setXin classFlixelObject
-
setY
public void setY(float y) - Overrides:
setYin classFlixelObject
-
setPosition
public void setPosition(float x, float y) Description copied from class:FlixelObjectHelper function to set the coordinates of this object.- Overrides:
setPositionin classFlixelObject- Parameters:
x- The new x position.y- The new y position.
-
loadGraphic
public final FlixelSprite loadGraphic(com.badlogic.gdx.graphics.Texture texture, int frameWidth, int frameHeight) Description copied from class:FlixelSpriteLoads a texture and automatically resizes the size ofthissprite.- Overrides:
loadGraphicin classFlixelSprite- Parameters:
texture- The texture to load ontothissprite (owned by caller).frameWidth- How wide the sprite should be.frameHeight- How tall the sprite should be.- Returns:
thissprite for chaining.
-
makeGraphic
Description copied from class:FlixelSpriteCreates a solid color rectangular texture on the fly.- Overrides:
makeGraphicin classFlixelSprite- Parameters:
width- The width of the graphic.height- The height of the graphic.color- The color of the graphic.- Returns:
thissprite for chaining.
-
setAngle
public void setAngle(float degrees) Sets the group's rotation in degrees. The behavior depends on the currentFlixelSpriteGroup.RotationMode:FlixelSpriteGroup.RotationMode.INDIVIDUAL: the delta is applied to each sprite's own rotation.FlixelSpriteGroup.RotationMode.WHEEL: the value is stored and applied during the nextupdate(float)call.FlixelSpriteGroup.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.
- Overrides:
setAnglein classFlixelObject- Parameters:
degrees- the new rotation in degrees.
-
getRotationMode
-
setRotationMode
-
getRotationRadius
public float getRotationRadius() -
setRotationRadius
public void setRotationRadius(float rotationRadius) -
setAlpha
public void setAlpha(float a) Sets the opacity of the group and all of its current members. Members added later viaadd(FlixelSprite)will have their alpha multiplied by this value rather than overwritten.- Overrides:
setAlphain classFlixelSprite- Parameters:
a- Alpha between 0 (fully transparent) and 1 (fully opaque).
-
getAlpha
public float getAlpha()- Overrides:
getAlphain classFlixelSprite
-
setColor
public void setColor(com.badlogic.gdx.graphics.Color tint) Sets a color tint on the group and propagates it to all current members.- Overrides:
setColorin classFlixelSprite
-
setColor
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.- Overrides:
setColorin classFlixelSprite
-
setScale
public void setScale(float scaleXY) Sets a uniform scale on the group and propagates it to all current members.- Overrides:
setScalein classFlixelSprite
-
setScale
public void setScale(float scaleX, float scaleY) Sets a non-uniform scale on the group and propagates it to all current members.- Overrides:
setScalein classFlixelSprite
-
flip
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.- Overrides:
flipin classFlixelSprite
-
setFlipX
public void setFlipX(boolean flipX) Sets the X-axis flip state on the group and all members to the desired value. Unlikeflip(boolean, boolean), which toggles, this method ensures a specific state.- Parameters:
flipX-trueto flip horizontally,falseto un-flip.
-
setFlipY
public void setFlipY(boolean flipY) Sets the Y-axis flip state on the group and all members to the desired value. Unlikeflip(boolean, boolean), which toggles, this method ensures a specific state.- Parameters:
flipY-trueto flip vertically,falseto un-flip.
-
isVisible
public boolean isVisible() -
setVisible
public void setVisible(boolean visible) -
setAntialiasing
public void setAntialiasing(boolean antialiasing) - Overrides:
setAntialiasingin classFlixelSprite
-
isAntialiasing
public boolean isAntialiasing()- Overrides:
isAntialiasingin classFlixelSprite
-
getFacing
public int getFacing()- Overrides:
getFacingin classFlixelSprite
-
setFacing
public void setFacing(int facing) - Overrides:
setFacingin classFlixelSprite
-
setOrigin
public void setOrigin(float originX, float originY) Sets the rotation and scale pivot point on every current member.- Overrides:
setOriginin classFlixelSprite
-
setOriginCenter
public void setOriginCenter()Centers the origin on every current member based on each member's own dimensions.- Overrides:
setOriginCenterin classFlixelSprite
-
add
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 amaxSizeand is already at capacity, the sprite is not added.- Specified by:
addin interfaceFlixelGroupable<FlixelSprite>- Parameters:
sprite- The sprite to add.
-
addAndReturn
Adds a sprite to the group and returns it, allowing for method chaining.- Parameters:
sprite- The sprite to add.- Returns:
- The added sprite.
-
insert
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:
index- The index to insert the sprite at.sprite- The sprite to insert.
-
remove
Removes the sprite from the group and restores local coordinates. Does not callFlixelSprite.destroy().- Specified by:
removein interfaceFlixelGroupable<FlixelSprite>
-
detach
Same asremove(FlixelSprite): restores local coordinates and unlinks the sprite.- Specified by:
detachin interfaceFlixelGroupable<FlixelSprite>- Parameters:
sprite- The member to remove.
-
recycle
Revives the first dead member, or creates a new sprite, appliespreAdd(me.stringdotjar.flixelgdx.FlixelSprite), and adds it when undermaxSize. When at capacity and no dead slot exists, returnsnull. -
replace
Replaces an existing member with a new sprite. The new sprite is offset by the group's current position. IfoldSpriteis not found,newSpriteis simply added to the end of the group instead.- Parameters:
oldSprite- The member to replace.newSprite- The replacement sprite.- Returns:
- The replacement sprite.
-
clear
public void clear()Unlinks every member and restores local coordinates. Does not callFlixelSprite.destroy().- Specified by:
clearin interfaceFlixelGroupable<FlixelSprite>
-
get
Returns the member at the given index, ornullif the index is out of bounds.- Parameters:
index- The index of the member to get.- Returns:
- The member at the given index, or
nullif the index is out of bounds.
-
getLength
public int getLength() -
countMembers
public int countMembers()Returns the number of non-null members, which may differ fromgetLength(). -
isEmpty
public boolean isEmpty() -
getMaxSize
public int getMaxSize()Description copied from interface:FlixelGroupableReturns the maximum number of members allowed. When0, the group can grow without limit.- Specified by:
getMaxSizein interfaceFlixelGroupable<FlixelSprite>
-
setMaxSize
public void setMaxSize(int maxSize) Description copied from interface:FlixelGroupableSets the maximum number of members allowed. Values less than0are clamped to0(unlimited).- Specified by:
setMaxSizein interfaceFlixelGroupable<FlixelSprite>
-
getMembers
Description copied from interface:FlixelGroupableReturns the backing array, ornullif the implementation has not allocated it yet (FlixelGroup).- Specified by:
getMembersin interfaceFlixelGroupable<FlixelSprite>
-
getRandom
Returns a random member, ornullif the group is empty. -
getRandom
Returns a random member from the range [startIndex,startIndex + length).- Parameters:
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
nullif the range is empty.
-
sort
Sorts the members of the group using the given comparator.- Parameters:
comparator- The comparator to use to sort the members.
-
getFirstMatching
Returns the first member that satisfies the predicate, ornullif none match.- Parameters:
predicate- The predicate to test the members against.- Returns:
- The first member that satisfies the predicate, or
nullif none match.
-
indexOf
Returns the index of the given sprite in the group. If the sprite is not a member of the group, returns-1.- Parameters:
sprite- The sprite to get the index of.- Returns:
- The index of the given sprite in the group, or
-1if the sprite is not a member of the group.
-
contains
Returnstrueif the group contains the given sprite,falseotherwise.- Parameters:
sprite- The sprite to check.- Returns:
trueif the group contains the given sprite,falseotherwise.
-
bringToFront
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:
sprite- The sprite to bring to the front of the group's list.
-
sendToBack
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:
sprite- The sprite to send to the back of the group's list.
-
swapMembers
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:
index1- The index of the first sprite to swap.index2- The index of the second sprite to swap.
-
getBounds
public com.badlogic.gdx.math.Rectangle getBounds(com.badlogic.gdx.math.Rectangle out) Computes the axis-aligned bounding rectangle that encloses all members, taking each member's position, size, and scale into account.- Parameters:
out- An optional output rectangle. Ifnull, a new one is created.- Returns:
- The bounding rectangle.
-
getMidpoint
public com.badlogic.gdx.math.Vector2 getMidpoint(com.badlogic.gdx.math.Vector2 out) Returns the center point of the bounding rectangle that encompasses all members.- Parameters:
out- An optional output vector. Ifnull, a new one is created.- Returns:
- The midpoint of the group's bounds.
-
update
public void update(float delta) Description copied from class:FlixelSpriteUpdatesthissprite.- Specified by:
updatein interfaceFlixelUpdatable- Overrides:
updatein classFlixelSprite- Parameters:
delta- The amount of time that has passed since the last frame update.
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch) Draws all members in insertion order. The group itself does not render its own graphic; only its members are drawn. Nothing is rendered whenisVisible()isfalse.- Specified by:
drawin interfaceFlixelDrawable- Overrides:
drawin classFlixelSprite- Parameters:
batch- The batch used for rendering.
-
destroy
public void destroy()Description copied from class:FlixelBasicCleans up this object so it can be garbage-collected. A destroyedFlixelBasicshould not be used anymore. UseFlixelBasic.kill()if you only want to disable it temporarily andFlixelBasic.revive()it later.Override this function to clean up any resources used by this object, such as textures, fonts, sounds, etc.
This function is called automatically when
FlixelBasic.dispose()orFlixelBasic.reset()is executed, so you don't need to call it manually.- Specified by:
destroyin interfaceFlixelDestroyable- Overrides:
destroyin classFlixelSprite- See Also:
-