Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  [Solved] Libgdx Bug (hope not)  (Read 3780 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2012-01-28 15:14:15 »

I have this (sprites.png)

also have this
1  
2  
3  
Texture sprites = new Texture(Gdx.files.internal("res/sprites.png"));
textures.put("ship", new TextureRegion(sprites, 0, 0, 80, 42));
textures.put("missile", new TextureRegion(sprites, 0, 50, 32, 16));

then why I get this?


Grin
It should be drawing a ship (corret) and a missile, the red one right below ship on sprite map, but the missile won't. "textures" is just a HashMap. I draw them normally with SpriteBatch. Any tips?

Offline cylab

JGO Ninja


Medals: 52



« Reply #1 - Posted 2012-01-28 22:11:16 »

I bet TextureRegion does take the coordinates of both rectangle corners and not top left corner + width and height. At the moment you request a region with negative height (16-50=-34), so you get the region 0,16,32,50 but with the y-coordinates flipped.

To get the correct region for the missile you need to use
1  
textures.put("missile", new TextureRegion(sprites, 0, 50, 32, 66)); // 50 + 16


Mathias - I Know What [you] Did Last Summer!
Offline ReBirth
« Reply #2 - Posted 2012-01-29 03:40:42 »

Gonna try that Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline AurelienRibon

Senior Newbie


Medals: 3


Toolmaker, yeah.


« Reply #3 - Posted 2012-01-29 07:03:03 »

Well, not really...

From TextureRegion.java:
1  
2  
3  
4  
5  
6  
        /** @param width The width of the texture region. May be negative to flip the sprite when drawn.
         * @param height The height of the texture region. May be negative to flip the sprite when drawn. */

        public TextureRegion (Texture texture, int x, int y, int width, int height) {
                this.texture = texture;
                setRegion(x, y, width, height);
        }


I work a lot with TextureRegions, and never saw a single bug. What version do you use: a nighlies or a stable?
There are 2 constructors with such arguments, the second one takes floats instead of ints, as u/v/u2/v2 texture coordinates, so be careful to give ints if you want width/height. Just in case you changed your code before putting it here...

Offline UprightPath
« Reply #4 - Posted 2012-01-29 08:33:11 »

Also, it might help to check the code you're using to draw it with. That seems more likely to be the culprit than what you're using to load it up.

I'm not familiar with the library, but it seems to me that you might be calling a method that changes the sprite's region without intending to.

Offline ReBirth
« Reply #5 - Posted 2012-01-29 23:26:53 »

Problem fixed Smiley

three solutions above, all great. But the correct one is UprightPath's. So I always thought when we construct a TextureRegion, the region was cropped right there. However when I looked the source, TextureRegion class is nothing but just (IMHO) holding link to Texture and 4 params (x, y, w, h). The 4 params have to mentioned again when you draw because I use SpriteBatch's drawing method that receive boolean to flip (this game is shooter, so need many flips).

Thanks guy!

Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2012-02-02 20:34:49 »

You might simplify your life by using Sprite (which is the tiniest bit faster too).

Offline ReBirth
« Reply #7 - Posted 2012-02-03 02:51:18 »

Okay I'll try it. What I like from TextureRegion is split method Smiley

Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #8 - Posted 2012-02-04 06:12:06 »

Okay I'll try it. What I like from TextureRegion is split method Smiley
TexturePacker is better. Smiley
http://code.google.com/p/libgdx/wiki/TexturePacker

Offline ReBirth
« Reply #9 - Posted 2012-02-04 07:05:13 »

Thank you very much. Fortunately I haven't changed the code yet Tongue However this class looks more complicated.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2012-02-05 03:34:37 »

Guess I'll stick back to TextureRegion, it's easier Grin

Offline pitbuller
« Reply #11 - Posted 2012-02-05 10:44:55 »

Sprites are just extended texture regions with utility. Lot simpler.
First you create sprite just like you would do texture region. Then you can set position, size, scale, rotation origin, color, etc.
Drawing is simple as
1  
sprite.draw(spriteBatch);

For textureRegion you have to do all those for yourself and usually that make cluttered rendering code(lots of hoops and branches) or you end up extending texture region and you find that you just made sprite0.2 without all those methods and untested code.
Offline ReBirth
« Reply #12 - Posted 2012-02-06 03:15:02 »

It's just because I want my entity class to do the logic and render itself according to it. Currently they update themself and draw themself with TextureRegion reference inside them. Ofc Sprite is worth to try.

Offline pitbuller
« Reply #13 - Posted 2012-02-06 06:44:29 »

It's just because I want my entity class to do the logic and render itself according to it. Currently they update themself and draw themself with TextureRegion reference inside them. Ofc Sprite is worth to try.
Then just put the sprite reference inside of that entity.That also give nice logic - render separation.
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #14 - Posted 2012-02-06 15:12:51 »

It's just because I want my entity class to do the logic and render itself according to it. Currently they update themself and draw themself with TextureRegion reference inside them. Ofc Sprite is worth to try.
Then just put the sprite reference inside of that entity.That also give nice logic - render separation.
Sprite merges model (position, rotation, etc) and view (drawing), so if you care about that, it isn't a good choice. If ReBirth has an entity that holds model data and draws itself, Sprite seems like it would be fine.

Offline ReBirth
« Reply #15 - Posted 2012-02-08 02:51:05 »

Is sprite also good when I use shifting, ie for animation by changing origin X and Y of main spritemap?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (45 views)
2014-10-17 03:59:02

Norakomi (36 views)
2014-10-16 15:22:06

Norakomi (27 views)
2014-10-16 15:20:20

lcass (31 views)
2014-10-15 16:18:58

TehJavaDev (61 views)
2014-10-14 00:39:48

TehJavaDev (61 views)
2014-10-14 00:35:47

TehJavaDev (51 views)
2014-10-14 00:32:37

BurntPizza (69 views)
2014-10-11 23:24:42

BurntPizza (39 views)
2014-10-11 23:10:45

BurntPizza (81 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!