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 (568)
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  
  Getting a variable from an extended class [Solved]  (Read 2069 times)
0 Members and 1 Guest are viewing this topic.
Offline Quarry
« Posted 2012-03-11 10:47:33 »

So I have this array which contains UIElements (obviously);

1  
public UIElement elements[] = {...}


and it contains classes that extend UIElements so that it's easier for me to add and remove stuff;

1  
2  
3  
4  
5  
public UIElement elements[] = {new Button("Back", 6, 104, 0), new SmallButton('<', 168, 32, 1), new SmallButton('>', 216, 32, 2), new SmallButton('C', 224, 64, 3),
        new Button("Embark", 186, 104, 4), new Textbox("Wayne Smith", 32, 32, 5), new Label("Name", 8, 32), new Label("Race", 8, 48), new Label("Gender", 8, 64),
        new Label("Clothing", 136, 48), new Label("Hair", 136, 64), new ArrayButton(new String[]{"Foreigner", "Native"}, 40, 48, 6),
        new ArrayButton(new String[]{"Male", "Female"}, 40, 64, 7), new ArrayButton(new String[]{"Dungaree", "Jacket", "Loincloth", "F.Loincloth"}, 173, 48, 8),
        new ArrayButton(new String[]{"Style A", "Style B"}, 173, 64, 9)};


For example; ArrayButtons store an array, an integer value and a scroll function (that changes that integer and sets the string on the button to show that element of the array)

HOWEVER, the problem is that I cannot simply;

1  
2  
3  
4  
5  
player.race = elements[6].selection;
player.gender = elements[7].selection;
player.clothX = elements[8].selection / 2;
player.clothY = elements[9].selection % 2;
player.hairX = elements[10].selection;


Because the array contains UIElements, not ArrayButtons and I can't access a variable that only exists in an ArrayButton

What can I do?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-03-11 11:07:44 »

You'll have to show us more code, particularly the class which creates the ui elements and it's surrounding context. Post the whole class if it's not too big.

Usually if one class creates an object of a particular type (eg. Button) and later needs to access specific info from that class (eg. Button.seletion) then you need to hold onto it as a reference to the specific type (eg. Button no UIElement).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Quarry
« Reply #2 - Posted 2012-03-11 11:10:12 »

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  
package tumbleweed.ui.elements;

import org.newdawn.slick.Image;
import tumbleweed.Main;

public class UIElement {

    public int id;
    public float x, y;
    public int width, height;
    public String string;
    public Image image;
    public boolean isHovered;

    public void init() {
    }

    public void render() {
    }

    public void checkHovered(int x, int y) {
        if (x >= this.x * Main.SCALE && x <= this.x * Main.SCALE + width * Main.SCALE && y >= this.y * Main.SCALE && y <= this.y * Main.SCALE + height * Main.SCALE) {
            setHovered(true);
        } else {
            setHovered(false);
        }
    }

    public void setHovered(boolean value) {
        isHovered = value;
    }
}

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  
package tumbleweed.ui.elements;

import org.newdawn.slick.Color;
import tumbleweed.ui.Screen;

public class ArrayButton extends UIElement {

    public String options[];
    public int selection;

    public ArrayButton(String options[], float x, float y, int id) {
        this.options = options;
        this.x = x;
        this.y = y;
        this.id = id;
    }
   
    public void init() {
        image = Screen.button;
        width = image.getWidth();
        height = image.getHeight();
    }
   
    public void render() {
        Color color = isHovered ? Palette.LABEL_YELLOW : Palette.LABEL_WHITE;

        Screen.drawImage(image, x, y, 1);

        Screen.drawFont(options[selection], x + (12 - options[selection].length()) * 2, y + (height - 4) / 2, color);
    }

    public void scrollOption(boolean forward) {
        selection = forward ? selection + 1 : selection - 1;

        if (selection >= options.length) {
            selection = 0;
        } else if (selection < 0) {
            selection = options.length - 1;
        }

        string = options[selection];
    }
}


I used to have tons of arrays for each object but it turned out to be a huge mess and I tried doing this but it really doesn't help me at the moment
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stranger

Senior Member


Medals: 6



« Reply #3 - Posted 2012-03-11 11:27:17 »

You can do something like this:
1  
2  
3  
4  
UIElement el = elements[index];
if (el instanceof  ArrayButton){
    ((ArrayButton)el).<ArrayButtonVar>
}

Anton
Offline Quarry
« Reply #4 - Posted 2012-03-11 11:33:55 »

1  
((ArrayButton)el).<ArrayButtonVar>


Was the part I needed that you aaaaalllot
Offline Quarry
« Reply #5 - Posted 2012-03-11 11:39:41 »

Nope,
1  
((ArrayButton)elements[6]).<selection>


didn't work, getting identifier expected error from Netbeans
Offline Danny02
« Reply #6 - Posted 2012-03-11 11:56:16 »

kill it with fire

-casts should nearly never be used
-why do u use an array, when you want to access single elements

I only tell you what you did wrong in the last post, when you swear to not use it^^
((ArrayButton)elements[6]).selection

Sry but i don't have time atm for longer examples and explanations
Offline Quarry
« Reply #7 - Posted 2012-03-11 11:59:13 »

I use an array to draw all GUI elements without hassle if I add / remove elements

((ArrayButton)elements[6]).selection worked anyways
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2012-03-11 12:02:07 »

Casting is the wrong solution here.

Show us the code with 'public UIElement elements[] =' in it. At a guess, you probably want some kind of UI object that holds a List<UIElement> that handles updating and drawing, and elsewhere you want to hang on to a specific Button instance that you then ask for information from.

Also consider making your class variables private and not having everything public.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Quarry
« Reply #9 - Posted 2012-03-11 12:02:58 »

Why is it a wrong solution?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2012-03-11 12:05:35 »

Because it's fragile and prone to breaking (via a ClassCastException) if/when you change the button into something else.

You're also duplicating information (you're saying that element[6] is a Button both when you're creating it and when you access it). Google 'don't repeat yourself'.

It's also completely unnessesary with just a small restructuring.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline lhkbob

JGO Knight


Medals: 32



« Reply #11 - Posted 2012-03-11 17:30:48 »

Nope,
1  
((ArrayButton)elements[6]).<selection>


didn't work, getting identifier expected error from Netbeans

I'm not going to join the debate on casting or not casting, but did you actually type the < and > as part of your variable name? Because that definitely won't work if your variable is just named selection.  Try removing those.

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 (40 views)
2014-09-24 16:13:29

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

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

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

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

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

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

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

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

BurntPizza (55 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!