Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 proper value from an array at specified index  (Read 996 times)
0 Members and 1 Guest are viewing this topic.
Offline nellykvist

Senior Newbie





« Posted 2013-09-21 03:04:38 »

I'm creating a game in Slick2D. First, let me show you the GUI state where I'm stuck.
http://imageshack.us/photo/my-images/593/4v3v.jpg/
So, I have a problem in a class SettingsUI, with increasing/decreasingValue methods, whenever I call increase/decrease method, to choose value from an array, what happens when I press increase arrow button, instead of giving me (i+1) value, which is a value {1000, 700} width/height, loop iterates to the last value which is {1400, 900}. Same happens in reversed, when I'm decreasing it goes to the zero element, which is by default {800, 600}. And what I have found out (by printing values), when I click on that arrow button, it prints like 30 times that value from just ONE click. I have tried to add a statement break, when I increase/decrease, inside a loop, but doesn't work. So could you tell me, how can I get this to work properly, what should I add/change?

SettingsUI.class
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  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
public class SettingsUI implements UIConstants {

    private PText menuTitle;
    private PText optionTitle;

    private static final int VIDEO_ROWS = 3;
    private static final int AUDIO_ROWS = 2;
    private static final int TOTAL_COLS = 2;

    private PButton[][] videoButtons = new PButton[VIDEO_ROWS][TOTAL_COLS];
    private PButton[][] audioButtons = new PButton[AUDIO_ROWS][TOTAL_COLS];

    private static final int BLOCK_X_SPACING = 40;
    private static final int BLOCK_Y_SPACING = 50;

    private static final String ARROW_LEFT_RES = "res/gui/button_arrow_left.png";
    private static final String ARROW_RIGHT_RES = "res/gui/button_arrow_right.png";

    public SettingsUI() throws SlickException {
        initComponents();
    }

    private void initComponents() throws SlickException {
        this.menuTitle = new PText(BASE_COLOR, 34, false);
        this.optionTitle = new PText(BASE_COLOR, 22, false);

        for (int row = 0; row < VIDEO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                switch (col) {
                    case 0:
                        videoButtons[row][col] = new PButton(ARROW_LEFT_RES, null);
                        continue;
                    case 1:
                        videoButtons[row][col] = new PButton(ARROW_RIGHT_RES, null);
                        continue;
                }
            }
        }
        for (int row = 0; row < AUDIO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                switch (col) {
                    case 0:
                        audioButtons[row][col] = new PButton(ARROW_LEFT_RES, null);
                        continue;
                    case 1:
                        audioButtons[row][col] = new PButton(ARROW_RIGHT_RES, null);
                        continue;
                }
            }
        }
    }

    public void update(GameContainer container, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        // case width/height
       if (videoButtons[0][0].contains(mx, my)) {
            if (videoButtons[0][0].isButtonPressed(input)) {
                decreaseValue();
            }
        } else if (videoButtons[0][1].contains(mx, my)) {
            if (videoButtons[0][1].isButtonPressed(input)) {
                increaseValue();
            }
        }
        /*
        for (int row = 0; row < VIDEO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                if (videoButtons[row][col].contains(mx, my)) {
                    if (!videoButtons[row][col].isButtonPressed(input)) continue;
                    System.out.println("row: " + row + " / col: " + col);
                    switch (col) {
                        case 0:
                            decreaseValue();
                            break;
                        case 1:
                            increaseValue();
                            break;
                    }
                }
            }
        }

        for (int row = 0; row < AUDIO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                if (audioButtons[row][col].contains(mx, my)) {
                    if (!audioButtons[row][col].isButtonPressed(input)) continue;
                    switch (col) {
                        case 0:
                            break;
                        case 1:
                            break;
                    }
                }
            }
        }
        */

    }

    // temp
   private static final int[][] screen_size = {
            {800, 600},
            {1000, 700},
            {1200, 800},
            {1400, 900}
    };
    private void increaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i < screen_size.length - 1) {
                    Settings.Video.setWidth(screen_size[i+1][0]);
                    Settings.Video.setHeight(screen_size[i+1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());

    }
    private void decreaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i > 0) {
                    Settings.Video.setWidth(screen_size[i-1][0]);
                    Settings.Video.setHeight(screen_size[i-1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());
    }

    // temp
   private static String[][] VIDEO_ORDER = {
            {"FRAME SIZE", String.valueOf(Settings.Video.getWidth() + " x " + Settings.Video.getHeight())},
            {"FULL SCREEN", String.valueOf(Settings.Video.isFullscreen())},
            {"FPS", String.valueOf(Settings.Video.isFps())}
    };
    private static String[][] AUDIO_ORDER = {
            {"TURN SOUND", String.valueOf(Settings.Audio.isSound())},
            {"VOLUME", String.valueOf(Settings.Audio.getVolume())}
    };

    public void render(GameContainer container, Graphics g) throws SlickException {
        int x = CONTAINER_CENTER_X / 2 - 15;
        int y = CONTAINER_MAX_Y / 6 + 40;

        menuTitle.renderText("VIDEO:", g, x, y);
        for (int row = 0; row < VIDEO_ROWS; row++) {
            renderBlock(g, optionTitle, videoButtons, VIDEO_ORDER[row][0], VIDEO_ORDER[row][1], x, y, row);
        }

        x = CONTAINER_CENTER_X / 2 - 15;
        y = CONTAINER_CENTER_Y + 40;

        menuTitle.renderText("AUDIO:", g, x, y);
        for (int row = 0; row < AUDIO_ROWS; row++) {
            renderBlock(g, optionTitle, audioButtons, AUDIO_ORDER[row][0], AUDIO_ORDER[row][1], x, y, row);
        }
    }

    private void renderBlock(Graphics g, PText optionTitle, PButton[][] buttons, String option, String value, int x, int y, int row) throws SlickException {
        x += BLOCK_X_SPACING;
        y += BLOCK_Y_SPACING;

        optionTitle.renderText(option, g, x, y + (BLOCK_Y_SPACING * row));

        for (int col = 0; col < TOTAL_COLS; col++) {
            int bx = x + x - BLOCK_X_SPACING + (BLOCK_X_SPACING * col + (x * col));
            int by = y - 10 + (row * BLOCK_Y_SPACING);

            buttons[row][col].renderImage(g, bx, by);
        }

        x = x + x + BLOCK_X_SPACING + 25;
        y = y + (BLOCK_Y_SPACING * row);

        optionTitle.renderText(value, g, x, y);
    }

}


