XNA:2D Foreach 碰撞检测问题,引用 "t" 在当前上下文中不存在。
XNA : 2D Foreach Collision detection issues with a reference of "t" not exiting in current context.
我的问题是;当使用边界框进行碰撞检测时,我的第二个玩家的箭头没有检测到他们击中屏幕上的 "pots" 的事实。我的第一个玩家可以很好地击中他们,当我试图复制玩家 1 的 foreach 语句来检测碰撞时我遇到了问题并且 game1.cs 抛出错误,例如无法找到我使用过的变量但我知道我已经声明.很迷茫:3,还是一个为了作业而学习XNA的学生;所以对我放轻松。
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;
}
您尝试访问 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;
}
darknuts 和玩家 2 的箭之间的碰撞检查似乎有错别字:
if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
应该是
if (t.boundingBox.Intersects(p2.arrowList[i].boundingBox))
我的问题是;当使用边界框进行碰撞检测时,我的第二个玩家的箭头没有检测到他们击中屏幕上的 "pots" 的事实。我的第一个玩家可以很好地击中他们,当我试图复制玩家 1 的 foreach 语句来检测碰撞时我遇到了问题并且 game1.cs 抛出错误,例如无法找到我使用过的变量但我知道我已经声明.很迷茫:3,还是一个为了作业而学习XNA的学生;所以对我放轻松。
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;
}
您尝试访问 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;
}
darknuts 和玩家 2 的箭之间的碰撞检查似乎有错别字:
if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
应该是
if (t.boundingBox.Intersects(p2.arrowList[i].boundingBox))