gameServer.java:從父類訪問變量
import java.util.ArrayList;
public class gameServer{
public static server server;
public static gameRunner gameRunner;
public static ArrayList<packet> packets = new ArrayList<packet>();
public static void main(String[] args) throws Exception {
server = new server();
server.startServer(5050);
Runnable listenServer = new Runnable() {
public void run() {
while(true) {
packets.add(server.getPacket());
}
}
};
new Thread(listenServer).start();
gameRunner = new gameRunner();
while(true) {
if(packets.size() <= 0)
continue;
gameRunner.executeMessage(packets.get(0));
packets.remove(0);
}
}
}
server.js:
import java.net.*;
import java.util.Date;
public class server {
public static DatagramSocket socketServer;
private Date date = new Date();
public void startServer (int port) throws SocketException {
socketServer = new DatagramSocket(port);
System.out.println("Started Server on port: " + port);
}
public void sendPacket (packet packet) {
String message = null;
for(int i = 0; i < packet.header.length; i++) {
message += packet.header[i] + "\r\n";
}
message += "\r\n" + packet.message;
String[] info = packet.address.split(":");
try {
sendPacket(message, (Inet4Address)InetAddress.getByName(info[0]), Integer.parseInt(info[1]));
} catch (Exception e) {
System.out.println("failed to determine host");
}
}
}
gameRunner.js:
import java.util.ArrayList;
import java.util.Date;
public class gameRunner {
public static ArrayList<object> objects = new ArrayList<object>();
public static player[] players = new player[1000];
public Date date = new Date();
Runnable updatePlayers = new Runnable() {
public void run() {
while(true) {
long StartTime = date.getTime();
for(int i = 0; i < players.length; i++) {
if(players[i] == null)
continue;
players[i].updatePos();
}
while(StartTime + 100 < date.getTime()) { }
}
}
};
Runnable sendPlayerPackets = new Runnable() {
public void run() {
while(true) {
parent.gameServer.sendPacket(player[0].packet); //<<<----
}
}
};
}
我是新來的類,抽象的,固有的等等。我需要能夠通過gameRunner訪問gameServer中的socketServer對象來發送消息。我不知道如何做到這一點,在此先感謝。
你可以看看我的更新,感謝 – 2012-01-01 22:02:49
+1回答什麼問題應該是。 :-) – 2012-01-01 23:35:55