Hey guys I’m having trouble with running proper animations on my game.
I used texture packer to put all the animations of my ninjaboy character.
the images are saved as idle_001, idle_002 etc etc.
My problem is that when I apply changes to the sprite for example setScale(0.3f, 0.3f) it only works if I render this specific sprite, but when i render it through an animation the changes don’t take effect.
Here is my code
package com.haki.loh.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.Array;
import com.haki.loh.gametates.GameState;
public class Player extends Entity {
private TextureAtlas textureAtlas;
private Array<Sprite> ninjaBoyIdle;
private float time;
private Animation animation;
public Player(GameState state) {
super(state);
this.batch = state.getBatch();
//load texture pack file
textureAtlas = new TextureAtlas(
Gdx.files.internal("images/ninjaboy/ninjaboy.txt"));
//create Array of Sprties
ninjaBoyIdle = textureAtlas.createSprites("idle_");
//resize all sprites
for (int i = 0; i < ninjaBoyIdle.size; i++) {
ninjaBoyIdle.get(i).setScale(0.3f, 0.3f);
}
//create the animation
animation = new Animation(1 / 12f, ninjaBoyIdle);
world = state.getWorld();
BodyDef bdef = new BodyDef();
bdef.position.set(160 / PPM, 200 / PPM);
bdef.type = BodyType.DynamicBody;
body = world.createBody(bdef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(14 / PPM, 14 / PPM);
FixtureDef fdef = new FixtureDef();
fdef.shape = shape;
body.createFixture(fdef).setUserData(body);
// create foot
shape.setAsBox(4 / PPM, 4 / PPM, new Vector2(0, -14 / PPM), 0);
bdef.position.set(160 / PPM, 100 / PPM);
fdef.shape = shape;
fdef.isSensor = true;
body.createFixture(fdef);
}
@Override
public void update(float dt) {
}
@Override
public void render() {
batch.begin();
batch.draw(animation.getKeyFrame(time += 1 / 60f, true), 0, 0);
batch.end();
}
}
From the code above the animation is playing and everything is working except that I changed the scale of everything inside the SpriteArray before i loaded it into the Animation class. The changes did not take effect…
However if I change the render() to this
public void render() {
batch.begin();
ninjaBoyIdle.get(i).draw(batch);
batch.end();
}
The changes I made on the for loop will apply. If anybody could provide guidance it would be much appreciated.