How do I find "most recent key down"?

0 favourites
  • 8 posts
From the Asset Store
Random Maze Generator with Door & Key System - tutorial capx
  • Hi all,

    I am trying to create a nice character movement system and I am employing the 8 direction behaviour (only using 4-directions) to do so.

    How I have it working is when an arrow key is down, a boolean is set to true which moves the character in the corresponding direction. So "right arrow key down" sets "IsRight" to "True" and the player moves right.

    The problem is when I am holding the right arrow key, then hold down the up arrow key (while still holding the right arrow key), the player just keeps moving right because this boolean was set first.

    What I would like to do is only set the boolean of "most recent key down" to true as I think this will solve my problem. Is there a way to do this?

    (By the way I have tried setting the boolean on key-pressed but I need to set it on key-down for several reasons.)

  • I don't really understand the need for the individual Booleans you are defining. In every place you would use your Boolean, you can just reference if the key is down. The added Boolean value just bloats the system and makes it harder to read/debug.

    As for the question about keeping track of what is the last key pressed, you could implement a stack which stores key press information and check the last key on the stack when trying to move. See the attached example. The stack is built using an array and when an arrow key is pressed, the direction is added to the front of the stack. When the key is released, the direction is removed. Then, to determine the direction the player will move, the front item is checked.

    If you really do need the Boolean values, just replace the "is down" conditions with your Booleans.

    drive.google.com/file/d/1unE5jG0C7lq8iU_Lrji5ltvD616Wq9H-/view

  • the.sand Could you explain a little more what you are trying to achieve? It sounds like you are trying to recreate the already built in movement?

    So "right arrow key down" sets "IsRight" to "True" and the player moves right.

    The 8-direction behaviour will already move the player right when the right arrow key is pressed by default.

  • calminthenight The built in movement controls don't allow for overriding one direction when another is pressed in the way the OP describes. If you try it you will see, that up and down will override left and right but left and right won't override up and down. Also, opposing directions cancel each other out. The OP wants the most recent pressed directional key to move the character even if another key is still pressed.

  • InDWrekt

    Thanks so much for your response!

    An array is more complex than I hoped but if it works it works!

    I will look into it.

    Thanks again!

  • Ah I understand now. This should do the trick: 1drv.ms/u/s!AkmrWgxeuxlKhIcuHeHIGlbJh0a5mg

    I just did it for left and right but it is easily expanded to up and down too

  • the.sand, Don't think of it as an array. Think of it as a stack. You are adding and removing actions from the stack. Only the action on top of the stack is active. The others activate as the top action is removed and the next is uncovered.

    Also, if you do take a look at the example by calminthenight, you will notice without the stack, more events and variables are required just to get 2 directions to work. To get the other 2 directions, you would have to double both the variables and events. That makes the code more difficult to work with and debug when something goes wrong. It is a fine example if you don't want to make use of the stack but, you can see the event sheet can very quickly become hard to follow.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • calminthenight InDWrekt

    Thank you both for your help!

    I will look into both solutions and see which ones works best (and which one I am able to program myself).

    Thanks again for your time and effort!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)