Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Here is the gameplay. There is three condition.

The player step on a Switch-Tile and it became false.

1) When the Enemy step on it (trapped) AND the player step on it too, the Enemy will be destroyed.

2) But when the Enemy step on it AND the player DIDN'T step on it too, the Enemy will be escaped.

3) If the Switch-Tile condition is true then nothing happened. The effect is activated when the Switch tile is false (player step on the Switch-Tile).

Because there are a lot of Enemy and a lot of Switch-Tile, I have to use foreach loop.

The problem is after the Enemy is ESCAPED (case 2) and step on another Switch-Tile again, nothing happened to the enemy!

I didn't know what's wrong. The effect should be the same, but the Enemy pass the Switch tile like nothing happened (They should be trapped)

Can someone tell me what's wrong?

Here is the code :

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;                            
                    }
                }

            }
        }
share|improve this question
This is not the place for such localized questions. Use the debugger and step through your code. Write out your algorithm in words and see if your code matches. – Byte56 Dec 4 '12 at 14:35
At minimum, you can probably speed up this method by checking switch.Active outside the loop over enemies. Also, don't check booleans with == - just use the boolean itself (so, switch.Active && ....). Personally, the use also seems conceptually backwards (mostly because it's doing two things, not one). You also need to check whether the enemy is currently trapped (and time elapsed if so) - currently enemies are 'permanently stuck' because you keep adding to moveCounter every frame, not only when initially being trapped. – Clockwork-Muse Dec 5 '12 at 0:03

closed as too localized by Byte56, Laurent Couvidou, Trevor Powell, Josh Petrie, Gajoo Dec 9 '12 at 7:00

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Edit: [my questions removed] Your best bet would be to map out the decision tree (flowchart) including the steps performed by the player.

It appears that after the first enemy survives, any one of them will because the counter is not reset.

Noah

share|improve this answer
no, turns out is a local variable. So, I put it in Enemy class (now its enemy.moveCounter = 0). But then, it stays in slow position. I don't know why, still trying to fix it. Thx anyway – aldok Dec 4 '12 at 10:05
Answers are not the place for more questions. Sounds like you're trying to take part in a discussion. – Byte56 Dec 4 '12 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.