Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My Menu has a START and a QUIT option. I'm using a controller's D-PAD for selection. The game begins with START highlighted. When I push the down button on the D-PAD, it highlights the QUIT option. Problem is when I release the down button, it returns to highlighting START.

Is there a way to possibly hold the position so that if I highlight QUIT, it will stay highlighted after releasing the button?

//First starts with START highlighted:

if(menuSelect == 1 && GC.getDPADDir() == 7){ // if QUIT is highlighted, push up
        menuSelect++;
    }
    else if(GC.isButtonPressed(1)){
        // go to game
        sbg.enterState(1);
    }

if(menuSelect == 2 && GC.getDPADDir() == 1){ // if start is highlighted, push down
        menuSelect--;       
    }
    else if(GC.isButtonPressed(1)){
        // quit
        System.exit(0);
    }
share|improve this question
    
You do realise that the second else if is never reached. Because if the first if fails ( because no direction is hit --> GC.getDPADDir() ) and you hit a button it will always be sbg.entereState(1). –  Sidar Aug 19 '12 at 11:46

1 Answer 1

up vote 2 down vote accepted

Not exactly what you are looking for but perhaps you could try it like so:

private int maxMenuItems  = 2;
private int menuSelect  = 1;

-Check for direction first

if(GC.getDPADDir() == 7){
        menuSelect++;
        if(menuSelect > maxMenuItems) menuSelect  = maxMenuItems; 
    }
else if(GC.getDPADDir() == 1){ 
        menuSelect--;
        if(menuSelect < 1) menuSelect = 1;
    }

-Then loop over the button press check

if(GC.isButtonPressed(1)){
   switch(menuSelect){
       case 1:
       sbg.enterState(1);
       break;
       case 2:
       System.exit(0);
       break;
   }
}

I havn't done much with menus and what not. But I think your flaw is within your if else "cluster". It would also be easier to use a base of 0. You might want to iterate over your items or reference them by index if you were to put them in an array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.