简体   繁体   English

将鼠标悬停在slick2d上吗?

[英]Mouse Over Area for slick2d?

I cannot find an answer in Google, but is there a Mouse Over Area for Slick2D ? 我在Google中找不到答案,但是Slick2D是否有Mouse Over Area Google just gives me results of Java 's MouseOverArea . Google只是给我JavaMouseOverArea I just want to know if there is a MouseOverArea for Slick2D , and how it looks like. 我只想知道Slick2D是否有MouseOverArea ,它的外观如何。

Here is my code: 这是我的代码:

Game class 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 class: 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 class: 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 class: 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 class: Play课:

I'm working on it still... but this class doesn't need the MouseOverArea , the Menu class does. 我仍在努力...但是此类不需要MouseOverArea ,而Menu则需要。


So that was my code above. 这就是我上面的代码。 I just need a MouseOverArea for Slick2D . 我只需要一个MouseOverArea用于Slick2D Google doesn't help. Google没有帮助。 Hope you can. 希望你能。

Also, can you have a TextField in Slick2D ? 另外,您可以在Slick2D使用TextField吗? I don't know if I can. 我不知道是否可以。 I know in normal Java you can, but can you in Slick2D ? 我知道在普通Java可以,但是在Slick2D吗?

If there are any mistakes, don't worry, I can fix them. 如果有任何错误,请放心,我可以修复它们。

Thanks 谢谢

Isn't the code put in here? 代码不是放在这里吗?

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM