Jump to content


Photo

8 Way Movement


  • Please log in to reply
17 replies to this topic

#1 kokoslover

kokoslover

    GMC Member

  • New Member
  • 55 posts

Posted 13 July 2007 - 01:13 AM

This is probably really simple, but I am like dyslexic with code. I've got four-way movement at the moment. But I want to be able to go all 8 directions. Seems like it should have been asked before but it didn't come up when I searched.

Edited by kokoslover, 13 July 2007 - 02:07 AM.

  • 0

#2 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6274 posts
  • Version:GM:Studio

Posted 13 July 2007 - 01:35 AM

Um...what is it that you want us to tell you?
  • 0

#3 kokoslover

kokoslover

    GMC Member

  • New Member
  • 55 posts

Posted 13 July 2007 - 01:41 AM

But I want to be able to go all 8 directions.

<{POST_SNAPBACK}>


Right now my character moves in four directions. I want eight but I have trouble with the code. I have if left is pressed, move 180 with speed 4 and such... But I can't seem to get it be able to check for two buttons being press, E.G. down and left moving diagonally down-left.
  • 0

#4 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6274 posts
  • Version:GM:Studio

Posted 13 July 2007 - 01:46 AM

Use a few if statements that use keyboard_key. Instead of using the variable speed, just set x and y reletive to where they were, depending on what keys are pressed
  • 0

#5 CoderBlue

CoderBlue

    GMC Member

  • New Member
  • 668 posts

Posted 13 July 2007 - 01:50 AM

xy wont help much, unless your math is good with slopes your movement will be ugly

you want to set a direction and a motion when the key is pressed

for example..

0 for E
45 for NE
90 for N
and etc

best to bind these to the number pad as well
  • 0

#6 ~Dannyboy~

