变量不在线程中更新

时间:2013-06-23 06:08:09

标签: java sockets variables multiplayer

我最近从在线教程中下载了一些简单的游戏代码。我目前正试图让它成为多人游戏。但是在程序中我将坐标写入套接字的部分中,变量不会更新。我在互联网上搜索了一个答案,但我找不到答案。我测试了代码并成功发送了Ints。

My Client.java

import java.net.*;
import java.util.Scanner;
import java.io.*;

import javax.swing.SwingUtilities;



public class Client implements Runnable{

   public void run() {

       Craft crObj = new Craft();
       int playerX = 3001;
    int playerY = 300;
       String serverName = "127.0.0.1";
       int port = 6789;
       int port1 = 6788;
       Socket client = null, client1 = null;
       System.out.println("Connecting to " + serverName + " on port " + port);
       OutputStream outsetup = null, out1setup = null;


       try {
       client = new Socket(serverName, port);
       client1 = new Socket(serverName, port1);
       } catch (IOException e) {
           System.out.println(e);
       }
       System.out.println("Connected");

       Scanner test = new Scanner(System.in);
       try {
            outsetup = client.getOutputStream();
            out1setup = client1.getOutputStream();
       } catch(IOException e) {
           System.out.println(e);
       }
       DataOutputStream out = new DataOutputStream(outsetup);
       DataOutputStream out1 = new DataOutputStream(out1setup);


       while(true) {
           try {
//Doesn't Update Here
               playerX = crObj.getX();
               playerY = crObj.getY();
               out.writeInt(playerX);
               out.flush();
               out1.writeInt(playerY);
               out1.flush();

           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }

      }

   }
}

My Craft.java

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Craft {

    private String craft = "craft.png";
    Client clObj = new Client();
    public int dx;
    public int dy;
    public int x;
    public int y;
    private Image image;
    public ImageIcon ii;

    public Craft() {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
        image = ii.getImage();
        x = 40;
        y = 60;
    }


    public void move() {
        x += dx;
        y += dy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }
    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = -2;
            ii = new ImageIcon(this.getClass().getResource("craftBACK.png"));
            image = ii.getImage();
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 2;
            ii = new ImageIcon(this.getClass().getResource("craft.png"));
            image = ii.getImage();
        }

        if (key == KeyEvent.VK_UP) {
            dy = -2;
            ii = new ImageIcon(this.getClass().getResource("craftUP.png"));
            image = ii.getImage();
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 2;
            ii = new ImageIcon(this.getClass().getResource("craftDOWN.png"));
            image = ii.getImage();
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }

    }
}

Board.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Board extends JPanel implements ActionListener {
    public int xMain = 0;
    Client clObj = new Client();
    private Timer timer;
    private Craft craft;

    public Board() {

        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);

        craft = new Craft();

        timer = new Timer(5, this);
        timer.start();
    }


    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    public void actionPerformed(ActionEvent e) {
        craft.move();
        xMain = craft.getX();
        System.out.println(xMain);
        repaint();
    }


    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }

}

RType.java

import javax.swing.JFrame;

public class RType extends JFrame {

    public RType() {

        add(new Board());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setTitle("R - Type");
        setResizable(false);
        setVisible(true);
        (new Thread(new Client())).start();
    }

    public static void main(String[] args) {
        new RType();
    }
}

2 个答案:

答案 0 :(得分:1)

由于您正在访问来自不同线程的值,因此它们应该有所帮助。

替换

public int dx;
public int dy;
public int x;
public int y;

public volatile int dx;
public volatile int dy;
public volatile int x;
public volatile int y;

您可能还需要在while(true)循环中设置一些延迟

playerX = crObj.getX();
playerY = crObj.getY();

延迟一段时间后获取这些值(Thread.sleep(100))

答案 1 :(得分:-1)

使变量静态对我有用

替换

public int dx;
public int dy;
public int x;
public int y;

使用

public static volatile int dx;
public static volatile int dy;
public static volatile int x;
public static volatile int y;

不建议在此使用volatile限定词。但是为了获得新的价值,应该使用它。