Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Linking strings / textures together somehow  (Read 866 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-03-21 01:40:39 »

In my most recent WIP game I have ran into a little trouble which has got me thinking but my idea seems a little too complex. So here is what I want to do. After I open a menu and click the item I want to place, the menu closes and then I click where i want to place it and everything works great. Now here is the problem: How do I link each string (drawn in the menu so you know which one to click) with a (libgdx) texture (which is drawn once you click somewhere) Is there anyway to do this or would I have to just do something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
if(buildingStringIsClicked){
     closeTheMenu;
     batch.draw(twoStoryBuilding, 200, 200);
}
if(somethingElseStringIsClicked){
     closeTheMenu;
     batch.draw(someTexture, 200, 200);
}
//more if statements here for each string

Alternatively I could even create a method that takes a string and a texture as a parameter. The only reason I don't want to do this is there may be around 100 different placable items creating a lot of method calling or if statements. I am not actually you can link a string and a texture together but does anyone have any advice in a direction to go from here, or would this method above be the way to go? All input is appreciated Cheesy

Offline vbrain
« Reply #1 - Posted 2013-03-21 01:48:39 »

There are many ways of doing this. You could use a HashMap<String, Texture> and work with that.

Read up on hashmaps: here and here.
1  
2  
3  
4  
5  
6  
7  
Map<String, Texture> map = new HashMap<String, Texture>();

//put texture
m1.put("Option 1", textureObject);

//get texture
m1.get("Option 1");


Note however, that this method is probably inefficient, unnecessary, and a waste of memory.

You can also use Enums to do what you want.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-03-21 02:33:11 »

Since hash maps may be inefficient for this task, would that still be your first recommendation, or would u recommend enums first?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2013-03-21 05:40:39 »

Using a HashMap is faster than your current code anyway.

Myomyomyo.
Offline vbrain
« Reply #4 - Posted 2013-03-21 19:58:59 »

Since hash maps may be inefficient for this task, would that still be your first recommendation, or would u recommend enums first?

If you can't do it with enums, then use HashMaps.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #5 - Posted 2013-03-21 20:44:15 »

Awesome, I will look into it more this weekend thank you for the help and links!

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2013-03-28 01:03:54 »

I just remembered about this post and went to go try out these hash maps and they are surprisingly cool! I feel like such a nerd Cool

Offline Slyntax

Junior Member


Medals: 3



« Reply #7 - Posted 2013-03-28 05:04:06 »

Not sure if you would want to do this, or if it's even recommended by better programmers than myself but you can always declare your own class and extend Texture.

It would look like...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Texture2 extends Texture
{
    String str;
   
    public Texture2(FileHandle file, String str)
    {
        super(file);
        this.str = str;
    }
    //... More constructors here
   
    // You can also add get/set methods if you want
}


In the main code just change 'new Texture(blah)' to 'new Texture2(blah, string)'

Then it's always tied to that texture.

Edit: Updated the code example.
Offline HeroesGraveDev

JGO Kernel


Medals: 260
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2013-03-28 05:19:37 »

You will find that HashMaps are very useful for lots of things in game development.

Offline StumpyStrust
« Reply #9 - Posted 2013-03-28 07:41:22 »

I would not have it linked to a texture in the way you are trying. Have you object/entity/building have a texture it uses to draw itself with and then set that texture. I would have a hashmap like everyone says but create it once during start up probably when you load the resources. Heck, if you have a resource manager class, just use that.

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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!