Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 682 times)
0 Members and 1 Guest are viewing this topic.
Offline nellykvist

Senior Newbie





« Posted 2013-09-24 14: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 Member


Medals: 3



« Reply #1 - Posted 2013-09-25 08: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.

xsi3rr4x (76 views)
2014-04-15 18:08:23

BurntPizza (69 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!