简体   繁体   English

Java:客户端,客户端,(线程)服务器,流客户端信息,创建的JPanel,但messages(?)阻止了游戏开始

[英]Java: client, client, (threaded) server, streams client info, JPanel created but messages(?) block start of game

I am developing a small scale client1/ client2, server (threaded) TCP game . 我正在开发一个小型的client1/ client2, server (threaded) TCP game In trying to deal with latency issues I've realised there were flaws in my transmitState() . 在尝试处理延迟问题时,我意识到我的transmitState()存在缺陷。 It was forcing unnecessary info down the comms streams creating sluggishness, leaving cars in different places in the JPanel, and was basically just wrong. 它迫使不必要的信息进入通讯流,从而导致速度缓慢,从而使汽车停放在JPanel中的不同位置,这基本上是错误的。 So I took it out. 所以我把它拿出来了。

But in doing so I've created problems in that despite all comms, protocols, threads, 'messenger's and everything working, somewhere in my logic I've halted the start of the game by not allowing for the repaint() , transmitState() and receiveState() to do their job in logical orders. 但是,尽管所有通讯科在做那个,所以我已经创建的问题,协议,线程,“Messenger的一切工作,在什么地方我的逻辑,我不容许的暂停比赛开始repaint() transmitState()receiveState()以逻辑顺序执行其工作。

I really can't see where I've gone wrong. 我真的看不到哪里出了问题。 Any suggestions? 有什么建议么?

NOTE: if conditions in keyPressed case statement tests to get sprite to 'spin' 360degrees on left right etc. and vel is velocity so can't exceed 100. carRem = car remote client. 注意: if keyPressed case语句中的条件进行了测试,以使子画面在左右等方向上“旋转” 360度,并且vel是速度,因此不能超过carRem =汽车远程客户端。 I know there's a lot of code but I've been (rightly) told off in the past for not including enough code so I think all of this shows what is needed to be shown (let me know if I'm wrong :) ). 我知道有很多代码,但是过去(正确地)因为没有包含足够的代码而被告知,所以我认为所有这些都表明需要显示的内容(如果我错了,请告诉我:)) 。

TRANSMIT STATE: 传输状态:

public void transmitState(String s)
    {
        try
        {
            outputStream.writeBytes(s + "\n");
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
    }

RECEIVE STATE: 接收状态:

    public void receiveState()
    {
        try
        {
            messageIn = inputStream.readLine();
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
        testState(messageIn);
    }

TEST STATE: 测试状态:

public void testState(String s)
{
if (s.equals("l") || s.equals("r") || s.equals("u") || s.equals("d"))
{
  c = s.charAt(0);
  this.i2 = i2;
  this.vel2 = vel2;
  this.carRem = carRem;

  switch (c)
  {
    case 'l':
      if (i2 == 0)
      {
        i2 = 15;
      }
      else
      {
        i2--;
      }
      carRem.setCurrentImage(i2);
      break;
    case 'r':
      if (i2 == 15)
      {
        i2 = 0;
      }
      else
      {
        i2++;
      }
      carRem.setCurrentImage(i2);
      break;
    case 'u':
      if (vel2 == 100)
      {

      }
      else
      {
        vel2 = vel2 + 10;
        carRem.setVel(vel2);
      }
      break;
    case 'd':
      if (vel2 == 0)
      {

      }
      else
      {
        vel2 = vel2 - 10;
        carRem.setVel(vel2);
      }
      break;
  }
}
}

JPANEL (with most client functionality): JPANEL(具有大多数客户端功能):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UIPanel extends JPanel implements ActionListener, KeyListener
{       
    //sprite creation stuff, works fine (excluded here)
    private Timer timer;
    private CarLocal carLoc;
    private CarRemote carRem;
    private String carLocColour;

    private ClientMessenger cliMess = null;

    public UIPanel(CarLocal carLoc, CarRemote carRem, ClientMessenger cliMess)
    {
        this.carLoc = carLoc;
        this.carRem = carRem;
        this.cliMess = cliMess;

        images1 = new ImageIcon[TOTAL_IMAGES];
        images2 = new ImageIcon[TOTAL_IMAGES];

        carLocColour = carLoc.getColour();
            //setting sprites and testing order  (works fine and excluded here)

        this.addKeyListener(this);
        this.setFocusable(true);

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

    public void keyTyped(KeyEvent e) 
    {

    }

   public void keyPressed(KeyEvent e) 
    {
        int i1 = carLoc.getCurrentImage();
        int i2 = carRem.getCurrentImage();
        int vel1 = carLoc.getVel();
        int vel2 = carRem.getVel();
        switch (e.getKeyCode()) 
        {
        case KeyEvent.VK_LEFT:
                if(i1 == 0)
                {
                    i1 = 15;
                }
                else
                {
                    i1--;
                }
                carLoc.setCurrentImage(i1);
                cliMess.transmitState("l");
                break;
            case KeyEvent.VK_RIGHT:
                if(i1 == 15)
                {
                    i1 = 0;
                }
                else
                {
                    i1++;
                }
                carLoc.setCurrentImage(i1);
                cliMess.transmitState("r");
                break;
        case KeyEvent.VK_UP:
                if(vel1 == 100)
                {

                }
                else
                {
                    vel1 = vel1 + 10;
                    carLoc.setVel(vel1);
                    cliMess.transmitState("u");
                }
                break;
        case KeyEvent.VK_DOWN:
                if(vel1 == 0)
                {

                }
                else
                {
                    vel1 = vel1 - 10;
                    carLoc.setVel(vel1);
                    cliMess.transmitState("d");
                }
                break;
        }

        carLoc.setCurrentImage(i1);
        carLoc.setVel(vel1);;
   }

   public void keyReleased(KeyEvent e) 
    {

   }

    public void actionPerformed(ActionEvent e)
    {
        repaint();
        cliMess.receiveState();
    }

    public void paintComponent(Graphics g)
    {
        for(int iVel1 = carLoc.getVel(); iVel1 > 0; iVel1 = iVel1 - 10)
        {
            carLoc.forwardCalc(); 
        }
        for(int iVel2 = carRem.getVel(); iVel2 > 0; iVel2 = iVel2 - 10)
        {
            carRem.forwardCalc();
        }

        super.paintComponent(g);
        int i1X = carLoc.getCarX();
        int i1Y = carLoc.getCarY();
        int i2X = carRem.getCarX();
        int i2Y = carRem.getCarY();
        int currentImage1 = carLoc.getCurrentImage();
        int currentImage2 = carRem.getCurrentImage();
        Color c0 = Color.black;
        //includes some graphics stuff, works fine, excluded here

        images1[currentImage1].paintIcon( this, g, i1X, i1Y);
        images2[currentImage2].paintIcon( this, g, i2X, i2Y);
    }
}

根据您计划发送的数据量,您可能需要调用flush()来确保在不需要内部缓冲区已满的情况下发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM