C# XNA:2D Foreach碰撞检测问题,参考;";在当前上下文中不退出。
我的问题是,;当使用边界框进行碰撞检测时,我的第二个玩家的箭头并没有检测到它们正在撞击屏幕上的“锅”。我的第一个玩家可以很好地命中它们,当尝试复制玩家1的foreach语句以检测碰撞时,我遇到问题,game1.cs抛出错误,例如无法找到我使用过的变量,但我知道我已经声明了。非常困惑:3、还是一名学生在学习XNA做作业;所以对我放松点C# XNA:2D Foreach碰撞检测问题,参考;";在当前上下文中不退出。,c#,foreach,xna,collision,C#,Foreach,Xna,Collision,我的问题是,;当使用边界框进行碰撞检测时,我的第二个玩家的箭头并没有检测到它们正在撞击屏幕上的“锅”。我的第一个玩家可以很好地命中它们,当尝试复制玩家1的foreach语句以检测碰撞时,我遇到问题,game1.cs抛出错误,例如无法找到我使用过的变量,但我知道我已经声明了。非常困惑:3、还是一名学生在学习XNA做作业;所以对我放松点 case MenuScreens.Playing: { // This is allowin
case MenuScreens.Playing:
{
// This is allowing the player to go back to
// The main menu by pressing the escape key.
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
{
menuState = MenuScreens.MainMenu;
}
//Updating the player1 and player2
p.Update(gameTime);
p2.Update(gameTime);
foreach (Darknut a in darknutList)
{
// Collision for player to darknut
// Note : If bounding box "a" (the darknut) comes into contact with boundingbox "p" (the player)
// Then darknut will be destroyed : BUT with reduction in player health.
if (a.boundingBox.Intersects(p.boundingBox))
{
// Health of the player at "20" hitpoints. Every time the player gets hit by a darknut
// take 20 pixels of the healthbar from 200 in total.
p.health -= 20;
// Make darknut invisable = deletion
a.isVisible = false;
}
// check to see if any darknuts come in contact with the arrows if so destroy arrow and darknut.
for (int i = 0; i < p.arrowList.Count; i++)
{
// Returning the element at a specified element in the sequence
// Translate : If any of the darknuts bounding box intersecs with the arrows bounding box
// then destroy both by making the darknut and arrow invisible.
if (a.boundingBox.Intersects(p.arrowList[i].boundingBox))
{
// when player hits darknut with arrow give them "X" score
hud.playerScore += 5;
a.isVisible = false;
p.arrowList.ElementAt(i).isVisible = false;
}
}
foreach (Darknut t in darknutList)
{
if (t.boundingBox.Intersects(p2.boundingBox))
{
// Health of the player at "20" hitpoints. Every time the player gets hit by a darknut
// take 20 pixels of the healthbar from 200 in total.
p2.health -= 20;
// Make darknut invisable = deletion
t.isVisible = false;
}
for (int i = 0; i < p2.arrowList.Count; i++)
{
// Returning the element at a specified element in the sequence
// Translate : If any of the darknuts bounding box intersecs with the arrows bounding box
// then destroy both by making the darknut and arrow invisible.
if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
{
// when player hits darknut with arrow give them "X" score
hud.playerScore2 += 5;
t.isVisible = false;
p2.arrowList.ElementAt(i).isVisible = false;
}
}
}
a.update(gameTime);
t.update(gameTime);
}
// Loading Darknuts
LoadDarknuts();
// Loading Darknuts
//Updating the player
p.Update(gameTime);
//Updating the player2
p2.Update(gameTime);
// Updating the background
bg.Update(gameTime);
// Updating the player health if it hits zero then go to gameover State
if (p.health <= 0)
menuState = MenuScreens.GameOver;
break;
}
案例菜单屏幕。播放:
{
//这允许玩家返回到
//按escape键可打开主菜单。
KeyboardState keyState=Keyboard.GetState();
if(keyState.IsKeyDown(Keys.Escape))
{
menuState=MenuScreens.main菜单;
}
//更新player1和player2
p、 更新(游戏时间);
p2.更新(游戏时间);
foreach(黑色螺母列表中的黑色螺母a)
{
//玩家与黑暗者的碰撞
//注意:如果边界框“a”(黑色螺母)与边界框“p”(玩家)接触
//然后黑坚果将被摧毁:但是玩家的生命值会降低。
如果(a.boundingBox.相交(p.boundingBox))
{
//玩家的生命值为“20”生命值。每次玩家被黑暗怪物击中时
//从总共200个像素中选取20个像素的healthbar。
p、 健康-=20;
//使暗螺母不可见=删除
a、 isVisible=false;
}
//检查是否有任何深色螺母与箭头接触,如果有,则销毁箭头和深色螺母。
对于(int i=0;i 如果(p.health您尝试访问t
变量,但未声明块
case MenuScreens.Playing:
{
// This is allowing the player to go back to
// The main menu by pressing the escape key.
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
{
menuState = MenuScreens.MainMenu;
}
//Updating the player1 and player2
p.Update(gameTime);
p2.Update(gameTime);
foreach (Darknut a in darknutList)
{
// Collision for player to darknut
// Note : If bounding box "a" (the darknut) comes into contact with boundingbox "p" (the player)
// Then darknut will be destroyed : BUT with reduction in player health.
if (a.boundingBox.Intersects(p.boundingBox))
{
// Health of the player at "20" hitpoints. Every time the player gets hit by a darknut
// take 20 pixels of the healthbar from 200 in total.
p.health -= 20;
// Make darknut invisable = deletion
a.isVisible = false;
}
// check to see if any darknuts come in contact with the arrows if so destroy arrow and darknut.
for (int i = 0; i < p.arrowList.Count; i++)
{
// Returning the element at a specified element in the sequence
// Translate : If any of the darknuts bounding box intersecs with the arrows bounding box
// then destroy both by making the darknut and arrow invisible.
if (a.boundingBox.Intersects(p.arrowList[i].boundingBox))
{
// when player hits darknut with arrow give them "X" score
hud.playerScore += 5;
a.isVisible = false;
p.arrowList.ElementAt(i).isVisible = false;
}
}
foreach (Darknut t in darknutList)
{
if (t.boundingBox.Intersects(p2.boundingBox))
{
// Health of the player at "20" hitpoints. Every time the player gets hit by a darknut
// take 20 pixels of the healthbar from 200 in total.
p2.health -= 20;
// Make darknut invisable = deletion
t.isVisible = false;
}
for (int i = 0; i < p2.arrowList.Count; i++)
{
// Returning the element at a specified element in the sequence
// Translate : If any of the darknuts bounding box intersecs with the arrows bounding box
// then destroy both by making the darknut and arrow invisible.
if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
{
// when player hits darknut with arrow give them "X" score
hud.playerScore2 += 5;
t.isVisible = false;
p2.arrowList.ElementAt(i).isVisible = false;
}
}
t.update(gameTime);
}
a.update(gameTime);
//t.update(gameTime);
}
// Loading Darknuts
LoadDarknuts();
// Loading Darknuts
//Updating the player
p.Update(gameTime);
//Updating the player2
p2.Update(gameTime);
// Updating the background
bg.Update(gameTime);
// Updating the player health if it hits zero then go to gameover State
if (p.health <= 0)
menuState = MenuScreens.GameOver;
break;
}
案例菜单屏幕。播放:
{
//这允许玩家返回到
//按escape键可打开主菜单。
KeyboardState keyState=Keyboard.GetState();
if(keyState.IsKeyDown(Keys.Escape))
{
menuState=MenuScreens.main菜单;
}
//更新player1和player2
p、 更新(游戏时间);
p2.更新(游戏时间);
foreach(黑色螺母列表中的黑色螺母a)
{
//玩家与黑暗者的碰撞
//注意:如果边界框“a”(黑色螺母)与边界框“p”(玩家)接触
//然后黑坚果将被摧毁:但玩家生命值降低
if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
if (t.boundingBox.Intersects(p2.arrowList[i].boundingBox))