所以我正在尝试使用带有keyListener的客户端服务器。当消息“#left”或“#right”被发送到服务器时,它应该更新图形对象并通过调用map.repaint()方法将其移动到左侧。相反,它将对象移动一次,然后不再发生进一步的响应。下面发布的是代码。有人可以提供解决方案吗?
//服务器
@Override
protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
// TODO Auto-generated method stub
if(msg instanceof String)
{
String message = msg.toString();
if(message.equals("#connect"))
{
String error = "Already connected";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #connect error message");
}
break;
}
}
if(match == null)
{
Matcher matcher = new Matcher(this,client);
matches.add(matcher);
}
}
if(message.equals("#left"))
{
action("left",client);
}
if(message.equals("#right"))
{
action("right",client);
}
if(message.equals("#jump"))
{
action("jump",client);
}
if(message.startsWith("#shoot"))
{
String error = "please connect";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
break;
}
}
if(match == null)
{
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #left error message");
}
}else
{
match.moveLeft(client);
}
}
if(message.contains("?"))
try {
client.sendToClient(msg);
} catch (IOException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (msg instanceof Integer)
try {
client.sendToClient(msg);
} catch (IOException ex) {
System.out.println("Message not sent. Turning off.");
}
private void action(String errorm, ConnectionToClient client)
{
String error = "please connect";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
break;
}
}
if(match == null)
{
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #" + errorm + " error message");
}
}else
{
if(errorm.equals("left"))
{
match.moveLeft(client);
}else if(errorm.equals("right"))
{
match.moveRight(client);
}
else if(errorm.equals("jump"))
{
match.updateJump(client);
}
}
}
//the matcher class which controls graphics
public void moveLeft(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
map.updateFirstPlayer(map.getx1Rec1() - 5, map.gety1Rec1(), map.getx2Rec1(), map.gety2Rec1());
map.repaint();
map.updateUI();
}
}
if(i == 1)
{
synchronized(this)
{
map.updateSecondPlayer(map.getx1Rec2() - 5, map.gety1Rec2(), map.getx2Rec2(), map.gety2Rec2());
map.repaint();
map.updateUI();
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not update left");
}
}
public void moveRight(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
map.updateFirstPlayer(map.getx1Rec1() + 5, map.gety1Rec1(), map.getx2Rec1(), map.gety2Rec1());
map.repaint();
map.updateUI();
}
}
if(i == 1)
{
synchronized(this)
{
map.updateSecondPlayer(map.getx1Rec2() + 5, map.gety1Rec2(), map.getx2Rec2(), map.gety2Rec2());
map.repaint();
map.updateUI();
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not update right");
}
}
public void updateJump(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
for (int j = 0; j < 50; j+=5)
{
map.updateFirstPlayer(map.getx1Rec1() , map.gety1Rec1() + j, map.getx2Rec1(), map.gety2Rec1());
/*try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
map.repaint();
}
}
}
if(i == 1)
{
synchronized(this)
{
for (int j = 0; j < 50; j+=5)
{
map.updateSecondPlayer(map.getx1Rec2(), map.gety1Rec2() + j, map.getx2Rec2(), map.gety2Rec2());
/*try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
map.repaint();
map.updateUI();
}
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not return jump");
}
}