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 Answers4

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.

Zhenghong Wang
  • 2,117
  • 17
  • 19
  • This way is the easiest way of changing sprite's image..Thanks..! –  Sep 04 '12 at 08:40
  • This has not been possible for a long time. As of at least 2 years ago the field mTextureRegion has been marked final, here's the source https://github.com/nicolasgramlich/AndEngine/blob/GLES2/src/org/andengine/entity/sprite/Sprite.java?source=c – Sojurn Mar 03 '14 at 05:20
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.

  • What about the width and height constraints of that image? –  Jul 26 '12 at 12:24
  • And i need to check pixel level collision so i'm using PixelPerfectAnimatedSprite,so can i check pixel level collision for TiledSprite as well? –  Jul 26 '12 at 12:55
  • @Andy Sorry I haven't used pixel perfect collision so I don't know. I write my own collision functions depending on the sprites. And in regard to the width and height, are you referring to the big image containing the small ones or the small image? –  Jul 26 '12 at 13:10
  • big image containing small images whose size is exceeding 1024x1024,so it might be a constraint.. –  Jul 26 '12 at 13:29
  • Indeed if the size is exceeding `1024x1024` low end devices will have to suffer, I had that problem myself when I was developing my game. –  Jul 26 '12 at 13:33
  • So how did you overcome this? –  Jul 27 '12 at 04:53
  • In your case with TiledTextureRegion I'm not sure how you can do this, in my case I made multiple sprites with separate Atlases and TextureRegions allocated, to build what I needed. –  Jul 27 '12 at 06:23
0

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

JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • I have to change 5 to 6 images for a single sprite dynamically,so i have to use 6 different sprites.. Any method in which dis can be done using a single sprite? –  Jul 26 '12 at 12:07
  • Yes, or maybe you can use animated sprite instead. Don't worry about the number of Sprites, AndEngine should handle it easily. – JohnEye Jul 26 '12 at 12:11
  • I'm using an animated sprite for which i have to replace another 5 to 6 images which again has few frames.. Is there a simple way by which i can do dis without creating a different sprite? –  Jul 26 '12 at 12:18
  • See the great answer by Alexandru Averescu. – JohnEye Jul 26 '12 at 12:28
  • If i use TiledSprite,image width and height is exceeding 1024x1024,from which am forced to use different images.Any equivalent method for setImage() in J2ME for changing sprite images? –  Jul 26 '12 at 12:36
  • Sprites larger than 1024x1024 may not be a problem, I tried 4096x4096 some time ago and it worked on the devices I could test it on. – JohnEye Jul 26 '12 at 12:43
  • Not sure, I would expect it not to work on older versions and work on newer. Is there any reason why it should not be so? – JohnEye Jul 26 '12 at 13:09
  • I know, but is there a reason why it should be problem on _newer_ devices? Old versions of Android are now pretty rare. – JohnEye Jul 26 '12 at 13:18
  • http://stackoverflow.com/questions/6815141/what-is-maximum-size-of-texture-andengine.. –  Jul 26 '12 at 13:27
-1

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

ediheld
  • 233
  • 3
  • 13