I made a test multiplayer game (http://www.greenfoot.org/scenarios/15639 ).
You can't really do anything just move around. (and the confirm with a frend (and skype) that it works and start to work on a real project)
You are blue the other players are red.
The problem is that the red players (the multiplayer players) don't appear.
I will show a tiny part of my code but if you want to see the full code you should probably just download the source I provided.
I don't think the mistake is in one of the utility classes I made because it does print the movement updates to the console.
sorry I had to show so much code the most inportend stuff is form playworld (I think)
the mistake most likely is here somewhere :
Menu.class :
-The reicive method gets called form the client
-The messageHandler just has an abstract class with an abstract method reicive(String text)
-The join() and host() get called if a Host or Join object gets clicked
PlayWorld.set (the pl object is the local player (blue))
the addObject doesn't happen for some reason.
the client :
I found out to that .jar exports with libraries don't work.
import greenfoot.*;
import java.io.IOException;
/**
* Write a description of class Menu here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Menu extends World implements MessageHandler
{
static Client c;
static Server s;
PlayWorld w;
/**
* Constructor for objects of class Menu.
*
*/
public Menu()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
addObject(new Host(),200,200);
addObject(new Join(),400,200);
}
public void join() {
String ip = Greenfoot.ask("ip");
String portS = Greenfoot.ask("port");
join(ip,Integer.valueOf(portS));
}
public void join(String ip, int port) {
try {
c = new Client(ip,port,this);
}catch(IOException e) {
System.out.println("ERROR:" + e);
e.printStackTrace();
}
w = new PlayWorld();
Greenfoot.setWorld(w);
send("get player");
}
public void host() {
//String ip = Greenfoot.ask("ip");
String portS = Greenfoot.ask("port");
host(Integer.valueOf(portS));
}
public void host(int port) {
try {
s = new Server(port);
}catch(IOException e) {
System.out.println("ERROR:" + e);
e.printStackTrace();
}
join("127.0.0.1",port);
}
public void reicive(String data) {
String[] words = data.split(" ");
if(words[0] == "player") {
String id = words[1];
String x = words[2];
String y = words[3];
w.set(id,Integer.valueOf(x),Integer.valueOf(y));
}else if(words[0] == "get") {
if(words[1] == "player") {
MyPlayer pl = w.getPlayer();
send("player " + pl.id + " " + pl.getX() + " " + pl.getY());
}
}
}
public static void send(String data) {
try {
c.send(data);
}catch(IOException e) {
System.out.println("ERROR:" + e);
e.printStackTrace();
}
}
}
public void set(String id,int x,int y) {
boolean found = false;
if(id == pl.id) pl.setLocation(x,y);
else {
for(Object a : getObjects(OtherPlayer.class)) {
OtherPlayer pl = (OtherPlayer) a;
if(pl.id == id) {
pl.setLocation(x,y);
found = true;
break;
}
}
}
if(found == false) {
addObject(new OtherPlayer(id),x,y);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client implements Runnable {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public long sleepTime = 100; //check for incomming messages 10 times per second
public MessageHandler handler;
//public static IOHelp io = new IOHelp();
public Client(String ip,MessageHandler m) throws IOException {
this(ip,25565,m);
}
public Client(String ip, int port,MessageHandler m) throws IOException {
handler = m;
socket = new Socket(ip, port);
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Thread t = new Thread(this);
t.start();
System.out.println("new Client is construckted (" + ip + "," + port + "," + m+")");
}
public Client(String ip,long sleepTime,MessageHandler m) throws IOException {
this(ip,25565,sleepTime,m);
}
public Client(String ip, int port,long sleepTime,MessageHandler m) throws IOException {
handler = m;
socket = new Socket(ip, port);
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.sleepTime = sleepTime;
Thread t = new Thread(this);
t.start();
System.out.println("new Client is construckted (" + ip + "," + port + "," + m+","+sleepTime+")");
}
public void send(String data) throws IOException {
out.println("send,"+ data);//io.stringize(data));
}
public void sendAll(String data) throws IOException {
out.println("sendAll,"+ data);//io.stringize(data));
}
boolean active = true;
@Override
public void run() {
// Auto-generated method stub
System.out.println("Client is lissening for messages");
while(active) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// Auto-generated catch block
//e.printStackTrace();
}
String input;
try {
while((input = in.readLine()) != null) {
System.out.println("the clients got :" + input);
handler.reicive(input);
}
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Client isn't lissening for messages anymore");
}
public void stop() {
active = false;
}
}

