I'm developing a game for Android using andEngine. I have to change sprite images dynamically. In J2ME, i used
sprite.setImage("img.png");
But in andengine, i not able to find method
//sprite.setImage(?); -In andengine
Any solutions ?
I think use TextureRegion is a better way to change sprite's image.
Add the following code in org.anddev.andengine.entity.sprite
public void setTextureRegion(TextureRegion textureRegion) {
this.mTextureRegion = textureRegion;
}
Then you can change image by this method. You can check the andengine samples to see how to create a texture region from an image.
PS, if mTextureRegion is final, just remove the final syntax.
You need to use TiledSprite instead of simple Sprite. That TiledSprite takes TiledTextureRegion as parameter. You create a single TiledTextureRegion containing an image with all the small images you need to set on your sprite. Then you call setCurrentTileIndex(index) where index is the index of the image you need to place on your sprite.
I believe you have to do it manually, that is detach/hide the sprite and attach/show a different one.
I solved this by detaching my sprite, assigning a new Sprite (mySprite = new Sprite(...)) to my sprite and attaching my sprite again.