java | Multiplayer platform

  • Leider hab ich ein problem da mein characte rin dem fall das oval sich nicht bewegt.
    Deshalb woltle ich fragen ob hier jemand im forum den fehler entdeckt
    mit W,A,S,D kann man ihn steuern, leider bewegt er sich nicht.


    Server.java:
    import java.io.*;
    import java.net.*;
    public class server{

    static ServerSocket serverSocket;
    static Users[]user = new Users[10];
    public void main(String[]args)throws Exception{
    System.out.println("Starting Server...");
    serverSocket = new ServerSocket(7777);
    System.out.println("Server Started...");
    while(true){
    Socket socket = serverSocket.accept();
    for(int i=0;i<10;i++){
    if(user[i] == null){
    System.out.println("Connection from:" + socket.getInetAddress());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    DataInputStream in = new DataInputStream(socket.getInputStream());
    user[i] = new Users(out,in,user,i);
    Thread th = new Thread(user[i]);
    th.start();
    break;
    }
    }
    }
    }
    }
    class Users implements Runnable{
    DataOutputStream out;
    DataInputStream in;
    Users[] user = new Users[10];
    String name;
    int playerid;
    int playeridin;
    int xin;
    int yin;
    public Users(DataOutputStream out,DataInputStream in,Users[]user,int pid){
    this.out = out;
    this.in = in;
    this.user = user;
    this.playerid = pid;
    }
    public void run(){
    try{
    out.writeInt(playerid);
    }catch(IOException e1){
    System.out.println("Fail to send Playerid");
    }
    while(true){
    try{
    playeridin = in.readInt();
    xin = in.readInt();
    yin = in.readInt();
    for(int i=0;i<10;i++){
    if(user[i] != null){
    user[i].out.writeInt(playeridin);
    user[i].out.writeInt(xin);
    user[i].out.writeInt(yin);
    }
    }
    }catch(IOException e){
    user[playerid] = null;
    }
    }
    }
    }
    Client.java:
    import java.net.*;
    import java.util.Scanner;
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.*;


    public class client extends Applet implements Runnable,KeyListener{


    static Socket socket;
    static DataOutputStream out;
    static DataInputStream in;

    int playerid;

    int[]x = new int[10];
    int[]y = new int[10];

    boolean left,down,right,up;

    int playerx;
    int playery;

    public void init(){
    setSize(100,100);
    addKeyListener(this);
    try{
    System.out.println("Connecting...");
    socket = new Socket("localhost",7777);
    System.out.println("Connection was Succesfull");
    in = new DataInputStream(socket.getInputStream());
    playerid = in.readInt();
    out = new DataOutputStream(socket.getOutputStream());
    Input input = new Input(in,this);
    Thread thread = new Thread(input);
    thread.start();
    Thread thread2 = new Thread(this);
    thread2.start();
    }catch(Exception e){
    System.out.println("Unable to start Client");
    }


    }
    public void updateCoordiantes(int pid, int x2, int y2){
    this.x[pid] = x2;
    this.y[pid] = y2;
    }
    public void paint(Graphics g){
    for(int i=0;i<10;i++){
    g.drawOval(x[i],y[i],5,5);
    }
    }

    public void run(){
    while(true){
    if(right == true){
    playerx+=10;
    }
    if(left == true){
    playerx-=10;
    }
    if(down == true){
    playery+=10;
    }
    if(up == true){
    playery-=10;
    }
    if(right||left||up||down){
    try{
    out.writeInt(playerid);
    out.writeInt(playerx);
    out.writeInt(playery);
    }catch(Exception e){System.out.println("Error Sendin...");}
    }
    repaint();

    try {
    Thread.sleep(400);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == 65){
    left = true;
    }
    if(e.getKeyCode() == 87){
    up = true;
    }
    if(e.getKeyCode() == 68){
    right = true;
    }
    if(e.getKeyCode() == 83){
    down = true;
    }

    }
    public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == 65){
    left = false;
    }
    if(e.getKeyCode() == 87){
    up = false;
    }
    if(e.getKeyCode() == 68){
    right = false;
    }
    if(e.getKeyCode() == 83){
    down = false;
    }
    }
    public void keyTyped(KeyEvent e) {}
    }
    class Input implements Runnable{

    client client;
    DataInputStream in;
    public Input(DataInputStream in,client c){
    this.in = in;
    this.client = c;
    }
    public void run(){
    while(true){
    try{
    int playerid = in.readInt();
    int x = in.readInt();
    int y = in.readInt();
    client.updateCoordiantes(playerid,x,y);
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }
    }


    kann mir wer helfen ?


    mfg kyle

  • Er bewegt sich, nur musst du gedrückt halten und er bewegt sich relativ langsam.
    //edit: Wenn du den Thread.sleep() in Client.java auf 100 setzt läuft das ganze schon wesentlich besser und schneller, nur flackert die Grafik ein wenig.