Jump to content


Photo

Top Down 8 Directional Movement


  • Please log in to reply
7 replies to this topic

#1 Lucien

Lucien

    GMC Member

  • GMC Member
  • 66 posts

Posted 30 March 2010 - 05:07 PM

I was wondering how I would go about giving a sprite 8 directions to move in, instead of four, using the arrowkeys. I want to assign of walking animation to each direction (i have diagonal sprites).

The drag n drop only lets me use the arrowkeys, but not check whether left AND up are pressed, and so on.

any help would be appreciated, thanks.
  • 0

#2 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 30 March 2010 - 05:10 PM

x+=keyboard_check(vk_right)-keyboard_check(vk_left);

y+=keyboard_check(vk_down)-keyboard_check(vk_up);

  • 0

#3 YellowAfterlife

YellowAfterlife

    GMC Member

  • Global Moderators
  • 3771 posts
  • Version:GM:Studio

Posted 30 March 2010 - 05:10 PM

Use gml pieces?
key_hor = keyboard_check(vk_right) - keyboard_check(vk_left)
key_vert = keyboard_check(vk_down) - keyboard_check(vk_up)
key_speed = min(1,abs(key_hor)+abs(key_vert))
key_direction = point_direction(0,0,key_hor,key_vert)
after executing this, you'll have keyboard direction and speed variables.
  • 0

#4 Lucien

Lucien

    GMC Member

  • GMC Member
  • 66 posts

Posted 30 March 2010 - 05:17 PM

Thanks for the help guys, but I'm still kind of confused by your post YellowAfterLife. I'm actually quite new to this whole thing.
  • 0

#5 antaios

antaios

    GMC Member

  • New Member
  • 82 posts

Posted 31 March 2010 - 03:57 AM

key_hor = keyboard_check(vk_right) - keyboard_check(vk_left)

this checks if the right or left key is pressed, if both, then theres no horizontal distance, if the left key is pressed, then the x movement is -1, and 1 for right key


key_vert = keyboard_check(vk_down) - keyboard_check(vk_up)

same as horizontal, up is -1, down is 1


key_speed = min(1,abs(key_hor)+abs(key_vert))

the "abs" parts just make negative values, "minuses" into plus's, so you get an added up number of 0,1 or 2, instead of -2,-1,0,1 or 2
then the "min" part capps it at 1, so you move at the same speed, no matter what direction you move.

you should add a * something at the end like:
key_speed = min(1,abs(key_hor)+abs(key_vert)) * maxspeed
then, it ill define a max speed greater than one, but also allow for zero (since 0 is < one, and your using the "minimum" function)


key_direction = point_direction(0,0,key_hor,key_vert)

this finds the direction your player should move in, based on what keys are pressed, ie if hor = -1 and vert = -1, then you angle would be... 135 degrees
(try it with pencils or something)

then to add the speed and direction to your player, just use
if you want to use hspeed and vspeed:
hspeed = lengthdir_x(key_speed,key_direction)
vspeed = lengthdir_y(key_speed,key_direction)

or straight to x and y values:
x += lengthdir_x(key_speed,key_direction)
y += lengthdir_y(key_speed,key_direction)
  • 0

#6 Izkhanilov

Izkhanilov

    GMC Member

  • GMC Member
  • 209 posts

Posted 31 March 2010 - 05:50 AM

I actually was confronted with this issue in the game I'm making (Which has been in development hell for a year now, It's kinda like Duke Nukem ForNever)

The trick is to not focus on the standard movement commands. Instead, use jump-to positions like so:



;) Event Left-key
  • :whistle: Jump relative to location -4, 0

And


:P Event Right-key
  • :P Jump relative to location 4, 0


This works the same going up and down. So when you press W and A or Up and Left at the same time, it will cause the object to jump relative to it's own position ( -4, -4 ) for each step that those keys are pressed. Make sense?

Hope this helps!
  • 0

#7 antaios

antaios

    GMC Member

  • New Member
  • 82 posts

Posted 31 March 2010 - 06:07 AM

not to be rude or anything Izkhanilov, but you're forgetting that doing what you suggested would lead to faster movement diagonally than horizontally or vertically
  • 0

#8 katejohn

katejohn

    GMC Member

  • New Member
  • 1 posts

Posted 10 May 2010 - 09:13 AM

You can use combination of arrow keys or arrow key with an other key. Simple arrow key will be used to drag and drop options but combination can be used for directions. I have read it somewhere that these keys send two codes so you can check them. I have read a useful article on article directory, articleworlddb and you can also try to find something useful for you.

Kate
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users