user1554336
user1554336

Reputation:

Sprite.setImage() in andengine

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 ?

Upvotes: 3

Views: 4648

Answers (4)

ediheld
ediheld

Reputation: 233

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

Upvotes: -1

Zhenghong Wang
Zhenghong Wang

Reputation: 2127

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.

Upvotes: 7

user874649
user874649

Reputation:

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.

Upvotes: 3

JohnEye
JohnEye

Reputation: 6895

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

Upvotes: 0

Related Questions