图像鼠标悬停错误

时间:2013-11-01 13:31:08

标签: java image lwjgl slick2d

我的问题是,每当我试图在鼠标悬停在它上面时改变图像,它就不会改变,当我做playgame.destroy();它只是在它后面显示一个白色屏幕,而不是其他图像。继承我的代码:

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState {

Image playgame;
Image exitgame;
Image playgame_hover;
Image exitgame_hover;

public String mouse = "No Input Yet!";

public Menu(int state) {
}

@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    playgame = new Image("res/playgame.png");
    exitgame = new Image("res/exitgame.png");
    playgame_hover = new Image("res/playgame_hover.png");
    exitgame_hover = new Image("res/exitgame_hover.png");
}

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    g.drawString(mouse, 590, 10);
    playgame.draw(100,100);
    exitgame.draw(100, 200);
}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
    int mousex = Mouse.getX();
    int mousey = Mouse.getY();
    mouse = "Mouse coordinate x: " + mousex + " y: " + mousey;
    // x-min:105  x-max:300  y-min:  y-max:300
    if(input.isMouseButtonDown(0)) {
        if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
        sbg.enterState(1);
    }
        if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
        System.exit(0);
    }
    }
    if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
        playgame.destroy();
        playgame_hover.draw(100, 100);
    }
    if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
        exitgame.destroy();
        exitgame_hover.draw(100, 200);
    }
    }

    @Override
    public int getID() {
    return 0;
    }
    }

1 个答案:

答案 0 :(得分:0)

您只能使用render方法绘制内容。您必须保存update方法中更改的状态,然后使用render方法读取这些状态。