Jump to content

- - - - -

Solid Top-Down Movement Example

  • Please log in to reply
4 replies to this topic

#1 wimlore


    GMC Member

  • GMC Member
  • 124 posts

Posted 24 November 2011 - 11:11 PM

  • Title: RW's Top-Down Movement Example
  • Description: This example is intended to provide you with a method of allowing arrow-key events to override each other equally.
  • GM Version: :GM8:
  • Registered: no
  • File Type: .gmk
  • File Size: 0.01 MB
  • File Link: rw_td_movement_example from Host-A.net
  • Required Extensions: N/A
  • Required DLLs: N/A
Thank you for your interest in my example!
Please read the Help File included in the example to have a complete understanding of how this movement method works.

The Issue: Did you ever notice how the events for the right arrow key override the left arrow key? This is due to the event order. Just like a stack of 'if statements', where the one on top will always execute first. I have tried many ways to work around this, and so far this is the best alternative I could come up with.

Mechanics: The object has five variables declared within it's create event. Four of the variables are to represent the four directions: up, down, left, and right. The fifth variable represents the direction the object will eventually go.

Each of these variables will increment in value while the corresponding keys are being pressed.

In the 'Key-Press Event' of an arrow key, the values of the other directions gets reset to zero.

In the 'Key-Release Event' of an arrow key, the value of that direction gets reset to zero.

In the Step Event, the variable 'max_value' uses the function 'max()' on all four directional values. Then, a switch statement compares the max_value variable with the four directional values to determine which direction the object will go, using the 'motion_set()' function.

The Results: Each Arrow Key Event has the capability of overriding each other. Thus, allowing the user to button mash arrow keys with more consistent movement.

Object's Information(Incase you do not want to download the example)

GMC Search Tags
  • 0

#2 GMkizzle


    GMC Member

  • GMC Member
  • 384 posts

Posted 20 January 2012 - 09:11 AM

Okay almost solid, but there is a problem.
If you hold 'up' and press the 'left' and 'right' keys, the player will only move left and right, and in no way north/up.

Nice work so far :)
  • 0

Posted Image

#3 wimlore


    GMC Member

  • GMC Member
  • 124 posts

Posted 27 January 2012 - 10:53 PM

Hello :) Firstly, I want to say thanks for sharing my interest in this endeavor!

What you described is exactly what I wanted to happen! :thumbsup:

If I were to 'Hold' UP and 'Press' LEFT and/or RIGHT the user should only go left or right, depending on which key was pressed last.
Since the last pressed key is always the direction the user will go.
The same works if you were to 'Hold' LEFT and 'Press' UP and/or DOWN.

Sorry maybe I should have mentioned this is 4-Directions only, and no diagonal? :unsure:
  • 0

#4 Thagrahn


    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 24 February 2012 - 01:37 AM

So, this is just to get around GameMaker checking the keys in a set order.

The code seems a little over the top, but is similar to what I have seen done to make sure the player isn't holding the left and right movement keys at the same time.

Wouldn't be to hard to edit this so the diagonals can be used as well, but otherwise looks like it does what it was designed for.
  • 0

#5 wimlore


    GMC Member

  • GMC Member
  • 124 posts

Posted 05 April 2012 - 08:13 PM

You're right, Thagrahn. The code is a bit over the top. Also, it is actually really easy to adjust this example to work for diagonal movement. Which is probebly why no one has asked how. But for kicks, I rewrote some code and got a decent method. Hope this doesn't scare away all the drag & droppers. After all, it's just a little copy and paste :thumbsup:

Here is the modified obj_ball_advanced's information

First what I did was I got rid of all the key-press events, since resetting the directional values every key-press would prevent me from being able to move diagonally.
Then I got rid of max_val, since I would want to be moving in more than 1 direction at a time.
Finally I changed the movement style to hspeed and vspeed instead of using motion_set(), and compressed everything into the step event.
  • 0