textfield不使用BasicGameState

时间:2013-01-17 22:29:27

标签: java textfield lwjgl slick2d

我在一个干净的BasicGame上测试了TextField并且它运行了。 (我看到了边框,我可以输入。)

代码:

            TextField lanText;  
            TrueTypeFont font;

            public void init(........)
        {
            font = new TrueTypeFont(new java.awt.Font(java.awt.Font.SERIF,java.awt.Font.BOLD,8),false);
            lanText = new TextField(gc, font, 50, 100, 350, 25);

}

    public void render(.....)
        {  
            lanText.render(gc, g);
        }

但是当我在使用BasicGameState的游戏中尝试它时它不起作用。怎么了? (我可以看到边框,但我不能打字)

我有4个州。菜单0.游戏1.合作2.选项3。

我试图将它添加到状态2(Coop)。

public class Game
extends StateBasedGame
{
    public final int menu = 0;
    public final int game = 1;
    public final int option = 2;
    public final int coop = 3;

    public Game(String gamename)
    {
        super(gamename);
        this.addState(new Menu(menu));
        this.addState(new Game(game));
        this.addState(new Option(option));
        this.addState(new Coop(coop));
    }

public void initStatesList(GameContainer gc)
    throws SlickException
    {
    this.getState(menu).init(gc, this);
    this.getState(game).init(gc, this);
    this.getState(option).init(gc, this);
    this.getState(coop).init(gc, this);
    this.enterState(menu);
}
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了。以下是任何遇到同样问题的人的代码。

import java.awt.Font;
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.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Coop
        extends BasicGameState
{
    TextField text;
    UnicodeFont font;

    public Coop(int state)
    {
    }

    public void init(GameContainer gc , StateBasedGame sbg)
            throws SlickException
    {
        font = getNewFont("Arial" , 16);
    }

    public void render(GameContainer gc , StateBasedGame sbg , Graphics g)
            throws SlickException
    {
        text.render(gc , g);
        g.setFont(font);
    }

    public void update(GameContainer gc , StateBasedGame sbg , int delta)
            throws SlickException
    {
        font.loadGlyphs();
    }

    public int getID()
    {
        return 3;
    }

    public void enter(GameContainer gc , StateBasedGame sbg)
            throws SlickException
    {
        text = new TextField(gc , font , 150 , 270 , 200 , 35);
    }

    public UnicodeFont getNewFont(String fontName , int fontSize)
    {
        font = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
        font.addGlyphs("@");
        font.getEffects().add(new ColorEffect(java.awt.Color.white));
        return (font);
    }
}

我不知道为什么需要使用font.loadGlyphs();但没有它,它就无法工作。