This code is to transfer the X and Y coords of the player to a different computer so i can make a networking adventure code but the networking method is static so i can send the X and Y coords from it. I have tried to remove the static from the method it does not work so i am posting it here. (this is only the server code it will not run proberly if you try and compile it on your comp i just need help how to get the X and Y coords into the networking method)
The code is a bit confusing. This class is run as an applet on one or more clients, and as an application on the server?
I don't see any code in the applet that would make a network call to the server. You may want to start reading through the networking chapter of the Java Tutorial. It contains an example of how to create a client/server pair that communicates over sockets.
The code itself works as it is now but if i try and take a variable say to transfer player positions i get a error of
"non static variable X cannot be referenced from a static context"
that is on line 98 that i get that error so how do i get rid of that error. The reason i want to this is so i can transfer a players position to another computer on a network running the client code. (which is basically the same) Then draw the player on the map so it can be a networked 2 player game. here is just that code again
The error comes at the line
The X is to transfer the X coords of the player
posted 11 years ago
Static code (like the main method) can't access instance variables, only static fields. But since X is set in the client -and not anywhere in the server code- what good does accessing it do?
I think the code would benefit from a strict separation of the client and the server in two different classes.
I'm also a bit sceptical about you saying that the code works - there's no networking code in the client, so how could it work?
(And you will need to tackle the issue discussed in the article I linked to - unless you can guarantee that both client and server will always run on Windows.)