Reputation: 2147
How to implement animation in libgdx using individual sprites or textures.I tried to do it with Packing the images in a TexturePacker and also making one png file and mapping file as specified here but it is not working perfectly as there are only 3 images to animate.Tell me how to do animation using individual images or textures.
Upvotes: 1
Views: 621
Reputation: 2147
Found the answer as mentioned below:
region1 = new TextureRegion(texture1);
region2 = new TextureRegion(texture2);
region3 = new TextureRegion(texture3);
animation = new Animation(0.08f, region1, region2, region3);
Upvotes: 0
Reputation: 6221
Okay first of all if you have 3 pictures and you want to create a kind of sprite out of it, they do need the same sizes. I would implement it as a kind of actor. If you dont like the actor idea simply delete the extends and add position and sizes to it. Also change the act
to update
and also delete the override of the draw(...,...)
.
public class IndividualSprite extends Actor {
private Array<Texture> textures; //meight change it to TextureRegions which also contains a texture but sizes and positions
private float intervaltime = 0;
private float interval;
private int count = 0;
/**
* Varargs<br>
* @param timeIntervall
* @param t
* Varargs. Parse all textures here as list or simply with ,t1
* ,t2 , t3
*/
public IndividualSprite(float timeIntervall, Texture... t) {
this.interval = timeIntervall;
this.textures = new Array<Texture>();
for (Texture texture : t) {
this.textures.add(texture);
}
// now sett default width height and pos
this.setX(0);
this.setY(0);
this.setWidth(textures.get(0).getWidth());
this.setWidth(textures.get(0).getHeight());
}
public void addTexture(Texture... t) {
for (Texture texture : t) {
this.textures.add(texture);
}
}
public void deleteTexture(Texture... t) {
for (Texture texture : t) {
this.textures.removeValue(texture, true);
}
}
public void setInterval(float i) {
this.interval = i;
}
@Override
public void act(float delta) {
super.act(delta);
this.intervaltime += delta;
if (this.intervaltime >= interval) {
if (count == textures.size) {
count = 0;
} else {
count++;
}
this.intervaltime = 0;
}
}
@Override
public void draw(SpriteBatch batch, float alpha) {
batch.begin();
batch.draw(textures.get(count), getX(), getY(), getWidth(), getHeight());
batch.end();
}
}
But an actor you can give him Actions to move or whatever. It is a kind of selfdefined Sprite. This is just one way to do it. There should be way more and maybe even better. You can also extend the Sprite class and change stuff around so it takes more than one Texture and so on. Just try it out. This is an Actor solution or non actor solution if you change the things i mentioned. Dont forget to call the .act/update
else it does nothing else than showing the first FirstTexture
.
Upvotes: 1