Phaser更新精灵纹理

Phaser update sprite texture

我在更新 sprite 时遇到问题。

我创建了一个换衣服的功能。这应该像这样工作:

// Something like this.
pet = pet_with_hat

在 js/main.js 的第 387 行 https://github.com/futer/pandachii 处有代码。

当我写这篇文章时:

this.pet = this.game.add.sprite(90,90, 'pet_black_hat')

出现错误:

 Uncaught TypeError: Cannot read property 'health' of undefined
 GameState.update @ main.js:194
 Phaser.StateManager.update @ phaser.js:16628
 Phaser.Game.update @ phaser.js:23092
 Phaser.RequestAnimationFrame.updateRAF @ phaser.js:41421
 _onLoop @ phaser.js:41407

如何将此宠物的精灵图像更改为另一个?

我将不胜感激。

问题是您将现有的 this.pet 替换为新的 Phaser.Sprite,然后新的 Phaser.Sprite 没有您设置的任何自定义属性。

对于移相器 2:

您可以使用loadTexture()替换宠物精灵的图像。

this.pet.loadTexture('pet_black_hat');

对于移相器 3:

您可以使用setTexture()替换宠物精灵的图像。

this.pet.setTexture('pet_black_hat');

对于 Phaser 3,您可以使用 setTexture

carSprite.setTexture('tank')
  • 如果您使用 scene.add.image(...) 添加了 new 图像,那么您可以使用, carSprite.setTexture('tank')

  • 如果你想select你用scene.load.atlas(...)加载的同一纹理图集中的不同精灵,那么你可以调用carSprite.setFrame('tank')

  • 如果你想select一个精灵在一个完全不同的纹理图集中,那么你可以调用,carSprite.setTexture('newTextureAtlasKey', 'tank')

希望这对您有所帮助。