Ahojte. Potrebujem pomoc s tlačítkom "Back" a "Try Again". Hráč keď dohrá vypíše mu to At least you tried + skóre. Ale keď chce hráč hrať znova tak mu to celý program zavrie. To isté keď si vyberie v menu tlačítko "Help". Je tam iba popísané ako sa to ovláda a to je všetko. Ale keď chce ísť hráč späť do hlavného menu a klikne na tlačítko "back" vypne to celý program. Trápim sa s tým už asi 3 dni a je to celkom depresívne. (Iba začínam s Javou). A pri všetkých "game.gameState" mi píše: "The static field Game.gameState should be accesed in static way.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import com.tutorial.main.Game.STATE;
public class Menu extends MouseAdapter{
private Game game;
private Handler handler;
private HUD hud;
private Random r = new Random();
public Menu(Game game, Handler handler, HUD hud){
this.game = game;
this.hud = hud;
this.handler = handler;
}
public void mousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
if(game.gameState == STATE.Menu){
//Play button
if(mouseOver(mx, my, 210, 150, 200, 64)){
game.gameState = STATE.Game;
handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
handler.clearEnemys();
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
}
//Help Button
if(mouseOver(mx, my, 210, 250, 200, 64)){
game.gameState = STATE.Help;
}
}
//Quit button
if(mouseOver(mx, my, 210, 350, 200 ,64)){
System.exit(0);
}
//back button for Help
if(game.gameState == STATE.Help) {
if(mouseOver(mx, my, 210, 350, 200, 64)) {
game.gameState = STATE.Menu;
return;
}
}
//back button for Help
if(game.gameState == STATE.Help) {
if(mouseOver(mx, my, 210, 350, 200, 64)) {
game.gameState = STATE.Menu;
hud.setLevel(1);
hud.setScore(0);
handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
handler.clearEnemys();
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
}
}
}
public void mouseReleased(MouseEvent e){
}
private boolean mouseOver(int mx, int my, int x, int y, int width, int height){
if(mx > x && mx < x + width){
if(my > y && my < y + height){
return true;
}else return false;
}else return false;
}
public void tick(){
}
public void render (Graphics g){
if(game.gameState == STATE.Menu){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 40);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Wave", 250, 50);
g.setFont(fnt2);
g.drawRect(210, 150, 200, 64);
g.drawString("Play", 270, 190);
g.drawRect(210, 250, 200, 64);
g.drawString("Help", 270, 290);
g.drawRect(210, 350, 200, 64);
g.drawString("Quit", 271, 390);
}else if(game.gameState == STATE.Help){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 40);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Help", 250, 50);
g.setFont(fnt3);
g.drawString("Use WASD keys to move player and dodge enemies", 80, 230);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Back", 255, 395);
}else if(game.gameState == STATE.End){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 40);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Game Over", 180, 50);
g.setFont(fnt3);
g.drawString("At least you tried: " + hud.getScore(), 80, 230);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Try Again", 225, 395);
}
}
}
Prepáčte ak som v tomto vlákne niečo pokazil alebo zle napísal. Ale ešte sa v tomto neviem celkom orientovať. Keď tak ďakujem každému za pomoc :).