0
\$\begingroup\$

I am creating a JavaScript game that is currently implemented as follows:

  • Run a game loop
  • Check for arrow keys pressed
  • When arrow is pressed, calculate the new player coordinates
  • if new player coordinates result in a collision, do not move the player, but instead append a status message to the onscreen status box

My issue is that if I am holding down the arrow key and collide with an object, that onscreen status box gets hundreds of messages appended to it.

I originally thought that a way to get around this would be to check the last line in the status box and if it is the same as the new line then not to append the new line. However, this will cause a problem if the user walks away and comes back to the same object again, in this case the user SHOULD see a second message appended to the box.

What is a proper way to deal with this situation?

\$\endgroup\$
2
  • \$\begingroup\$ Is your aim to display the right message without duplicates or to only trigger the collision once? \$\endgroup\$ Oct 3, 2019 at 15:05
  • \$\begingroup\$ @TomTsagk I suppose to only trigger the collision once which would take care of the messages. \$\endgroup\$
    – kojow7
    Oct 3, 2019 at 17:22

3 Answers 3

1
\$\begingroup\$

Have the character keep track of which objects it is currently "touching". Whenever collision happens with a new object, add it to the list, and trigger a collision event (The message in your case).

When collision happens with an object it is already touching, do position collisions (so the character doesn't clip through), but don't trigger the event. If the character moves away from the object it was touching, remove it from the list.

This will work with both 2D or 3D graphics.

\$\endgroup\$
0
\$\begingroup\$

What is this message box used for? Is it just for debugging or is it part of a gameplay feature?

You would need to add booleans to your code and conditions, which check, whether you should print the message or not. What kind of condition you add would depend on what you want. You need to ask yourself what needs to happen in order to UNLOCK the status box (i.e. enable printing messages again). Do you want to make it dependable on time? For example, the status box prints a message upon collision and with continious collision you print every x seconds. Or whether you want to only catch "first collisions". In that case you would need to re-enable the status box once the player stopped moving into the object.

I don't know what your code structure looks like in detail. Would help, if you could post the collision detection of the player object with other objects and the code for the status box.

\$\endgroup\$
0
\$\begingroup\$

What about simply checking if the message box exists. When it doesn't you can make one and if it does then don't make one. This assumes it gets removed. If it doesn't get removed you could make a boolean which is set to false when the player doesn't collide. but true when they do. If they collide you check the value of the bool, if its false you add the message and set it to true. Otherwise you don't add the message.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .