关于坦克在其他客户端能够显示,但是不能运动的错误的定位

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class TankMoveMsg implements Msg{
	int msgType = Msg.TANK_MOVE_MSG;

	int id;
	Direction dir;
	TankClient tc;

	private boolean exist;
	public TankMoveMsg(int id, Direction dir) {
		this.id = id;
		this.dir = dir;
	}
	public TankMoveMsg(TankClient tc) {
		this.tc = tc;
	}
	@Override
	public void send(DatagramSocket ds, String IP, int udpPort) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeInt(msgType);
			dos.writeInt(id);
			dos.writeInt(dir.ordinal());
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] buf = baos.toByteArray();
		
		try {
			DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress(IP,udpPort));
			ds.send(dp);
		}catch(SocketException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}	
	}
	@Override
	public void parse(DataInputStream dis) {
		try {
			int id = dis.readInt();
			if(tc.t.id == id){
				return;
			}
			Direction dir = Direction.values()[dis.readInt()];
			exist = false;
			for(int i=0;i<tc.tanks.size();i++){
				Tank t = tc.tanks.get(i);
				if(t.id==id){
					t.dir = dir;
					exist = true;
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
}

以上为坦克运动的消息类,用来在客户端坦克运动时向服务器端发送坦克运动的消息,即发送坦克改变方向的消息,然后服务器端收到此消息后将消息发送给所有的客户端,其他的客户端收到消息后,解析消息,整个过程都正常发生,客户端坦克运动时,服务器端有消息发出,客户端也有消息收到,但是另外一个客户端的坦克就是不动弹,既然数据消息在服务器端和各个客户端都正确的传送了,坦克不动,说明方向没有改变,在给坦克赋值新方向的时候出现问题了,去TankMoveMsg类中找所有与方向有关的。发现在parse方法中
t.dir = dir;
该句是给坦克容器中的所有坦克挨个取出来赋值新方向,但是之前写成了this.dir = dir;所以没有个容器中的坦克赋予新方向,当然坦克也就不会动了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值