3

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 ?

4

4 回答 4

7

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.

于 2012-09-04T05:29:30.960 回答
3

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.

于 2012-07-26T12:21:51.527 回答
0

I believe you have to do it manually, that is detach/hide the sprite and attach/show a different one.

于 2012-07-26T11:36:38.553 回答
-1

I solved this by detaching my sprite, assigning a new Sprite (mySprite = new Sprite(...)) to my sprite and attaching my sprite again.

于 2014-07-01T09:10:46.683 回答