Class FlixelSpriteGroup

All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable, com.badlogic.gdx.utils.Pool.Poolable, FlixelDebugDrawable, FlixelDestroyable, FlixelDrawable, FlixelUpdatable, FlixelBasicGroupable<FlixelSprite>, FlixelGroupable<FlixelSprite>

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 FlixelSpriteGroup.RotationMode:

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.

  • Constructor Details

    • FlixelSpriteGroup

      public FlixelSpriteGroup()
      Creates a sprite group with no member limit and default wheel radius 100.
    • 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 by FlixelSpriteGroup.RotationMode.WHEEL.
      rotation - The group's initial angle in degrees.
  • Method Details

    • setX

      public void setX(float x)
      Overrides:
      setX in class FlixelObject
    • setY

      public void setY(float y)
      Overrides:
      setY in class FlixelObject
    • setPosition

      public void setPosition(float x, float y)
      Description copied from class: FlixelObject
      Helper function to set the coordinates of this object.
      Overrides:
      setPosition in class FlixelObject
      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: FlixelSprite
      Loads a texture and automatically resizes the size of this sprite.
      Overrides:
      loadGraphic in class FlixelSprite
      Parameters:
      texture - The texture to load onto this sprite (owned by caller).
      frameWidth - How wide the sprite should be.
      frameHeight - How tall the sprite should be.
      Returns:
      this sprite for chaining.
    • makeGraphic

      public final FlixelSprite makeGraphic(int width, int height, com.badlogic.gdx.graphics.Color color)
      Description copied from class: FlixelSprite
      Creates a solid color rectangular texture on the fly.
      Overrides:
      makeGraphic in class FlixelSprite
      Parameters:
      width - The width of the graphic.
      height - The height of the graphic.
      color - The color of the graphic.
      Returns:
      this sprite for chaining.
    • setAngle

      public void setAngle(float degrees)
      Sets the group's rotation in degrees. The behavior depends on the current FlixelSpriteGroup.RotationMode:
      Overrides:
      setAngle in class FlixelObject
      Parameters:
      degrees - the new rotation in degrees.
    • getRotationMode

      public FlixelSpriteGroup.RotationMode getRotationMode()
    • setRotationMode

      public void setRotationMode(FlixelSpriteGroup.RotationMode rotationMode)
    • 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 via add(FlixelSprite) will have their alpha multiplied by this value rather than overwritten.
      Overrides:
      setAlpha in class FlixelSprite
      Parameters:
      a - Alpha between 0 (fully transparent) and 1 (fully opaque).
    • getAlpha

      public float getAlpha()
      Overrides:
      getAlpha in class FlixelSprite
    • 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:
      setColor in class FlixelSprite
    • 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:
      setColor in class FlixelSprite
    • setScale

      public void setScale(float scaleXY)
      Sets a uniform scale on the group and propagates it to all current members.
      Overrides:
      setScale in class FlixelSprite
    • setScale

      public void setScale(float scaleX, float scaleY)
      Sets a non-uniform scale on the group and propagates it to all current members.
      Overrides:
      setScale in class FlixelSprite
    • 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:
      flip in class FlixelSprite
    • setFlipX

      public void setFlipX(boolean flipX)
      Sets the X-axis flip state on the group and all members to the desired value. Unlike flip(boolean, boolean), which toggles, this method ensures a specific state.
      Parameters:
      flipX - true to flip horizontally, false to un-flip.
    • setFlipY

      public void setFlipY(boolean flipY)
      Sets the Y-axis flip state on the group and all members to the desired value. Unlike flip(boolean, boolean), which toggles, this method ensures a specific state.
      Parameters:
      flipY - true to flip vertically, false to un-flip.
    • isVisible

      public boolean isVisible()
    • setVisible

      public void setVisible(boolean visible)
    • setAntialiasing

      public void setAntialiasing(boolean antialiasing)
      Overrides:
      setAntialiasing in class FlixelSprite
    • isAntialiasing

      public boolean isAntialiasing()
      Overrides:
      isAntialiasing in class FlixelSprite
    • getFacing

      public int getFacing()
      Overrides:
      getFacing in class FlixelSprite
    • setFacing

      public void setFacing(int facing)
      Overrides:
      setFacing in class FlixelSprite
    • setOrigin

      public void setOrigin(float originX, float originY)
      Sets the rotation and scale pivot point on every current member.
      Overrides:
      setOrigin in class FlixelSprite
    • setOriginCenter

      public void setOriginCenter()
      Centers the origin on every current member based on each member's own dimensions.
      Overrides:
      setOriginCenter in class FlixelSprite
    • add

      public void add(@NotNull @NotNull 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 maxSize and is already at capacity, the sprite is not added.
      Specified by:
      add in interface FlixelGroupable<FlixelSprite>
      Parameters:
      sprite - The sprite to add.
    • addAndReturn

      public FlixelSprite addAndReturn(@NotNull @NotNull FlixelSprite sprite)
      Adds a sprite to the group and returns it, allowing for method chaining.
      Parameters:
      sprite - The sprite to add.
      Returns:
      The added sprite.
    • insert

      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:
      index - The index to insert the sprite at.
      sprite - The sprite to insert.
    • remove

      public void remove(FlixelSprite sprite)
      Removes the sprite from the group and restores local coordinates. Does not call FlixelSprite.destroy().
      Specified by:
      remove in interface FlixelGroupable<FlixelSprite>
    • detach

      public void detach(FlixelSprite sprite)
      Same as remove(FlixelSprite): restores local coordinates and unlinks the sprite.
      Specified by:
      detach in interface FlixelGroupable<FlixelSprite>
      Parameters:
      sprite - The member to remove.
    • recycle

      @Nullable public @Nullable FlixelSprite recycle()
      Revives the first dead member, or creates a new sprite, applies preAdd(me.stringdotjar.flixelgdx.FlixelSprite), and adds it when under maxSize. When at capacity and no dead slot exists, returns null.
    • replace

      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:
      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 call FlixelSprite.destroy().
      Specified by:
      clear in interface FlixelGroupable<FlixelSprite>
    • get

      public FlixelSprite get(int index)
      Returns the member at the given index, or null if the index is out of bounds.
      Parameters:
      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

      public int getLength()
    • countMembers

      public int countMembers()
      Returns the number of non-null members, which may differ from getLength().
    • isEmpty

      public boolean isEmpty()
    • getMaxSize

      public int getMaxSize()
      Description copied from interface: FlixelGroupable
      Returns the maximum number of members allowed. When 0, the group can grow without limit.
      Specified by:
      getMaxSize in interface FlixelGroupable<FlixelSprite>
    • setMaxSize

      public void setMaxSize(int maxSize)
      Description copied from interface: FlixelGroupable
      Sets the maximum number of members allowed. Values less than 0 are clamped to 0 (unlimited).
      Specified by:
      setMaxSize in interface FlixelGroupable<FlixelSprite>
    • getMembers

      @NotNull public @NotNull com.badlogic.gdx.utils.SnapshotArray<FlixelSprite> getMembers()
      Description copied from interface: FlixelGroupable
      Returns the backing array, or null if the implementation has not allocated it yet (FlixelGroup).
      Specified by:
      getMembers in interface FlixelGroupable<FlixelSprite>
    • getRandom

      public FlixelSprite getRandom()
      Returns a random member, or null if the group is empty.
    • getRandom

      public FlixelSprite getRandom(int startIndex, int length)
      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 null if the range is empty.
    • sort

      public void sort(@NotNull @NotNull Comparator<FlixelSprite> comparator)
      Sorts the members of the group using the given comparator.
      Parameters:
      comparator - The comparator to use to sort the members.
    • getFirstMatching

      public FlixelSprite getFirstMatching(Predicate<FlixelSprite> predicate)
      Returns the first member that satisfies the predicate, or null if none match.
      Parameters:
      predicate - The predicate to test the members against.
      Returns:
      The first member that satisfies the predicate, or null if none match.
    • indexOf

      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:
      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

      public boolean contains(FlixelSprite sprite)
      Returns true if the group contains the given sprite, false otherwise.
      Parameters:
      sprite - The sprite to check.
      Returns:
      true if the group contains the given sprite, false otherwise.
    • bringToFront

      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:
      sprite - The sprite to bring to the front of the group's list.
    • sendToBack

      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:
      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. If null, 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. If null, a new one is created.
      Returns:
      The midpoint of the group's bounds.
    • update

      public void update(float delta)
      Description copied from class: FlixelSprite
      Updates this sprite.
      Specified by:
      update in interface FlixelUpdatable
      Overrides:
      update in class FlixelSprite
      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 when isVisible() is false.
      Specified by:
      draw in interface FlixelDrawable
      Overrides:
      draw in class FlixelSprite
      Parameters:
      batch - The batch used for rendering.
    • destroy

      public void destroy()
      Description copied from class: FlixelBasic
      Cleans up this object so it can be garbage-collected. A destroyed FlixelBasic should not be used anymore. Use FlixelBasic.kill() if you only want to disable it temporarily and FlixelBasic.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() or FlixelBasic.reset() is executed, so you don't need to call it manually.

      Specified by:
      destroy in interface FlixelDestroyable
      Overrides:
      destroy in class FlixelSprite
      See Also: