index.d.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import type { IDestroyOptions } from '@pixi/display';
  2. import { Sprite } from '@pixi/sprite';
  3. import { Texture } from '@pixi/core';
  4. /**
  5. * An AnimatedSprite is a simple way to display an animation depicted by a list of textures.
  6. *
  7. * ```js
  8. * let alienImages = ["image_sequence_01.png","image_sequence_02.png","image_sequence_03.png","image_sequence_04.png"];
  9. * let textureArray = [];
  10. *
  11. * for (let i=0; i < 4; i++)
  12. * {
  13. * let texture = PIXI.Texture.from(alienImages[i]);
  14. * textureArray.push(texture);
  15. * };
  16. *
  17. * let animatedSprite = new PIXI.AnimatedSprite(textureArray);
  18. * ```
  19. *
  20. * The more efficient and simpler way to create an animated sprite is using a {@link PIXI.Spritesheet}
  21. * containing the animation definitions:
  22. *
  23. * ```js
  24. * PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
  25. *
  26. * function setup() {
  27. * let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
  28. * animatedSprite = new PIXI.AnimatedSprite(sheet.animations["image_sequence"]);
  29. * ...
  30. * }
  31. * ```
  32. * @memberof PIXI
  33. */
  34. export declare class AnimatedSprite extends Sprite {
  35. /**
  36. * The speed that the AnimatedSprite will play at. Higher is faster, lower is slower.
  37. * @default 1
  38. */
  39. animationSpeed: number;
  40. /**
  41. * Whether or not the animate sprite repeats after playing.
  42. * @default true
  43. */
  44. loop: boolean;
  45. /**
  46. * Update anchor to [Texture's defaultAnchor]{@link PIXI.Texture#defaultAnchor} when frame changes.
  47. *
  48. * Useful with [sprite sheet animations]{@link PIXI.Spritesheet#animations} created with tools.
  49. * Changing anchor for each frame allows to pin sprite origin to certain moving feature
  50. * of the frame (e.g. left foot).
  51. *
  52. * Note: Enabling this will override any previously set `anchor` on each frame change.
  53. * @default false
  54. */
  55. updateAnchor: boolean;
  56. /**
  57. * User-assigned function to call when an AnimatedSprite finishes playing.
  58. * @example
  59. * animation.onComplete = function () {
  60. * // finished!
  61. * };
  62. */
  63. onComplete?: () => void;
  64. /**
  65. * User-assigned function to call when an AnimatedSprite changes which texture is being rendered.
  66. * @example
  67. * animation.onFrameChange = function () {
  68. * // updated!
  69. * };
  70. */
  71. onFrameChange?: (currentFrame: number) => void;
  72. /**
  73. * User-assigned function to call when `loop` is true, and an AnimatedSprite is played and
  74. * loops around to start again.
  75. * @example
  76. * animation.onLoop = function () {
  77. * // looped!
  78. * };
  79. */
  80. onLoop?: () => void;
  81. private _playing;
  82. private _textures;
  83. private _durations;
  84. /**
  85. * `true` uses PIXI.Ticker.shared to auto update animation time.
  86. * @default true
  87. */
  88. private _autoUpdate;
  89. /**
  90. * `true` if the instance is currently connected to PIXI.Ticker.shared to auto update animation time.
  91. * @default false
  92. */
  93. private _isConnectedToTicker;
  94. /** Elapsed time since animation has been started, used internally to display current texture. */
  95. private _currentTime;
  96. /** The texture index that was displayed last time. */
  97. private _previousFrame;
  98. /**
  99. * @param textures - An array of {@link PIXI.Texture} or frame
  100. * objects that make up the animation.
  101. * @param {boolean} [autoUpdate=true] - Whether to use PIXI.Ticker.shared to auto update animation time.
  102. */
  103. constructor(textures: Texture[] | FrameObject[], autoUpdate?: boolean);
  104. /** Stops the AnimatedSprite. */
  105. stop(): void;
  106. /** Plays the AnimatedSprite. */
  107. play(): void;
  108. /**
  109. * Stops the AnimatedSprite and goes to a specific frame.
  110. * @param frameNumber - Frame index to stop at.
  111. */
  112. gotoAndStop(frameNumber: number): void;
  113. /**
  114. * Goes to a specific frame and begins playing the AnimatedSprite.
  115. * @param frameNumber - Frame index to start at.
  116. */
  117. gotoAndPlay(frameNumber: number): void;
  118. /**
  119. * Updates the object transform for rendering.
  120. * @param deltaTime - Time since last tick.
  121. */
  122. update(deltaTime: number): void;
  123. /** Updates the displayed texture to match the current frame index. */
  124. private updateTexture;
  125. /**
  126. * Stops the AnimatedSprite and destroys it.
  127. * @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
  128. * have been set to that value.
  129. * @param {boolean} [options.children=false] - If set to true, all the children will have their destroy
  130. * method called as well. 'options' will be passed on to those calls.
  131. * @param {boolean} [options.texture=false] - Should it destroy the current texture of the sprite as well.
  132. * @param {boolean} [options.baseTexture=false] - Should it destroy the base texture of the sprite as well.
  133. */
  134. destroy(options?: IDestroyOptions | boolean): void;
  135. /**
  136. * A short hand way of creating an AnimatedSprite from an array of frame ids.
  137. * @param frames - The array of frames ids the AnimatedSprite will use as its texture frames.
  138. * @returns - The new animated sprite with the specified frames.
  139. */
  140. static fromFrames(frames: string[]): AnimatedSprite;
  141. /**
  142. * A short hand way of creating an AnimatedSprite from an array of image ids.
  143. * @param images - The array of image urls the AnimatedSprite will use as its texture frames.
  144. * @returns The new animate sprite with the specified images as frames.
  145. */
  146. static fromImages(images: string[]): AnimatedSprite;
  147. /**
  148. * The total number of frames in the AnimatedSprite. This is the same as number of textures
  149. * assigned to the AnimatedSprite.
  150. * @readonly
  151. * @default 0
  152. */
  153. get totalFrames(): number;
  154. /** The array of textures used for this AnimatedSprite. */
  155. get textures(): Texture[] | FrameObject[];
  156. set textures(value: Texture[] | FrameObject[]);
  157. /**
  158. * The AnimatedSprites current frame index.
  159. * @readonly
  160. */
  161. get currentFrame(): number;
  162. /**
  163. * Indicates if the AnimatedSprite is currently playing.
  164. * @readonly
  165. */
  166. get playing(): boolean;
  167. /** Whether to use PIXI.Ticker.shared to auto update animation time. */
  168. get autoUpdate(): boolean;
  169. set autoUpdate(value: boolean);
  170. }
  171. /** @memberof PIXI.AnimatedSprite */
  172. export declare interface FrameObject {
  173. /** The {@link PIXI.Texture} of the frame. */
  174. texture: Texture;
  175. /** The duration of the frame, in milliseconds. */
  176. time: number;
  177. }
  178. export { }