Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
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  
  Is it possible to add variable values to texture name when calling for a draw?  (Read 3605 times)
0 Members and 1 Guest are viewing this topic.
Offline Norakomi
« Posted 2014-09-18 10:52:38 »

Hi,

I was wondering. Is it possible to add a variable to a texturename so when i draw an image the texture drawn is dependant of the value of the variable?
And how do I do that?

For example?
I want to draw a different texture dependant of my players nr. of lifes left.

Number of lifes is stored in the variable player_life

And i have different textures: player_ 01, player_02, player_03 etc

Now how would i do something like this:

draw(player_0[+player_life], x,y)


Or do I need an extra line of code to set which texture i want to use?

Offline trollwarrior1
« Reply #1 - Posted 2014-09-18 10:55:59 »

What rendering method are you using?
Offline Norakomi
« Reply #2 - Posted 2014-09-18 10:59:25 »

I'm drawing on a spritebatch (libgdx)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kingroka123
« Reply #3 - Posted 2014-09-18 11:40:39 »

You can do that same thing if you store the textures in a texture array called player_0
Offline Norakomi
« Reply #4 - Posted 2014-09-18 11:48:41 »

Yes. But then I still have to add and initialise an array somewhere plus i have to add all the different textures manually (I believe) to the different array positions.

If i want to do it like that & automate the proces i guess i should probably use textureRegion like this f.e.:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
say :
int player_life = 2;
 
& I have 3 textures (blocks1,blocks2,blocks3) stored in a TextureRegion[]

      blocks = new TextureRegion[3];
      for(int i = 0; i < blocks.length; i++) {
         blocks[i] = new TextureRegion(tex, 32 + i * 16, 0, 16, 16);
      }

....
i can draw 2nd texture by:

sb.draw(blocks[player_life], x, y);


But what I actually would like to know is if it is possible to call for different textures to be drawn the way i stated above.

Offline kingroka123
« Reply #5 - Posted 2014-09-18 11:56:11 »

I don't think so. But just for clarification, you are talking about having 3 separate texture variables which share all of the same letters in them except for one. You are then trying to stitch a number corresponding to the texture you want on the end of the variable tag when you call the texture for drawing. Basically treating it like a string?
Offline Norakomi
« Reply #6 - Posted 2014-09-18 12:05:55 »

Yes

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #7 - Posted 2014-09-18 12:23:39 »

You're just creating a sprite sheet, that's pretty normal usage of TextureRegions.
Note though that
blocks[player_life]
is the 3rd texture, not 2nd. (2 is the 3rd element, 1 would be 2nd)

If you ever want to do animation, sprite sheets are often used for that as well: https://github.com/libgdx/libgdx/wiki/2D-Animation
Offline Norakomi
« Reply #8 - Posted 2014-09-18 12:29:31 »

blocks[2] is indeed the third...my bad ;p

But then.... so I guess it's really not possible to stich a variable to a string to create the specific texture's name you want to call within a draw call the way i would like to?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #9 - Posted 2014-09-18 12:44:18 »

Well, if you wanted to use a literal
String
, you have to have some method to "parse" it, hence why it will always be less efficient (and shoddy) than just an indexed table.

It could be simple:
1  
2  
3  
Map<String, TextureRegion> texMap;

draw(texMap.get("player_0" + playerLife));


Of course this is stupid.

Using actual variables, and no Strings, just using a different form of table:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
TextureRegion player_01, player_02, player_03;

TextureRegion toUse = null;

switch (playerLife) {
    case 1:
        toUse = player_01;
        break;
    case 2:
        toUse = player_02;
        break;
    case 3:
        toUse = player_03;
        break;
}

draw(toUse);


I think is close to what you meant, but there are no macros or similar in java to generate this code for you. I hope you see that this is still quite unnecessarily painful.
The equivalent is this as shown earlier:

1  
2  
3  
TextureRegion[] textures;

draw(textures[playerLife]); // or if a more complicated mapping is required: textures[mapFunc(playerLife)]


The real question: why would you have separate variables to begin with? Ideally you have an array (or List, etc) from the start, and this whole thing becomes a non-issue.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Norakomi
« Reply #10 - Posted 2014-09-19 08:42:00 »

Much appreciated BP.

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (33 views)
2019-09-04 15:33:17

hadezbladez (3979 views)
2018-11-16 13:46:03

hadezbladez (1443 views)
2018-11-16 13:41:33

hadezbladez (3978 views)
2018-11-16 13:35:35

hadezbladez (768 views)
2018-11-16 13:32:03

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

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

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

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

nelsongames (3840 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!