Estoy tratando de restablecer mi animación al cuadro 0 después de terminar. Para esto estoy usando el siguiente código:
gameState.idle.play('wag', true).on('animationcomplete', () => {gameState.idle.pause(gameState.idle.currentAnim.frames[0])});
Sin embargo, cuando intento esto me sale el error
TypeError no detectado: gameState.idle.frames no está definido
¿Cómo puedo arreglar esto?
Si solo desea saber cómo obtener el primer cuadro de la animación actual, puede (si el código publicado es correcto y lo interpreté correctamente) :
gameState.idle.play('wag', true).on('animationcomplete', (currentAnim) => { gameState.idle.pause(currentAnim.frames[0]) }); Esto debería funcionar. aquí el enlace a la documentación, explica los parámetros del evento ANIMATION_COMPLETE . ( https://photonstorm.github.io/phaser3-docs/Phaser.Animations.Events.html#event:ANIMATION_COMPLETE )
Si no desea utilizar el parámetro pasado a los eventos, puede utilizar este código:
gameState.idle.play('wag', true).on('animationcomplete', () => { gameState.idle.pause(gameState.idle.anims.currentAnim.frames[0]) });También funciona, pero no es tan fácil de leer.
bajo el supuesto de que
gameState.idlees unPhaser.GameObjects.Sprite
Actualización (con código de jsfiddle de los comentarios):
la función de pause no es una función del objeto Sprite, es una función de las propiedades Phaser.Animations.AnimationState del Sprite :
const gameState = { gameWidth: 400, gameHeight: 200, menu: {}, textStyle: { fontFamily: "'Comic Sans MS'", fill: "#fff", align: "center", boundsAlignH: "left", boundsAlignV: "top", wordWrap: true, wordWrapWidth: 300 } }; function preload() { //this.load.baseURL = 'assets/'; //this.load.atlas('idle', 'idle.png', 'idle.json'); // added Image from an official phaser example, for a working example this.load.spritesheet('idle', 'https://labs.phaser.io/assets/animations/brawler48x48.png', { frameWidth: 48, frameHeight: 48 }); } function create() { this.anims.create({ key: "wag", frameRate: 8, frames: this.anims.generateFrameNumbers("idle", { start: 0, end: 5}), repeat: 0, }); gameState.idle = this.add.sprite(200, 100, "idle"); gameState.idle.setInteractive({cursor: 'url("assets/pet.cur"), pointer'}); } function update() { let test = Math.floor(Math.random() * (50) + 1); if (test == 50) gameState.idle.play('wag', true).on('animationcomplete', (currentAnim, frame, gameObject) =>{ gameObject.anims.pause(currentAnim.frames[0]); }); } var config = { backgroundColor: "0xf0f0f0", scale: { width: gameState.gameWidth, height: gameState.gameHeight, autoCenter: Phaser.Scale.CENTER_BOTH }, scene: { preload, create, update } }; var game = new Phaser.Game(config); <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>