Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  problem with code  (Read 965 times)
0 Members and 1 Guest are viewing this topic.
Offline nellykvist

Senior Newbie





« Posted 2013-09-24 12:44:14 »

I got a problem in my code, which is about button functionality, and when I tried to implement a working function from a test method into a code mechanism, for some reason logic gets reversed.

This is how this state looks
http://imageshack.us/photo/my-images/593/4v3v.jpg/

So, let me show you first a working function which is below, to introduce you what's going on. So what this simple function does, when you press the button, it increases or decreases for one value, and if that click is on the button down and if is like that for a 1 second, and if is still inside a bounds and still button down then we're extra faster increasing value with interval of 250ms. Here is the code:

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  
private final int timeDuration = 250;
private long pressedTime = timeDuration;
private long lastInvoke;
private boolean invoke;
/*
 * This metod is called in update() method
 */

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

    // this indicates on one of options, which is used as a parameter, when
    // decrease/increase method is invoked
    // for column at 0 index -> decrease
    // for column at 1 index -> increase
    // in this case 1-> volume option
    String option = SOUND_ORDER[1][0];

    boolean isButtonContains = audioButtons[1][0].contains(mx, my);
    boolean isButtonPressed = audioButtons[1][0].isButtonPressed(input);
    boolean isButtonDown = audioButtons[1][0].isButtonDown(input);  

    if (isButtonContains) {
        if (isButtonPressed) {
            invoke = false;
            lastInvoke = container.getTime();
            SettingAttributes.decreaseController(option);
        }
        if (pressedTime < timeDuration && !isButtonDown) {
            pressedTime = timeDuration;
        }
        if (lastInvoke + 1000 < container.getTime()) {
            invoke = true;
        }
        if (isButtonDown && invoke) {
            pressedTime -= delta;
            if (pressedTime <= 0 && isButtonDown) {
                pressedTime = timeDuration;
                SettingAttributes.decreaseController(option);
            }
        }
    }
}


However, normal code for increasing/decreasing works fine for all methods:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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;

            String option = VIDEO_ORDER[row][0];
            this.buttonInvokeController(option, col);
        }
    }
}

But when I try to implement above function on all buttons:
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  
public void update(GameContainer container, int delta) throws SlickException {
    Input input = container.getInput();
    int mx = input.getMouseX();
    int my = input.getMouseY();

    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;

            String option = VIDEO_ORDER[row][0];
            if (videoButtons[row][col].isButtonPressed(input)) {
                // this case is supposed that should invoke part of code
                // in else case
            } else {
                this.invoke = false;
                this.lastInvoke = container.getTime();
                this.buttonInvokeController(option, col);
                // case works
            }

            // all of the code below doesn't even invoke  
            if (this.pressedTime < timeDuration && !videoButtons[row][col].isButtonDown(input)) {
                this.pressedTime = timeDuration;
            }

            if (this.lastInvoke + 1000 < container.getTime()) {
                this.invoke = true;
            }

            if (videoButtons[row][col].isButtonDown(input) && this.invoke) {
                this.pressedTime -= delta;
                if (pressedTime <= 0 && videoButtons[row][col].isButtonDown(input)) {
                    this.pressedTime = timeDuration;
                    this.buttonInvokeController(option, col);
                }
            }
        }
    }
}



There's also below this video row options, a sound options, which is same code as above. And this are button methods:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public 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.isMousePressed(Input.MOUSE_LEFT_BUTTON);
    }

    public boolean isButtonDown(Input input) throws SlickException {
        return input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
    }
Offline Slyntax

Junior Devvie


Medals: 3



« Reply #1 - Posted 2013-09-25 06:47:38 »

I believe you want to change...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
String option = VIDEO_ORDER[row][0];
            if (videoButtons[row][col].isButtonPressed(input)) {
                // this case is supposed that should invoke part of code
                // in else case
            } else {
                this.invoke = false;
                this.lastInvoke = container.getTime();
                this.buttonInvokeController(option, col);
                // case works
            }


to

1  
2  
3  
4  
5  
if (videoButtons[row][col].isButtonPressed(input)) {
                this.invoke = false;
                this.lastInvoke = container.getTime();
                this.buttonInvokeController(option, col);
            }


I believe isButtonPressed only fires the first time it's pressed down. So the else statement was constantly resetting everything and always causing the code in your if statements to not run.
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (79 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!