C# XNA 2D自上而下游戏-FOREACH没有';检查敌人和开关瓦片不起作用

C# XNA 2D自上而下游戏-FOREACH没有';检查敌人和开关瓦片不起作用,c#,xna,xna-4.0,C#,Xna,Xna 4.0,这里是游戏。有三个条件 玩家踩在开关磁贴上,该磁贴变为false。 1) 当敌人踩到它(被困)并且玩家也踩到它时,敌人将被摧毁。 2) 但是当敌人踩到它而玩家没有踩到它时,敌人就会逃脱。 3) 如果切换平铺条件为true,则不会发生任何事情。当开关磁贴为false(玩家踩下开关磁贴)时,该效果被激活 因为有很多敌人和很多切换块,我不得不使用foreach循环 问题是在敌人逃脱(案例2)并再次踩下另一个开关后,敌人什么也没发生! 我不知道怎么了。效果应该是一样的,但是敌人通过开关瓦片就像什么都没发

这里是游戏。有三个条件

玩家踩在开关磁贴上,该磁贴变为false。 1) 当敌人踩到它(被困)并且玩家也踩到它时,敌人将被摧毁。 2) 但是当敌人踩到它而玩家没有踩到它时,敌人就会逃脱。 3) 如果切换平铺条件为true,则不会发生任何事情。当开关磁贴为false(玩家踩下开关磁贴)时,该效果被激活

因为有很多敌人和很多切换块,我不得不使用foreach循环

问题是在敌人逃脱(案例2)并再次踩下另一个开关后,敌人什么也没发生! 我不知道怎么了。效果应该是一样的,但是敌人通过开关瓦片就像什么都没发生一样(他们应该被困住)

有人能告诉我怎么了吗

代码如下:

public static void switchUpdate(GameTime gameTime)
        {
            foreach (SwitchTile switch in switchTiles)
            {
                foreach (Enemy enemy in EnemyManager.Enemies)
                {
                    if (switch.Active == false)
                    {                    
                        if (!enemy.Destroyed)
                        {                                
                            if (switch.IsCircleColliding(enemy.EnemyBase.WorldCenter,
                                enemy.EnemyBase.CollisionRadius))
                            {                                    
                                enemy.EnemySpeed = 10; //reducing Enemy Speed if it enemy is step on the Tile (for about two seconds)
                                enemy.Trapped = true;
                                float elapsed = (float)gameTime.ElapsedGameTime.Milliseconds;
                                moveCounter += elapsed;
                                if (moveCounter> minMoveTime)
                                {
//After two seconds, if the player didn't step on Switch-Tile.
//The Enemy escaped and its speed back to normal
                                    enemy.EnemySpeed = 60f;
                                    enemy.Trapped = false;
                                }
                            }
                        }
                    }

                    else if (switch.Active == true && enemy.Trapped == true
                        && switch.IsCircleColliding(enemy.EnemyBase.WorldCenter,
                                enemy.EnemyBase.CollisionRadius)
                        )
                    {
//When the Player step on Switch-Tile and 
//there is an enemy too on this tile which was trapped = Destroy Enemy  

                        enemy.Destroyed = true;                            
                    }
                }

            }
        }
此代码永远不会为真,因为您仅在开关未激活时设置敌人是否被困住。一旦你设置为真,你应该真正打破循环,然后再次测试,看看你是否应该摧毁敌人

重新思考你想做的逻辑,并确保在每个阶段你都能获得敌人的正确信息等


另外,您不需要使用foreach循环。您可以很容易地使用for循环并在容器上迭代,或者如果您真的患有虐待狂,您可以自己为每个敌人编写简单的硬代码。Foreach循环只是解决此类问题的一种方法,仅仅因为它们可以使用,并不意味着您需要使用它们;)

您将太多的if语句嵌套在一起,这会导致代码的高度复杂性,并导致代码可读性和可维护性差。如果您能更好地重构代码,上面的大部分代码都可以工作
           else if (switch.Active == true && enemy.Trapped == true
                && switch.IsCircleColliding(enemy.EnemyBase.WorldCenter,
                        enemy.EnemyBase.CollisionRadius)
                )
            {
                //When the Player step on Switch-Tile and 
                //there is an enemy too on this tile which was trapped = Destroy Enemy  

                enemy.Destroyed = true;                            
            }