0

我已经在一个干净的 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);
}
}
4

1 回答 1

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(); 但没有它,它就行不通。

于 2013-01-18T13:01:55.597 回答