我正在使用处理编写一个小游戏,并希望添加多人游戏功能。我使用Socket并且现在遇到了一个问题:我想只读取服务器上的最后一条消息,并使用一些不为服务器消息空闲的消息。这是我使用的代码。它就像是服务器的级别选择,它将选择的级别发送到客户端,因此它可以加载它但是在客户端上,因为服务器只在加载新级别时发送消息并且客户端的游戏才冻结直到收到来自服务器的消息。
服务器代码:
//Set up all the buttons
disableMenus();
enableMenus(7,13);
enableMenus(19,19);
//show the current loaded level
printLevel();
printGuards(true);
for(int i = 0 ; i < cams.length ; i++) {
if(cams[i]) {
while (camAngleMin[i] > camAngleMax[i]) {
camAngleMin[i] = camAngleMin[i] - 2*PI;
}
int t = 0;
int r = 0;
int buffer = round(camAngle[i]/(PI/2));
PImage images;
if(buffer > 3)
buffer = 0;
else if(buffer < 0)
buffer = 3;
images = camImage[buffer];
if(buffer == 0) {
t = images.width;
r = 1;
} else if(buffer == 1) {
t = images.width/2;
r = images.height;
} else if(buffer == 2) {
t = 0;
r = 1;
} else {
t = images.width/2;
r = 0;
}
if(camAngle[i] > camAngleMax[i] && camBuffer[i] == 0) {
camGoToMin[i] = true;
camBuffer[i] = 200;
} else if(camAngle[i] < camAngleMin[i] && camBuffer[i] == 0) {
camGoToMin[i] = false;
camBuffer[i] = 200;
} else if(camBuffer[i] > 0){
camBuffer[i]--;
}
//println(camGoToMin[i]+":"+camBuffer[i]);
if(camGoToMin[i] && camBuffer[i] == 0) {
camAngle[i] = camAngle[i] - (PI/512);
}
else if(camBuffer[i] == 0){
camAngle[i] = camAngle[i] + (PI/512);
}
image(images,camX[i],camY[i]);
//lumiereCam[i].display(camX[i]+t, camY[i]+r,false,null,camAngle[i]);
rayC[i].display(camX[i]+t, camY[i]+r, str(camAngle[i]));
}
//Send the current loaded to the client
if(!compareStrings(loadedLevel,lastLevelLoaded)) {
dout.println(loadedLevel);
lastLevelLoaded = loadedLevel;
}
dout.flush();
}
客户代码:
try {
//request the level loaded by the server
String level = din.readLine();
//set up the buttons
disableMenus();
//print the current loaded level on the screen
printLevel();
printGuards(true);
for(int i = 0 ; i < cams.length ; i++) {
if(cams[i]) {
while (camAngleMin[i] > camAngleMax[i]) {
camAngleMin[i] = camAngleMin[i] - 2*PI;
}
int t = 0;
int r = 0;
int buffer = round(camAngle[i]/(PI/2));
PImage images;
if(buffer > 3)
buffer = 0;
else if(buffer < 0)
buffer = 3;
images = camImage[buffer];
if(buffer == 0) {
t = images.width;
r = 1;
} else if(buffer == 1) {
t = images.width/2;
r = images.height;
} else if(buffer == 2) {
t = 0;
r = 1;
} else {
t = images.width/2;
r = 0;
}
if(camAngle[i] > camAngleMax[i] && camBuffer[i] == 0) {
camGoToMin[i] = true;
camBuffer[i] = 200;
} else if(camAngle[i] < camAngleMin[i] && camBuffer[i] == 0) {
camGoToMin[i] = false;
camBuffer[i] = 200;
} else if(camBuffer[i] > 0){
camBuffer[i]--;
}
//println(camGoToMin[i]+":"+camBuffer[i]);
if(camGoToMin[i] && camBuffer[i] == 0) {
camAngle[i] = camAngle[i] - (PI/512);
}
else if(camBuffer[i] == 0){
camAngle[i] = camAngle[i] + (PI/512);
}
image(images,camX[i],camY[i]);
//lumiereCam[i].display(camX[i]+t, camY[i]+r,false,null,camAngle[i]);
rayC[i].display(camX[i]+t, camY[i]+r, str(camAngle[i]));
}
}
//check if the game have to be launched
if(compareStrings(level,"start")) {
menu = 11;
}
//check if the server's level is already loaded
else if(!compareStrings(loadedLevel,level) && level != null && level.length() != 0) {
loadLevelPath(level);
}
} catch(SocketException e) {
menu = -1;
errorMsg("Le serveur à quitté",Error);
}
感谢您花时间回复我的帖子