我在Google中找不到答案,但是Mouse Over Area
是否有Slick2D
? Google只是给我Java
的MouseOverArea
结果。我只想知道MouseOverArea
是否有Slick2D
,以及它的外观。
这是我的代码:Game
类
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Life - Alpha";
public static int splash = 0;
public static int menu = 1;
public static int loading = 2;
public static int play= 3;
public Game(String gamename) {
super(gamename);
}
public void initStatesList(GameContainer gc) throws SlickException {
this.addState(new SplashScreen (splash));
this.addState(new Menu (menu));
this.addState(new Exit (exit));
this.addState(new Loading (loading));
this.addState(new Play(play));
this.enterState(0);
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
SplashScreen
类:import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class SplashScreen extends BasicGameState {
Image splash;
private int elapsedTime;
private final int DELAY = 3000;
public SplashScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
splash = new Image("res/SplashScreen.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(splash, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(1);
}
}
public int getID() {
return 0;
}
}
Menu
类:import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
public Menu(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Background.png");
g.drawImage(background, 0, 0);
Image logo = new Image("res/Logo.png");
g.drawImage(logo, 275, 50);
Image playButton = new Image("res/Play button.png");
g.drawImage(playButton, 210, 250);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
//I want to put the Slick2D MouseOverArea code here...
//So then when I put the mouse over the playButton, something will display.
}
}
public int getID() {
return 1;
}
}
Loading
类:import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Loading extends BasicGameState {
private int elapsedTime;
private final int DELAY = 5000;
public Loading(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Back.png");
g.drawImage(background, 0, 0);
Image loading = new Image("res/Loading.png");
g.drawImage(loading, 210, 150);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(3);
}
}
public int getID() {
return 2;
}
}
Play
类:我仍在努力...但是此类不需要
MouseOverArea
,Menu
类则需要。这就是我上面的代码。我只需要
MouseOverArea
的Slick2D
。 Google没有帮助。希望你能。另外,您可以在
TextField
中有一个Slick2D
吗?我不知道是否可以。我知道在普通Java
中可以,但是在Slick2D
中可以吗?如果有任何错误,请放心,我可以修复它们。
谢谢
最佳答案
代码不是放在这里吗?
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
//mouseover
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}