Edited by kokoslover, 13 July 2007 - 02:07 AM.
8 Way Movement
#1
Posted 13 July 2007 - 01:13 AM
#2
Posted 13 July 2007 - 01:35 AM
#3
Posted 13 July 2007 - 01:41 AM
But I want to be able to go all 8 directions.
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.
#4
Posted 13 July 2007 - 01:46 AM
#5
Posted 13 July 2007 - 01:50 AM
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
#6
Posted 13 July 2007 - 02:00 AM
vk_up can be replaced with vk_left, vk_right, and vk_down for the different keys
#7
Posted 13 July 2007 - 02:07 AM
#8
Posted 13 July 2007 - 02:07 AM
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
#9
Posted 13 July 2007 - 02:11 AM
//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
}
#10
Posted 13 July 2007 - 02:13 AM
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.
#11
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.
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.
#12
Posted 13 July 2007 - 02:21 AM
#13
Posted 13 July 2007 - 02:33 AM
#14
Posted 13 July 2007 - 01:34 PM
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.At least you are helping someone
#15
Posted 13 July 2007 - 01:49 PM
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
Edited by dovonob, 13 July 2007 - 01:53 PM.
#16
Posted 13 July 2007 - 03:12 PM
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.
#17
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!
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.
#18
Posted 14 July 2007 - 05:32 AM
forget all of this IF VKEY DOWN code, it's way too much work for what you want
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











