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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:23









Hovercraft Full Of Eels

260k20209316




260k20209316










asked Nov 11 at 4:56









LKDearing

11




11












  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python