XNA: 2D Foreach碰撞检测问题与参考"t"在当前上下文中不存在

本文关键字:quot 上下文 不存在 问题 碰撞检测 参考 2D XNA Foreach | 更新日期: 2023-09-27 17:50:44

我的问题是;当使用边界框进行碰撞检测时,我的第二个玩家的箭头无法检测到他们击中屏幕上的"锅"。我的第一个玩家可以很好地击中它们,当尝试复制玩家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;
                }

XNA: 2D Foreach碰撞检测问题与参考"t"在当前上下文中不存在

似乎在暗黑坚果和玩家2的箭头之间的碰撞检查中有一个打字错误:

if (t.boundingBox.Intersects(p.arrowList[i].boundingBox))
应该

if (t.boundingBox.Intersects(p2.arrowList[i].boundingBox))

您尝试访问没有块的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;
            }