我正在开发一个小规模的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(( 以确保在发送数据时不需要内部缓冲区已满。