LibGdX - modifying Sprites and Animating them

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.

I don’t understand why batch.draw() don’t respect some sprite changes.

You can use this for now.

batch.draw(animation.getKeyFrame(time += 1 / 60f, true), 0, 0, someWidth, someHeight);

I think you can’t use setWidth ou setHeight on Sprite, only setRegionWidth and setRegionHeight. So you can use bounds(x, y, w, h).

float ninjaScale = 0.3f;

		for (int i = 0; i < ninjaBoyIdle.size; i++) {
	         ninjaBoyIdle.get(i).setBounds(
	        		 0,
	        		 0,
	        		 ninjaBoyIdle.get(i).getWidth()*ninjaScale,
	        		 ninjaBoyIdle.get(i).getHeight()*ninjaScale);
	      }

hey Thanks I guess that can work but I also need to flip stuff. I might just not use the animation class or the batch.draw(). Anyway I’ll google a bit more.

jsut an update sprite.setFlip(boolean x, boolean y); does work on the same for loop as the code above. I’ve tried setRotation() and setScale() and setAlpha() none of which seemed to work with batch.draw()

EDIT: Just did some further testing setRotation() setScale() and setAlpha() does work if i draw it like this.

ninjaBoyIdle.get(0).draw(batch);

but for batch.draw() only flip seems to work for me.

Folks over at LibGdx said that it is expected behavior as Sprite extends TextureRegion. So the correct way of drawing is sprite.draw();

Correct, from what I understand the SpriteBatch draw methods are only called when you want to draw just a texture. If you are working with the Sprite class, or any other class that has a draw method, you shouldn’t handle the drawing yourself.

Yes, this make sense.

It’s funny because I just @Override some draw() methods from actors and totally forgot about this. :persecutioncomplex: