Having trouble with keys for a start menu on a game program
up vote
-1
down vote
favorite
I am following a tutorial on how to make a platformer game and it got to the part where I am making a main menu for the game. I put:
addKeyListener(this);
and
setFocusable(true);
in the "GamePanel.java" class. I made a class for the menu called "MenuState.java" which is:
package pratformturtlestates;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import pratformturtle.GamePanel;
public class MenuState extends GameState
private String options = "Start", "Help", "Quit";
private int currentSelection = 0;
protected MenuState(GameStateManager gsm)
super(gsm);
public void init()
public void tick()
public void draw(Graphics g)
g.setColor(new Color(125, 20, 200));
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
for(int i = 0; i < options.length; i++)
if(i == currentSelection)
g.setColor(Color.GREEN);
else
g.setColor(Color.WHITE);
//g.drawLine(GamePanel.WIDTH / 2, 0, GamePanel.WIDTH / 2, GamePanel.HEIGHT);
g.setFont(new Font("Arial", Font.PLAIN, 72));
g.drawString(options[i], GamePanel.WIDTH / 2 - 75, 100 + i * 200);
public void keyPressed(int k)
if(k == KeyEvent.VK_DOWN)
currentSelection++;
if(currentSelection >= options.length)
currentSelection = 0;
else if(k == KeyEvent.VK_UP)
currentSelection--;
if(currentSelection < 0)
currentSelection = options.length - 1;
if(k == KeyEvent.VK_ENTER)
if(currentSelection == 0)
gsm.states.push(new Level1State(gsm));
else if(currentSelection == 1)
else if(currentSelection == 2)
System.exit(0);
public void keyReleased(int k)
Whenever I run it the menu comes up with the purpleish background color and "start", "help", and "quit" but nothing happens when I hit the arrow keys or the enter key.
java
add a comment |
up vote
-1
down vote
favorite
I am following a tutorial on how to make a platformer game and it got to the part where I am making a main menu for the game. I put:
addKeyListener(this);
and
setFocusable(true);
in the "GamePanel.java" class. I made a class for the menu called "MenuState.java" which is:
package pratformturtlestates;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import pratformturtle.GamePanel;
public class MenuState extends GameState
private String options = "Start", "Help", "Quit";
private int currentSelection = 0;
protected MenuState(GameStateManager gsm)
super(gsm);
public void init()
public void tick()
public void draw(Graphics g)
g.setColor(new Color(125, 20, 200));
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
for(int i = 0; i < options.length; i++)
if(i == currentSelection)
g.setColor(Color.GREEN);
else
g.setColor(Color.WHITE);
//g.drawLine(GamePanel.WIDTH / 2, 0, GamePanel.WIDTH / 2, GamePanel.HEIGHT);
g.setFont(new Font("Arial", Font.PLAIN, 72));
g.drawString(options[i], GamePanel.WIDTH / 2 - 75, 100 + i * 200);
public void keyPressed(int k)
if(k == KeyEvent.VK_DOWN)
currentSelection++;
if(currentSelection >= options.length)
currentSelection = 0;
else if(k == KeyEvent.VK_UP)
currentSelection--;
if(currentSelection < 0)
currentSelection = options.length - 1;
if(k == KeyEvent.VK_ENTER)
if(currentSelection == 0)
gsm.states.push(new Level1State(gsm));
else if(currentSelection == 1)
else if(currentSelection == 2)
System.exit(0);
public void keyReleased(int k)
Whenever I run it the menu comes up with the purpleish background color and "start", "help", and "quit" but nothing happens when I hit the arrow keys or the enter key.
java
How canaddKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method,public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be aKeyEvent
object, not an int.
– Hovercraft Full Of Eels
Nov 11 at 5:00
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am following a tutorial on how to make a platformer game and it got to the part where I am making a main menu for the game. I put:
addKeyListener(this);
and
setFocusable(true);
in the "GamePanel.java" class. I made a class for the menu called "MenuState.java" which is:
package pratformturtlestates;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import pratformturtle.GamePanel;
public class MenuState extends GameState
private String options = "Start", "Help", "Quit";
private int currentSelection = 0;
protected MenuState(GameStateManager gsm)
super(gsm);
public void init()
public void tick()
public void draw(Graphics g)
g.setColor(new Color(125, 20, 200));
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
for(int i = 0; i < options.length; i++)
if(i == currentSelection)
g.setColor(Color.GREEN);
else
g.setColor(Color.WHITE);
//g.drawLine(GamePanel.WIDTH / 2, 0, GamePanel.WIDTH / 2, GamePanel.HEIGHT);
g.setFont(new Font("Arial", Font.PLAIN, 72));
g.drawString(options[i], GamePanel.WIDTH / 2 - 75, 100 + i * 200);
public void keyPressed(int k)
if(k == KeyEvent.VK_DOWN)
currentSelection++;
if(currentSelection >= options.length)
currentSelection = 0;
else if(k == KeyEvent.VK_UP)
currentSelection--;
if(currentSelection < 0)
currentSelection = options.length - 1;
if(k == KeyEvent.VK_ENTER)
if(currentSelection == 0)
gsm.states.push(new Level1State(gsm));
else if(currentSelection == 1)
else if(currentSelection == 2)
System.exit(0);
public void keyReleased(int k)
Whenever I run it the menu comes up with the purpleish background color and "start", "help", and "quit" but nothing happens when I hit the arrow keys or the enter key.
java
I am following a tutorial on how to make a platformer game and it got to the part where I am making a main menu for the game. I put:
addKeyListener(this);
and
setFocusable(true);
in the "GamePanel.java" class. I made a class for the menu called "MenuState.java" which is:
package pratformturtlestates;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import pratformturtle.GamePanel;
public class MenuState extends GameState
private String options = "Start", "Help", "Quit";
private int currentSelection = 0;
protected MenuState(GameStateManager gsm)
super(gsm);
public void init()
public void tick()
public void draw(Graphics g)
g.setColor(new Color(125, 20, 200));
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
for(int i = 0; i < options.length; i++)
if(i == currentSelection)
g.setColor(Color.GREEN);
else
g.setColor(Color.WHITE);
//g.drawLine(GamePanel.WIDTH / 2, 0, GamePanel.WIDTH / 2, GamePanel.HEIGHT);
g.setFont(new Font("Arial", Font.PLAIN, 72));
g.drawString(options[i], GamePanel.WIDTH / 2 - 75, 100 + i * 200);
public void keyPressed(int k)
if(k == KeyEvent.VK_DOWN)
currentSelection++;
if(currentSelection >= options.length)
currentSelection = 0;
else if(k == KeyEvent.VK_UP)
currentSelection--;
if(currentSelection < 0)
currentSelection = options.length - 1;
if(k == KeyEvent.VK_ENTER)
if(currentSelection == 0)
gsm.states.push(new Level1State(gsm));
else if(currentSelection == 1)
else if(currentSelection == 2)
System.exit(0);
public void keyReleased(int k)
Whenever I run it the menu comes up with the purpleish background color and "start", "help", and "quit" but nothing happens when I hit the arrow keys or the enter key.
java
java
edited Nov 11 at 5:23
Hovercraft Full Of Eels
260k20210316
260k20210316
asked Nov 11 at 4:56
LKDearing
11
11
How canaddKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method,public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be aKeyEvent
object, not an int.
– Hovercraft Full Of Eels
Nov 11 at 5:00
add a comment |
How canaddKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method,public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be aKeyEvent
object, not an int.
– Hovercraft Full Of Eels
Nov 11 at 5:00
How can
addKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method, public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be a KeyEvent
object, not an int.– Hovercraft Full Of Eels
Nov 11 at 5:00
How can
addKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method, public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be a KeyEvent
object, not an int.– Hovercraft Full Of Eels
Nov 11 at 5:00
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245968%2fhaving-trouble-with-keys-for-a-start-menu-on-a-game-program%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How can
addKeyListener(this);
compile when your class does not implement the KeyListener interface? Also your keyPressed method,public void keyPressed(int k) {
couldn't work since it is not a true override of KeyListener. The parameter should be aKeyEvent
object, not an int.– Hovercraft Full Of Eels
Nov 11 at 5:00