How to temporarily void a key code when an object is in a certain area of the screen?

So I have a character on screen, who can move left and right using the designated keys but can not move upwards until the character reaches a certain point in the screen(a ladder placed at the end). Once the character reaches this ladder, the user can press UP to climb up, however whilst they are climbing they are also able to move left and right using the keys assigned. Is there any way to make the left and right key command void whilst in the vicinity of the ladder, only being able to go up and then once passed up the ladder, the keys are then usable?

Tagged:

Answers

  • Have you done the code to tell if the character is in the vicinity of the ladder yet? It's easy enough once you've done that, the function to tell if the objects overlap should return a boolean value, character only moves left or right if keyPressed and overlap is false.

    Here are some examples of collision detection:

    http://www.jeffreythompson.org/collision-detection/index.php

    http://www.openprocessing.org/sketch/49289

  • You can use a boolean that decides the state of the player. You are already checking if the player is at the ladder, so checking if he's between the bottom and the top of the ladder should be no problem.

    void keyPressed(){ if(onLadder){ //up and down keys } else{ //left and right } }

Sign In or Register to comment.