Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Kryonet listener to Slick2d  (Read 1046 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-12-03 15:18:50 »

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?
 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
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();

    }
}
Offline Reck

Senior Newbie





« Reply #1 - Posted 2012-12-03 17:40:24 »

This is just bugging me, i can't seem to find anything on the subject anywhere.

I really hope someone here can help out! :-)
Offline Cero
« Reply #2 - Posted 2012-12-03 17:44:47 »

Im not exactly sure what you want, but it would seem that line 21 should be simply
1  
this.name = ((LoginAnswer) object).name;

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Reck

Senior Newbie





« Reply #3 - Posted 2012-12-03 17:48:50 »

Im not exactly sure what you want, but it would seem that line 21 should be simply
1  
this.name = ((LoginAnswer) object).name;


That is exactly what i want, however that is not working. It seems that the codeblock in the Listener behaves like its own class.
Netbeans suggests adding the field name in the Listener. (Line 15 that is)
I sure hope there's a way to "break out" of this block
Offline Cero
« Reply #4 - Posted 2012-12-03 22:22:07 »

ah yeah one of the annoying things about listeners
I use some workarounds which aren't - I'm sure someone else here knows how to do this properly

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2012-12-05 19:40:40 »

I sure hope there's a way to "break out" of this block

Yes. There is a way to "break out". You have an inner class there. The listener is indeed a new class.

To get access to the object of the class you defined the inner class (the listener) in, simply use
SuperClass.this.field
, so in this case
NFTClient.this.name = ((LoginAnswer) object).name;
.

I guess this should work Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (58 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!