我有一个游戏代码,可以构建一个框架并将其设置为可见和内容,并将contentPane
设置为我创建的GamePanel
类。这会创建一个gamestatemanager
,然后从状态0或菜单状态开始。这是Menu State类的代码。持续发生的事情是在显示菜单之前,会弹出一个弹出框,该弹出框是选择编号1的init()
的一部分,就像使用回车键和select()
方法选择它一样,我没有能够找出原因。
MenuState.java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class MenuState extends GameState {
private Background bg;
private int choice = 0;
private String[] options = { "Slot Machine", "Dice Toss", "Roulette Wheel", "Stats", "Leaderboards", "Quit" };
private Color titleColor;
private Font titleFont;
private Font font;
public MenuState(GameStateManager gsm) {
this.gsm = gsm;
try {
bg = new Background("/Backgrounds/menubg.png", 1);
bg.setVector(-0.1, 0);
titleColor = Color.BLACK;
titleFont = new Font("Georgia", Font.PLAIN, 28);
} catch (Exception e) {
e.printStackTrace();
}
font = new Font("Arial", Font.PLAIN, 12);
}
@Override
public void init() {
}
@Override
public void update() {
bg.update();
}
@Override
public void draw(Graphics2D g) {
// Draw BG
bg.draw(g);
// Draw title
g.setColor(titleColor);
g.setFont(titleFont);
g.drawString("Jolly Jackpot Land!", 36, 60);
g.setFont(font);
for (int i = 0; i < options.length; i++) {
if (i == choice)
g.setColor(Color.RED);
else
g.setColor(Color.WHITE);
g.drawString(options[i], 40, 130 + i * 15);
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 10));
g.drawString("v1.0.2.24.16 (c) Matthew Young", 165, 235);
}
private void select() {
if (choice == 0) {
// Slots
gsm.setState(GameStateManager.SLOTGAMESTATE);
}
if (choice == 1) {
// Dice
gsm.setState(GameStateManager.DICEGAMESTATE);
}
if (choice == 2) {
gsm.setState(GameStateManager.ROULETTEGAMESTATE);
}
if (choice == 3) {
gsm.setState(GameStateManager.STATSTATE);
}
if (choice == 4) {
gsm.setState(GameStateManager.HELPSTATE);
}
if (choice == 5) {
System.exit(0);
}
}
@Override
public void keyPressed(int k) {
if (k == KeyEvent.VK_ENTER) {
select();
}
if (k == KeyEvent.VK_UP) {
choice--;
if (choice == -1) {
choice = options.length - 1;
}
}
if (k == KeyEvent.VK_DOWN) {
choice++;
if (choice == options.length) {
choice = 0;
}
}
}
@Override
public void keyReleased(int k) {
}
}
GameStateManager.java
import java.util.ArrayList;
public class GameStateManager {
private ArrayList<GameState> gameStates;
private int currentState;
public static final int MENUSTATE = 0;
public static final int SLOTGAMESTATE = 1;
public static final int DICEGAMESTATE = 2;
public static final int ROULETTEGAMESTATE = 3;
public static final int STATSTATE = 4;
public static final int HELPSTATE = 5;
public GameStateManager() {
gameStates = new ArrayList<GameState>();
currentState = 0;
gameStates.add(new MenuState(this));
gameStates.add(new SlotGameState(this));
gameStates.add(new DiceGameState(this));
gameStates.add(new RouletteGameState(this));
gameStates.add(new HelpState(this));
}
public void setState(int state){
currentState = state;
gameStates.get(currentState).init();
currentState = 0;
}
public int getState() {
return currentState;
}
public void update() {
gameStates.get(currentState).init();
}
public void draw(java.awt.Graphics2D g){
gameStates.get(currentState).draw(g);
}
public void keyPressed(int k){
gameStates.get(currentState).keyPressed(k);
}
public void keyReleased(int k) {
gameStates.get(currentState).keyReleased(k);
}
}