• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

java 4次浏览

我正在开发小规模的client1/ client2, server (threaded) TCP game。在尝试处理延迟问题时,我意识到transmitState()中存在缺陷。它迫使不必要的信息进入通讯流,从而导致速度缓慢,从而使汽车停放在JPanel中的不同位置,这基本上是错误的。所以我把它拿出来了。

但是这样做却造成了一些问题,尽管所有的通讯,协议,线程,“信使”以及所有的东西都在工作,但根据我的逻辑,我不允许使用repaint()transmitState()来暂停游戏的开始。和receiveState()按照逻辑顺序执行任务。

我真的看不到哪里出了问题。有什么建议么?

注意:if案例陈述中的keyPressed条件进行测试,以使子画面在左右等方面“旋转” 360度,并且vel是速度,因此不能超过100。carRem =远程汽车客户端。我知道有很多代码,但是过去(正确)因为没有包含足够的代码而被告知,所以我认为所有这些都显示了需要显示的内容(如果我错了,请告诉我:)) 。

传输状态:

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

接收状态:

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

测试状态:

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(具有大多数客户端功能):

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()以确保发送数据时不需要内部缓冲区已满。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)