2013-11-01 79 views
0

我的问题是,每当我尝试更改图片时,鼠标悬停在它上面,它不会改变,当我做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; 
    } 
    } 

回答

0

您只能在render方法中绘制东西。您必须保存在update方法中更改的状态,然后在render方法中读取这些状态。