【问题标题】:Mouse Over Area for slick2d?slick2d的鼠标悬停区域?
【发布时间】:2013-05-08 10:24:17
【问题描述】:

我在 Google 中找不到答案,但有 Mouse Over Area 对应的 Slick2D 吗?谷歌只是给了我Java 的MouseOverArea 的结果。我只是想知道Slick2D 是否有MouseOverArea,以及它的样子。

这是我的代码:

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。谷歌没有帮助。希望你能。

另外,你能在Slick2D 中有一个TextField 吗?我不知道我能不能。我知道在正常的Java 可以,但是你可以在Slick2D 吗?

如果有任何错误,别担心,我会改正的。

谢谢

【问题讨论】:

    标签: java mouseover slick


    【解决方案1】:

    这里不是放代码吗?

    if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
            //mouseover
            if (input.isMousePressed(0)) {
                sbg.enterState(2);
            }
    
        }
    }
    

    【讨论】:

    • 欢迎来到该网站'您介意编辑您的答案,使其更像是一个答案而不是一个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2011-08-04
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多