c#游戏中的While Loop Crash

本文关键字:Loop Crash While 游戏 | 更新日期: 2023-09-27 18:01:37

我正在为一个项目制作一款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

使用switch语句代替while循环:

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;

}

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

编辑:也,请张贴你得到什么错误。这样更容易知道发生了什么。

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

f的默认值为0。如果在调用该方法之前没有设置f(>=1),那么它将不会进入while循环。

既然没有显示所有的代码,也没有显示错误,"crash"是否意味着立即退出?