XNA 2D Top Down game - FOREACH didn't work for checking Enemy and Switch-Tile
Posted
by
aldroid16
on Game Development
See other posts from Game Development
or by aldroid16
Published on 2012-12-04T06:11:17Z
Indexed on
2012/12/04
11:31 UTC
Read the original article
Hit count: 245
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;
}
}
}
}
© Game Development or respective owner