【问题标题】:While Loop Crash in C# gameC#游戏中的while循环崩溃
【发布时间】:2014-05-02 19:58:49
【问题描述】:

我正在为一个项目创建 Kinect 文本冒险游戏。我知道代码不是那么好,但该项目更多地关注游戏的效果而不是游戏本身,所以它不必很复杂。这部分代码用于使用某些骨骼位置在游戏中进行。我遇到的问题是我正在尝试使用 while 循环,以便根据 f 的当前值,它会转到那个特定的房间。唯一的问题是当我使用 while 循环时,它总是在它开始之前就崩溃了。我不确定是什么问题

private void ProcessGesture(Joint head, Joint handleft, Joint handright)
    {

        while (f!=0)
        {


            if (handright.Position.Y > head.Position.Y && handleft.Position.Y > head.Position.Y && f == 1)
            {
                txtBox1.Text = "You find yourself at the foot of a large mountain, with a gaping cave at the front. \nYou have come here to find the Lost Sword of Gaia and you have heard that it lies \nhere in the Cave of Borlak the Red. You can move east";
                f = 2;
                this.btnangle.Visibility = Visibility.Hidden;
                this.slider1.Visibility = Visibility.Hidden;
                this.Degree.Visibility = Visibility.Hidden;
                this.helpbtn.Visibility = Visibility.Hidden;


            } 

            if (handright.Position.X > 0.3 && f == 2)
            {
                txtBox1.Text = "You walk up to the entrance of the cave and spot and \nlittle goblin wearing a suit of armour and holding a spear. \n'I'm so bored' he says. 'What I need is a good high five'\nYou can go west";
                f = 3;
                this.sadGoblin.Visibility = Visibility.Visible;
            }
            if ((f == 3 && handright.Position.Y > head.Position.Y) || (f == 3 && handleft.Position.Y > head.Position.Y))
            {
                Uri uri1 = new Uri("/SkeletalTracking;component/Resources/hgoblin.jpg", UriKind.Relative);

                ImageSource source1 = new BitmapImage(uri1);

                this.sadGoblin.Source = source1;

                txtBox1.Text = "'Ah, thank you kind stranger, for that I shall let you in'.\n The goblin steps to the side ";

                f = 4;
            }
            if (f == 4 && handright.Position.Y > head.Position.Y && handleft.Position.Y > head.Position.Y)
            {
                this.sadGoblin.Visibility = Visibility.Hidden;
                txtBox1.Text = "You are now in the main hall of the mountain. You can hear chanting and singing from the north.\nYou see a statue in the middle of the room and two doors to the left and right of it";
                f = 5;
            }
            if (f == 5 && handleft.Position.X < -0.3)
            {
                txtBox1.Text = "This is Borlak's treasure room. In here are all of the things he has colleted over the years\nSome bought and some not so bought.\nYour eyes are drawn to a necklace in a glass case in the center of the room.\nThere is also a picture of Borlak holding a giant chunk of ham\nYou can go east";
                f = 6;

            }
            if (f == 6 && handright.Position.X > 0.3)
            {
                f = 5;
            }
            //else if (f == 5 && handright.Position.X > 0.3)
            // {
            //     txtBox1.Text = "";
            // }




        }



    }

【问题讨论】:

    标签: c# while-loop crash kinect


    【解决方案1】:

    不要使用while循环,而是使用switch语句:

    int caseSwitch = 1;
    
    switch (caseSwitch)
    {
    
    case 1:
        Console.WriteLine("Case 1");
        break;
    case 2:
        Console.WriteLine("Case 2");
        break;
    default:
        Console.WriteLine("Default case");
        break;
    

    }

    【讨论】:

    • 显然应该使用switch。需要时从您的 main 调用此方法,它会执行所需的案例。 +1
    【解决方案2】:

    你在哪里退出循环?看起来你陷入了无限循环。游戏通常每次更新都会进行这样的检查。我认为你不应该在一帧游戏中停留在这个循环中。您应该检查 f 是什么并决定每一帧要做什么。

    编辑:另外,请发布您遇到的错误。更容易说出发生了什么。

    在我看来,去掉while循环,让它每帧检查'f'。

    【讨论】:

    • 是的 - f 绝不会设置为 0,因此循环永远不会停止。
    • 如果f 设置为async 并使用multi-threading,那么它将退出循环,但这里的for 循环不是正确的方法。
    【解决方案3】:

    f 的默认值为 0。如果在调用此方法之前未设置 f ( >=1 ),则它不会进入 while 循环。

    由于没有显示所有代码,也没有显示错误,“crash”是否意味着立即退出?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 2012-06-19
      相关资源
      最近更新 更多