Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If the player steps on a Button-Tile when its true, it becomes false. If the player steps on a Button-Tile when it is false, it becomes true.

The problem is, when the player stands on (intersects) the Button-Tile, it will keep updating the condition. So, from true, it becomes false. Because its false and the player intersects it, it becomes true again. True-false-true-false and so on.

I use ElapsedGameTime to make the updating process slower, and the player can have a chance to change the Button to true or false.

However, it's not the solution I was looking for. Is there any other way to make it keep in False/True condition while the Player is standing on the Button tile?

share|improve this question

2 Answers 2

You may just want another bool value to act as a mark saying whether or not the button can be pressed again.

One route could be to start with a bool we'll call:

bool buttonCanBePressed = true;

in the part of your code where you are checking if the player is on the button you could say:

if (playerIsOnButton && buttonCanBePressed)
{
buttonCanBePressed= false; //or buttonCanBePressed= !buttonCanBePressed;

// do button specific logic here
}

You would then have a check to see if player is still on the button

if (!playerIsOnButton)
buttonCanBePressed = true;

This way the player can only activate the button once until he has left the button then returns.

share|improve this answer

A more generic version of Dialock's answer which does not require extra state for every triggerable tile is to simply only check the button press state when the player's primary tile position has changed. Something like:

Vec2 old_tile_pos = player.GetTilePos();
player.UpdatePosition();
Vec2 tile_pos = player.GetTilePos();
if (tile_pos != old_tile_pos)
  ProcessTileEffect(tile_pos);

That all assumes that you have a clear definition of the primary tile the player is on. In games where the player moves one whole tile at a time, that's pretty obvious. In games where the player can smoothly move, it could just be the the under the player's center position.

If you want a smoothly moving player and want tiles to be triggered when the player is only partially standing on them, you'll need to extend the logic above to check multiple tile positions. Get the list of all tiles the player is colliding with, move the player, then get the new tile collision list. Any tile in the first but not in the second list is one the player stepped off of. Swap that around and you've got the tiles the player stepped onto.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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