Sprite

Extends: AnimatedImage

Properties

  • spriteSheet (ifDraw2d) — roBitmap to pick cells from (the first sheet, when there are several)
  • spriteSheets (Array.<ifDraw2d>) — Every sheet, in order. Cells are numbered across them: sheet 0's first, then sheet 1's, ...
  • cellWidth (integer) — Width of each animation cell in the sprite image in pixels (0 for packed frames)
  • cellHeight (integer) — Height of each animation cell in the sprite image in pixels (0 for packed frames)
  • spriteFrames (BGE.SpriteFrames) — Which rect of which sheet each frame index draws; may be shared with other sprites
  • animations (roAssociativeArray) — Lookup map of animation name -> SpriteAnimation object
  • activeAnimation (SpriteAnimation) — The current animation being played

Constructor

new Sprite( owner: GameEntity, spriteSheet: ifDraw2d | Array.<ifDraw2d> | BGE.SpriteFrames, cellWidth: integer, cellHeight: integer, args?: roAssociativeArray, ): Sprite

Parameters

  • owner (GameEntity)
  • spriteSheet (ifDraw2d | Array.<ifDraw2d> | BGE.SpriteFrames) — One sheet, several with the same cell size (cells are numbered across sheets in order), or a BGE.SpriteFrames for packed frames or to share regions with other sprites
  • cellWidth (integer) — Width of each cell in pixels (ignored for a BGE.SpriteFrames)
  • cellHeight (integer) — Height of each cell in pixels (ignored for a BGE.SpriteFrames)
  • args (roAssociativeArray, optional, default: "{}") — Any extra properties to set

Instance Methods

setAnchor(x: float, y: float): void

Parameters

  • x (float)
  • y (float)

Returns

  • void

applyAnchorToRegion(): void

protected

Returns

  • void

getSheetIndexForFrame(frameIndex: integer): integer

Which sheet a cell index comes from, or -1 if it's out of range

Parameters

  • frameIndex (integer)

Returns

  • integer

getSpriteSheetsDebugInfo(): Array.<roAssociativeArray>

Size and cell layout of every sheet, for debug info

Returns

  • Array.<roAssociativeArray>

applyPreTranslation(x: float, y: float): void

Apply a pre-translation to set the pivot point for the sprite cell. Prefer setAnchor(): this changes the regions every sprite sharing spriteFrames draws with.

Parameters

  • x (float)
  • y (float)

Returns

  • void

addAnimation( name: string, frameList: Array.<integer> | AnimationFrameDescription, frameRate: float, playMode?: SpritePlayMode, ): BGE.SpriteAnimation

Adds a named animation.

Parameters

  • name (string)
  • frameList (Array.<integer> | AnimationFrameDescription) — Either an array of cell indexes, or an object {startFrame, frameCount}
  • frameRate (float) — Frames per second; must be above 0. BGE.frameRateFor() gives the rate for a duration.
  • playMode (SpritePlayMode, optional, default: "SpritePlayMode.Loop")

Returns

  • BGE.SpriteAnimation — the new animation, or invalid if the name is taken or the rate isn't positive

playAnimation(animationName: string, restart?: boolean): void

Play an animation from the set of animations in this SpriteSheet. Calling this repeatedly with the same already-playing animationName (e.g. every onUpdate) is a no-op for timing purposes - only an actual switch to a different animation resets the animation clock, so a one-shot Forward/Reverse animation always starts at its first frame rather than reading whatever elapsed time built up while a previous animation was playing. Pass restart = true to replay the animation already playing from its first frame (e.g. a repeated one-shot attack).

Parameters

  • animationName (string)
  • restart (boolean, optional, default: false) — reset the animation clock even if animationName is already playing

Returns

  • void

setActiveAnimation( animationName: string, resetClock: boolean, ): void

protected

Parameters

  • animationName (string)
  • resetClock (boolean)

Returns

  • void

advanceAnimationClockForTests(ms: integer): void

For tests only - fast-forwards the animation clock without waiting in real time (mirrors GameTimer's own addTime()/removeTime() testing convention). Needed because animationTimer is protected on the AnimatedImage ancestor, which a spec class (not itself a BGE.Sprite subclass) can't reach directly.

The first call freezes the clock (GameTimer.freeze()), so from then on only these calls move it and exact-time samples don't drift on a slow runner.

Parameters

  • ms (integer)

Returns

  • void

rawAnimationClockMsForTests(): integer

For tests only - the raw elapsed milliseconds on the animation clock, unaffected by any animation's own duration/play-mode clamping (unlike getCellDrawIndex()'s already-clamped output). See advanceAnimationClockForTests() above.

Returns

  • integer

getAnimationsDebugInfo(): roAssociativeArray

Debug helper - a plain data snapshot of every animation registered via addAnimation(): its frame rate/play mode, and each frame's index plus the exact spritesheet pixel rect (x/y/width/height) it draws from. Use to sanity-check a spritesheet's cell layout/frame-index math against the actual art when an animation looks like it's drawing the wrong region - see logAnimationsDebugInfo() to print this as JSON instead of inspecting it directly.

Returns

  • roAssociativeArray

getFrameRegionDebugInfo(frameIndex: integer): roAssociativeArray

One frame's sheet index and pixel rect (x/y/width/height) within that sheet, or an error note if frameIndex is out of range for this sprite's own sliced regions. Factored out of getAnimationsDebugInfo() so it's independently callable too.

Parameters

  • frameIndex (integer)

Returns

  • roAssociativeArray

logAnimationsDebugInfo(): void

Prints getAnimationsDebugInfo() as JSON via this sprite's owner Game.log() - call from onCreate()/onInput()/a Rooibos test while chasing a spritesheet slicing bug, e.g. to compare a walk cycle's actual per-frame pixel rects against the sheet's real layout.

Returns

  • void

getColumnTexture(): BGE.QuadColumnTexture

Returns

getCellDrawIndex(): integer

protected

Returns

  • integer