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\$
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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