1

可悲的是,我第三次问这个问题,我认为每次我问它都没有意义,但是是的,让我们再次尝试更多的单词/代码,还有照片和简单的测试,我有 2 个类,一个接收数据包并从中获取值它们和另一个应该使用这些值,但我未能从此类中获取它们注意:这些值将由新线程使用,因此让我们显示代码

namespace ProxyParadise.Network.Packets
{

    public class PacketHandler
    {
        public PacketHandler() // takes no parameters using get/set , failed take 1
        {
            Console.WriteLine("defining an object to get x,y take 1, value now is (" + actualX + "," + actualY + ")");
        }
        public void getxy(ushort Actualx, ushort Actualy) //using get and set with parameters failed take 2
        {
            Console.WriteLine("defining an object to get x,y take 2, value now is (" + actualX + "," + actualY + ")");
            actualX = actualX; Actualy = actualY;
        }
        public ushort actualX { get; set; }
        public ushort actualY { get; set; }
public unsafe void HandleServer(byte[] data)
        {
            try
            {
                    var type = *((ushort*)(ptr + 2));
                    switch (type)
                    {
                        case 10010:
                            {
                                if (BitConverter.ToUInt16(data, 24) != 0)
                                {
                                    actualX = BitConverter.ToUInt16(data, 24);
                                    actualY = BitConverter.ToUInt16(data, 26);
                                    Console.WriteLine("account coords updated to (" + actualX + "," + actualY + ")");
                                }
                                break;
                            }
                    }

这是一张测试这个的图片,它会在每次更新时在控制台上写行,一旦我收到这个数据包 测试 1 现在我试图在另一个类上获取这些值,所以我对它们做一些事情并将数据包发送到服务器,所以这是另一堂课(全班,所以谁说我不应该发布重要部分是有意义的)

public class ClientBase
{
    GameUser role2;
    PacketHandler ph = new PacketHandler();
        public ushort X, Y;
        PacketHandler ph2 = new PacketHandler();

    public ClientBase(GameUser role)
    {
        role2 = role;
        Thread T = new Thread(HuntThread) { Name = "Hunt Thread" };
        T.Start(this);
        Console.WriteLine("with name : " +T.Name +  " with execution context : "+T.ExecutionContext +" with state : "+ T.IsAlive + " with id : " + T.ManagedThreadId);
       T.Join();
    }
    Monster GetNextkill()
    {
       // byte Range = 18;
        Monster CurrentTarget = null;
        foreach (KeyValuePair<uint, Monster> Pair in Dictionary.Clone(Dictionary.Monsters))
        {
          //  if (Math.Max(Math.Abs( - Pair.Value.X), Math.Abs(pp.Y - Pair.Value.Y)) > 18)
          //  {
          //      Dictionary.Monsters.Remove(Pair.Key);
          //  }
           // else if (Math.Max(Math.Abs( - Pair.Value.X), Math.Abs(pp.Y - Pair.Value.Y)) <= Range)
           // {
           //     Range = (byte)Math.Max(Math.Abs(this.X - Pair.Value.X), Math.Abs(this.Y - Pair.Value.Y));
                CurrentTarget = Pair.Value;
             //   Console.WriteLine("there is " + Dictionary.Monsters.Count + " monster around you");
          //  }
        }
        return CurrentTarget;
    }
    public void HuntThread(object Sender)
    {
        try
        {
            ClientBase Client = Sender as ClientBase;
            while (true)
            {
                Monster Target = GetNextkill();
                if (Target != null)
                {
                    Thread.Sleep(1000);
                    ProxyParadise.Network.Packets.PacketStructure ps = new ProxyParadise.Network.Packets.PacketStructure();
                    //       ps.__Packet1022XP(Target.UID, Target.X, Target.Y); 
                    int xxx = Target.X - 1;
                    int yyy = Target.Y - 1;
                    ph2.getxy(X, Y);
                    Console.WriteLine("x and y in the thread is " + X + Y);
                    ps.__Packet10010(Target.X, Target.Y, role2, "normal", "server", ph.actualX, ph.actualY);
                    Thread.Sleep(1000);
                    ps.__Packet10010(Target.X, Target.Y, role2, "sdasdsa", "client", role2.X, role2.Y);
                    Thread.Sleep(1000);
                    ps.__Packet1022NONEXP(Target.UID, Target.X , Target.Y, role2);
                    //if fatal strike is on attack the monster 
                    //else
                    //jump next to the target and attack
                    Dictionary.Monsters.SafeRemove(Target.UID);

                }
                return;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("expection error : " + ex);
        }
    }
    public uint UID;

}

所以现在的问题是设置/获取和定义对象以获取值的两种方式(我通过尝试从方法中获取它们也做了两次结构,但在两种情况下都失败了)

现在这是测试图像 在此处输入图像描述

我怀疑为什么它是零,我有时认为这是因为新对象而其他时候我认为这是因为线程

这次我真的很想得到帮助,因为我已经尝试了 4 天,并且做了很多我听说过的方法(但不是锁),我真的很感谢你阅读这篇文章并帮助我度过你的时间和我对此表示赞赏的信息:) 也感谢将修复我的拼写和语法的 mod

4

1 回答 1

0

您在此处显示的参数和属性名称中的大写/小写字母是否与您正在运行的代码中的完全相同?

public void getxy(ushort Actualx, ushort Actualy) //using get and set with parameters failed take 2
{
    Console.WriteLine("defining an object to get x,y take 2, value now is (" + actualX + "," + actualY + ")");
    actualX = actualX; Actualy = actualY;
}

因为如果我更改参数的名称,您应该会明白为什么这看起来不对。

public void getxy(ushort inputx, ushort inputy) //using get and set with parameters failed take 2
{
    Console.WriteLine("defining an object to get x,y take 2, value now is (" + actualX + "," + actualY + ")");
    actualX = actualX; inputy = actualY;
}

这当然可以解释为什么它们总是为零,它们永远不会改变。

您是否尝试过在代码中放置断点并在调试中单步执行代码,特别是 getxy 方法以准确查看每次输入和输出值是什么?

于 2012-09-13T14:18:31.940 回答