Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (761)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (845)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGdX - modifying Sprites and Animating them  (Read 2744 times)
0 Members and 1 Guest are viewing this topic.
Offline HakiRed

Senior Newbie


Exp: 7-9 months



« Posted 2015-02-27 13:49:49 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
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
1  
2  
3  
4  
5  
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.
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #1 - Posted 2015-02-27 14:17:36 »

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

You can use this for now.
1  
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).
1  
2  
3  
4  
5  
6  
7  
8  
9  
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);
         }

_ Wink
Offline HakiRed

Senior Newbie


Exp: 7-9 months



« Reply #2 - Posted 2015-02-27 14:36:58 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HakiRed

Senior Newbie


Exp: 7-9 months



« Reply #3 - Posted 2015-02-27 15:25:03 »

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.
Offline HakiRed

Senior Newbie


Exp: 7-9 months



« Reply #4 - Posted 2015-02-27 16:02:22 »

Folks over at LibGdx said that it is expected behavior as Sprite extends TextureRegion. So the correct way of drawing is sprite.draw();
Offline Opiop
« Reply #5 - Posted 2015-02-27 16:05:11 »

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.
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #6 - Posted 2015-02-28 03:00:10 »

Yes, this make sense.

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

_ Wink
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (349 views)
2018-06-10 19:43:48

EgonOlsen (342 views)
2018-06-10 19:43:44

EgonOlsen (290 views)
2018-06-10 19:43:20

DesertCoockie (514 views)
2018-05-13 18:23:11

nelsongames (803 views)
2018-04-24 18:15:36

nelsongames (790 views)
2018-04-24 18:14:32

ivj94 (1246 views)
2018-03-24 14:47:39

ivj94 (394 views)
2018-03-24 14:46:31

ivj94 (1057 views)
2018-03-24 14:43:53

Solater (412 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!