java - Using client-server to move object on screen -
so i'm trying use client-server keylistener. when messages "#left" or "#right" sent server, should update graphical object , shift left calling map.repaint() method. instead, shifts object once , no further responses happen. posted below code. can offer solution?
// server
@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; (int = 0; < 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; (int = 0; < 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; (int = 0; < 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 controls graphics public void moveleft(connectiontoclient client) { (int = 0; < 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) { (int = 0; < 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) { (int = 0; < players.length; i++) { if(players[i].getid() == client.getid()) { if(i == 0) { synchronized(this) { (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) { (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"); } }
Comments
Post a Comment