我正在为一个项目创建一个Kinect Text Adventure游戏。我知道代码不是那么好,但是项目比游戏本身更具有游戏效果,因此它不必复杂。这部分代码用于使用某些骨架位置进行游戏。我遇到的问题是我正在尝试使用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 = "";
// }
}
}
答案 0 :(得分:4)
使用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;
}
答案 1 :(得分:2)
你在哪里退出循环?看起来你陷入无限循环。游戏通常会像每次更新一样检查。我不认为你应该在一个游戏框架中保持这个循环。你应该检查f是什么,并决定每帧做什么。
编辑:另外,请发布你得到的错误。告诉会发生什么会更容易。
在我看来,取走while循环,让它每帧检查'f'。
答案 2 :(得分:0)
f的默认值为0.如果在调用此方法之前未设置f(> = 1),则不会进入while循环。
由于未显示所有代码,且未显示错误,“崩溃”是否意味着立即退出?