SettingsScreenState.class
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  
public class SettingsScreenState extends BasicGameState implements UIConstants {

    public static final int ID = 5;
    private SettingsUI settingsUI;
    private Image titleImage;
    private PButton backButton;

    @Override
    public int getID() {
        return ID;
    }

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        this.settingsUI = new SettingsUI();
        this.titleImage = new Image("res/gui/title_settings.png");
        this.backButton = new PButton("res/gui/button_back.png", "res/gui/button_back_hover.png");
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        RandomUtility.bgTheme(container, g);

        g.drawImage(titleImage, 50, 50);
        this.settingsUI.render(container, g);
        this.backButton.renderImage(g, CONTAINER_CENTER_X - backButton.getWidth() / 2, CONTAINER_MAX_Y - backButton.getHeight() - 30);
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        this.settingsUI.update(container, delta);

        if (backButton.contains(mx, my)) {
            if (backButton.isButtonPressed(input)) {
                game.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
            }
        }
    }

}


PButton.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
ublic boolean contains(int x, int y) throws SlickException {
    int minX = this.x;
    int minY = this.y;
    int maxX = this.x + image.getWidth();
    int maxY = this.y + image.getHeight();

    if ((x > minX && x < maxX) && (y > minY && y < maxY)) {
        if (hoverImage != null) {
            image = new Image(hoverImage);
        }
        return true;
    }
    image = new Image(normalImage);
    return false;
}

public boolean isButtonPressed(Input input) throws SlickException {
    return input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
}


So, this is the code where is a problem:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void update(GameContainer container, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        // case width/height
       if (videoButtons[0][0].contains(mx, my)) {
            if (videoButtons[0][0].isButtonPressed(input)) {
                decreaseValue();
            }
        } else if (videoButtons[0][1].contains(mx, my)) {
            if (videoButtons[0][1].isButtonPressed(input)) {
                increaseValue();
            }
        }


And this are methods, that I'm using to increase/decrease 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  
private static final int[][] screen_size = {
            {800, 600},
            {1000, 700},
            {1200, 800},
            {1400, 900}
    };
    private void increaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i < screen_size.length - 1) {
                    Settings.Video.setWidth(screen_size[i+1][0]);
                    Settings.Video.setHeight(screen_size[i+1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());

    }
    private void decreaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i > 0) {
                    Settings.Video.setWidth(screen_size[i-1][0]);
                    Settings.Video.setHeight(screen_size[i-1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());
    }
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-09-21 04:10:53 »

You are using isMouseButtonDown, instead of isMouseButtonPressed in your PButton class.
Offline nellykvist

Senior Newbie





« Reply #2 - Posted 2013-09-21 10:11:25 »

Omg... I was so blind.... Thank you alot my man.  Smiley
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.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!