使用Slick2D的TextField时遇到问题。
使用Slick' BasicGame' TextField工作正常 - 我可以单击它,键入单词,System.out.println文本字段内容。
但是,在' BasicGameState'上使用相同的代码时,TextField是不可点击的,并且不响应任何输入。
所以: 工作代码:
package Help;
import java.awt.Font;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
public class TextFieldWorking extends BasicGame{
private TextField text1;
private UnicodeFont font = getNewFont("Arial" , 16);
public TextFieldWorking() {
super("Test");
}
@Override
public void init(GameContainer container) throws SlickException {
font.loadGlyphs();
text1 = new TextField(container, font, 50,50,100,25);
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
System.out.println(text1.getText());
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
text1.render(gc, g);
}
public UnicodeFont getNewFont(String fontName , int fontSize){
UnicodeFont returnFont = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
returnFont.addAsciiGlyphs();
returnFont.getEffects().add(new ColorEffect(java.awt.Color.white));
return (returnFont);
}
public static void main(String[] args) throws SlickException {
TextFieldWorking g = new TextFieldWorking();
AppGameContainer gc = new AppGameContainer(g);
gc.setDisplayMode(500, 500, false);
gc.start();
}
}
非工作代码:
package Help;
import java.awt.Font;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class TextFieldTest extends BasicGameState{
int WindowWidth, WindowHeight;
TextField text1;
private UnicodeFont font = getNewFont("Arial" , 16);
private int stateId = 0;
public TextFieldTest(int State) {
this.stateId = State;
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
WindowWidth = gc.getWidth();
WindowHeight = gc.getHeight();
font.loadGlyphs();
text1 = new TextField(gc, font, 50,50,100,25);
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
text1.render(gc, g);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
System.out.println(text1.getText());
}
public UnicodeFont getNewFont(String fontName , int fontSize){
UnicodeFont returnFont = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
returnFont.addAsciiGlyphs();
returnFont.getEffects().add(new ColorEffect(java.awt.Color.white));
return (returnFont);
}
public int getID(){
return this.stateId; //Returns the Id of this state (menu is 0)
}
}
为方便起见:
这与BasicGameState一致 - 它是Slick2D如何工作的 - 你可以立即复制 - 粘贴代码:P
package Help;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
public class Game extends StateBasedGame{
public static final String gamename = "Problem!";
public static final int TextFieldTestNum = 1;
public Game(String gamename){
super(gamename);
this.addState(new TextFieldTest(TextFieldTestNum));
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(TextFieldTestNum).init(gc, this);
this.enterState(TextFieldTestNum); //The First state to enter
}
@Override
public boolean closeRequested(){
System.exit(0);
return false;
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
Game g = new Game(gamename);
appgc = new AppGameContainer(g);
appgc.setDisplayMode(500, 500, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
}
任何想法都赞赏!
答案 0 :(得分:2)
你的问题是如何在浮油中使用StateBasedGames。如果你在这里引用我的wiki条目:http://slick.ninjacave.com/wiki/index.php?title=Game_States
你会注意到在initStatesList()方法中,我只使用this.addState(new StateName())。这是因为addState()方法的作用。它为您调用init方法,因此无需在顶级Application类(在您的情况下为Game.java)中执行getState()或enterState()。
总结如何解决问题,删除initStatesList()方法中的所有内容, 然后移动this.addState(new TextFieldTest(TextFieldTestNum)); 从主构造函数到initStatesList()方法。
一旦我在你的程序中纠正了它,它对我来说运行得很好。