When KryoNet recieves the LoginAnswer package, i want to set the field of NFTClient String name (Line 4) to the ((LoginAnswer)object).name variable (Line 21) in order to use it in the Render method. How can i do that?
public class NFTClient extends BasicGame {
Client client;
String name;
public NFTClient() {
super("Client");
client = new Client();
client.start();
Network.register(client);
// ThreadedListener runs the listener methods on a different thread.
client.addListener(new Listener.ThreadedListener(new Listener() {
public void connected(Connection connection) {
}
public void received(Connection connection, Object object) {
if (object instanceof LoginAnswer) {
String name = ((LoginAnswer) object).name;
}
}
public void disconnected(Connection connection) {
System.exit(0);
}
}));
try {
client.connect(5000, "localhost", Network.port);
// Server communication after connection can go here, or in Listener#connected().
System.out.println("heJ");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void init(GameContainer gc) throws SlickException {
}
public void render(GameContainer gc, Graphics g) throws SlickException {
g.drawString(this.name);
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
}
public static void main(String[] args) throws SlickException {
Log.set(Log.LEVEL_DEBUG);
AppGameContainer app = new AppGameContainer(new NFTClient());
app.setDisplayMode(768, 512, false);
app.setTargetFrameRate(60);
app.start();
}
}