~Dannyboy~

    ~hoqhuue(|~

  • GMC Member
  • 2144 posts
  • Version:GM8

Posted 13 July 2007 - 02:00 AM

I think what you need to do is in one key event (eg left) from the control tab drag in the "if an expression is true" action and paste in the box: "keyboard_check(vk_up)" this will check if the left and up keys are pressed, so you can check if the two keys are pressed :D

vk_up can be replaced with vk_left, vk_right, and vk_down for the different keys :(
  • 0

#7 kokoslover

kokoslover

    GMC Member

  • New Member
  • 55 posts

Posted 13 July 2007 - 02:07 AM

Thanks. :(
  • 0

#8 mr.gibblet

mr.gibblet

    GMC Member

  • New Member
  • 374 posts

Posted 13 July 2007 - 02:07 AM

You need to have key events in your step event.

Delete all the other key events and add this code into your step:
if(keyboard_check(vk_left)) {
   x-=1;
}
if(keyboard_check(vk_right)) {
   x+=1;
}
if(keyboard_check(vk_up)) {
   y-=1;
}
if(keyboard_check(vk_down)) {
   y+=1;
}

That should give you 8 directions :(
  • 0

#9 orange451

orange451

    GMC Member

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

Posted 13 July 2007 - 02:11 AM

STEP EVENT:
//go left
if keyboard_check(vk_left){
x-=2
}

//go right
if keyboard_check(vk_right){
x+=2
}

//go up
if keyboard_check(vk_up){
y-=2
}

//go up
if keyboard_check(vk_down){
y+=2
}


if keyboard_check(vk_left) and keyboard_check(vk_up){
x-=2
y-=2
} 


if keyboard_check(vk_right) and keyboard_check(vk_up){
x+=2
y-=2
} 

if keyboard_check(vk_right) and keyboard_check(vk_down){
x+=2
y+=2
} 

if keyboard_check(vk_left) and keyboard_check(vk_down){
x-=2
y+=2
}

  • 0

#10 mr.gibblet

mr.gibblet

    GMC Member

  • New Member
  • 374 posts

Posted 13 July 2007 - 02:13 AM

Actually, Orange.

If you have separate IF events with the keys, it will add on the values at the same time.
You do not need the if(keyboard_check(BLAH) && BLAHBLAH) to make the 8 directions.

You will only need the AND events if you are using else ifs to connect the events.
  • 0

#11 orange451

orange451

    GMC Member

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

Posted 13 July 2007 - 02:17 AM

Actually, Orange.

If you have separate IF events with the keys, it will add on the values at the same time.
You do not need the if(keyboard_check(BLAH) && BLAHBLAH) to make the 8 directions.

You will only need the AND events if you are using else ifs to connect the events.

<{POST_SNAPBACK}>


well S.O.R.R.Y. I just started gamemaker so thats what I thought would work because thats what I used in a mario/pokiemon game I made this morning(I got board, it sucked) but that code worked.
  • 0

#12 mr.gibblet

mr.gibblet

    GMC Member

  • New Member
  • 374 posts

Posted 13 July 2007 - 02:21 AM

At least you are helping someone :(
  • 0

#13 kokoslover

kokoslover

    GMC Member

  • New Member
  • 55 posts

Posted 13 July 2007 - 02:33 AM

Oh wow, that's exactly what I was looking for, how would I add in the sprite changing to yours Gibblet? Just stick in the sprite changing code?
  • 0

#14 orange451

orange451

    GMC Member

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

Posted 13 July 2007 - 01:34 PM

At least you are helping someone :wacko:

<{POST_SNAPBACK}>

wait, there is something wrong here. say he wants 8 dir sprites too, with you code he can only do 4 dir sprites but the character can move 8 dir, with mine it can move 8dir and it can have 8-dir sprites too.
  • 0

#15 dovonob

dovonob

    GMC Member

  • New Member
  • 172 posts

Posted 13 July 2007 - 01:49 PM

even though you have a solution i thought i'd tell you an alternate way without any code.

KEY PRESS EVENT FOR LEFT

(use the set speed drag and drop icon)
hspeed=-5

KEY PRESS EVENT FOR RIGHT

hspeed=5

KEY PRESS EVENT FOR UP

vspeed=-5

KEY PRESS EVENT FOR DOWN

vspeed=5

in key release for each button set hspeed or vspeed back to 0

this way diagonal 8 directional movement is done automatically. Only slight problem you get is that moving diagonal the speed isn't 5 it actually turns out as 7. something.

also you can put the change sprite drag and drop in the key press event and it will change the sprite when you press the button.

edit: actually although it sounds simpler, in the long run it's not so ignore me, i can't be bothered to explain switch statements, getting AI to move in the same way blah blah, the other solution is better :wacko:

Edited by dovonob, 13 July 2007 - 01:53 PM.

  • 0

#16 tbpb2010

tbpb2010

    GMC Member

  • GMC Member
  • 57 posts

Posted 13 July 2007 - 03:12 PM

i've tried all of those annoying and confusing methods in the past and i still ended up just using 4 different direction sprites (i could never figure out the actual solution) but if you think this may help check it out:

if you have your character using the "speed method" (if keyboard_check(vk_left) hspeed=-5 "<this is not relative" or hspeed-=5 "<this is relative") then to set the directional sprite depending on the speeds you have to actually use the direction. for example "if direction = 45 {sprite_index = chacter_facing_up_right}" or something like that but to use this method correctly you must have the speeds either set in fixed directions (North,Northeast,East,Southeast.etc) without the charcter's direction changing to 46 or 32 (not in the 8 compass directions) or to set a sprite for all 360 directions which is very time consuming and probably doesn't even work (i said i could never find the solution) but those might work for the "speed method." oh yeah and if you're worried about the speed being too fast for the diagonal directions like the speed is 5 going up and the speed is 5 going left but the speed is 7 going up-left, just limit the speed by putting this in the step event (if speed > 5 {speed = 5}) this is checking if the overall speed of the object is greater than 5 and if it is it'll make the speed go back to 5 thus limiting the speeds in every direction to 5!

the "jump method" if keyboard_check(vk_right) {x+= 3} and functions like that might require a check in the step event like "if keyboard_check(anykey) x=x+=3 and y=y-=3 {sprite_index = character_facing_upright}" and before all that you've already set the 4 directions to jumping directions like the left key means x-=3. i haven't really messed around with the "jump method" because i want my character to be able to speed up and slow down with friction and gravity but that's just me. hope it helped a little bit!

Edited by tbpb2010, 13 July 2007 - 03:23 PM.

  • 0

#17 dovonob

dovonob

    GMC Member

  • New Member
  • 172 posts

Posted 13 July 2007 - 04:04 PM

i've tried all of those annoying and confusing methods in the past and i still ended up just using 4 different direction sprites (i could never figure out the actual solution) but if you think this may help check it out:

if you have your character using the "speed method" (if keyboard_check(vk_left) hspeed=-5 "<this is not relative" or hspeed-=5 "<this is relative") then to set the directional sprite depending on the speeds you have to actually use the direction. for example "if direction = 45 {sprite_index = chacter_facing_up_right}" or something like that but to use this method correctly you must have the speeds either set in fixed directions (North,Northeast,East,Southeast.etc) without the charcter's direction changing to 46 or 32 (not in the 8 compass directions) or to set a sprite for all 360 directions which is very time consuming and probably doesn't even work (i said i could never find the solution) but those might work for the "speed method." oh yeah and if you're worried about the speed being too fast for the diagonal directions like the speed is 5 going up and the speed is 5 going left but the speed is 7 going up-left, just limit the speed by putting this in the step event (if speed > 5 {speed = 5}) this is checking if the overall speed of the object is greater than 5 and if it is it'll make the speed go back to 5 thus limiting the speeds in every direction to 5!

the "jump method" if keyboard_check(vk_right) {x+= 3} and functions like that might require a check in the step event like "if keyboard_check(anykey) x=x+=3 and y=y-=3 {sprite_index = character_facing_upright}" and before all that you've already set the 4 directions to jumping directions like  the left key means x-=3. i haven't really messed around with the "jump method" because i want my character to be able to speed up and slow down with friction and gravity but that's just me. hope it helped a little bit!

<{POST_SNAPBACK}>



my game uses 8 directional sprites flawlessly if you really want to know how to do it I could copy and past some stuff, be warned though, a few scripts and alot of explaining.

the two main things to do are

1. set a variable to tell what direction your going, something like

moving_dir=round(direction/8)

can't quite remember the equation off the top of my head but basically you need to divide 360 degrees into eighths so each direction is a number in the variable

moving_dir = 1 // move right
moving_dir = 2 // move up/right

etc etc

then use a switch statement to set the correct sprite

switch (moving_dir)

case1: sprite_index = spr_right
case2: sprite_index = spr_up_right

etc

I'm sure that's not the exact syntax but you get the idea i hope.
  • 0

#18 CoderBlue

CoderBlue

    GMC Member

  • New Member
  • 668 posts

Posted 14 July 2007 - 05:32 AM

im tellin you dude, bind exact directional movements to your numpad keys and do it that way, simplest way to do it

forget all of this IF VKEY DOWN code, it's way too much work for what you want
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users