我正在使用andEngine为Android开发游戏。我必须动态更改精灵图像。在J2ME中,我使用了
sprite.setImage("img.png");
但是在andengine中,我无法找到方法
//sprite.setImage(?); -In andengine
任何解决方案?
答案 0 :(得分:7)
我认为使用TextureRegion是更改精灵图像的更好方法。
在org.anddev.andengine.entity.sprite
中添加以下代码public void setTextureRegion(TextureRegion textureRegion) {
this.mTextureRegion = textureRegion;
}
然后您可以通过此方法更改图像。您可以检查andengine样本以了解如何从图像创建纹理区域。
PS,如果mTextureRegion
是最终版,则只需删除final
语法。
答案 1 :(得分:3)
您需要使用TiledSprite
而非简单Sprite
。 TiledSprite
将TiledTextureRegion
作为参数。您创建一个TiledTextureRegion
包含一个图像,其中包含您在精灵上设置的所有小图像。然后,您致电setCurrentTileIndex(index)
,其中index
是您需要在精灵上投放的图片的索引。
答案 2 :(得分:0)
我相信你必须手动完成,即分离/隐藏精灵并附加/显示不同的精灵。
答案 3 :(得分:-1)
我通过分离我的精灵,为我的精灵分配一个新的Sprite(mySprite = new Sprite(...))并再次附加我的精灵来解决这个问题。