Windows Top Down Shooter Controls.

N

nickvm98

Guest
I am trying to make a 360 degree top down movement but there is a problem with it. When I test it, the player by default moves right and does that when I release the arrow keys and pressing down + right makes it stuck. I dunno what the problem is.
Create:
image_speed = 0;

Step:
///Movement
var _ax,_ay;
x = floor(x);
y = floor(y);
_ax = keyboard_check(vk_right)- keyboard_check(vk_left);
_ay = keyboard_check(vk_down) - keyboard_check(vk_up);
move_towards_point(x+_ax,y+_ay,1);

Draw:
draw_sprite_ext(spr_plegs,image_index,x,y,1,1,direction,c_white,1);
draw_sprite_ext(spr_pbody,0,x,y,1,1,point_direction(x,y,mouse_x,mouse_y),c_white,1);
draw_sprite_ext(spr_phead,0,x,y,1,1,point_direction(x,y,mouse_x,mouse_y),c_white,1);
 

Simon Gust

Member
Hi,
I think you Need to recode the _ax and _ay variables to:
Code:
lft = - keyboard_check(vk_left);
rgt = keyboard_check(vk_right)
top = -keyboard_check(vk_up);
bot = keyboard_check(vk_down);
_ax = lft + rgt;
_ay = top + bot;
 

DukeSoft

Member
I usualy just and only use this
Code:
x += (keyboard_check(ord('D')) - keyboard_check(ord('A'))) * playerspeed;
y += (keyboard_check(ord('S')) - keyboard_check(ord('W'))) * playerspeed;
 
N

nickvm98

Guest
I tried this code but it didn't work:
lft = - keyboard_check(vk_left);
rgt = keyboard_check(vk_right)
top = -keyboard_check(vk_up);
bot = keyboard_check(vk_down);
_ax = lft + rgt;
_ay = top + bot;

This code is what I usually use too but it's complicated to map the sprites to each direction. I want a control scheme where the sprites rotate to the direction the character is moving towards using the arrow keys:
x += (keyboard_check(ord('D')) - keyboard_check(ord('A'))) * playerspeed;
y += (keyboard_check(ord('S')) - keyboard_check(ord('W'))) * playerspeed;
 

DukeSoft

Member
Code:
x += (keyboard_check(ord('D')) - keyboard_check(ord('A'))) * playerspeed;
y += (keyboard_check(ord('S')) - keyboard_check(ord('W'))) * playerspeed;
player_direction = point_direction(xprevious, yprevious, x, y);
player_moving = (x != xprevious || y != yprevious);
Now you have a 360 degree variable that will point in the direction the player is moving, and you'll have a variable that will tell if the player was moving or not.

Code:
if (player_direction < 45 && player_direction > 315)  {
    //Image is towards the right
}
if (player_direction > 225 && player_direction < 315)  {
    //Image is towards the bottom
}
if (player_direction > 135 && player_direction < 225)  { 
    //Image is towards the left
}
if (player_direction < 135 && player_direction > 45)  { 
    //Image is towards the top
}
 

samspade

Member
Code:
x += (keyboard_check(ord('D')) - keyboard_check(ord('A'))) * playerspeed;
y += (keyboard_check(ord('S')) - keyboard_check(ord('W'))) * playerspeed;
player_direction = point_direction(xprevious, yprevious, x, y);
player_moving = (x != xprevious || y != yprevious);
Now you have a 360 degree variable that will point in the direction the player is moving, and you'll have a variable that will tell if the player was moving or not.

Code:
if (player_direction < 45 && player_direction > 315)  {
    //Image is towards the right
}
if (player_direction > 225 && player_direction < 315)  {
    //Image is towards the bottom
}
if (player_direction > 135 && player_direction < 225)  {
    //Image is towards the left
}
if (player_direction < 135 && player_direction > 45)  {
    //Image is towards the top
}
Couldn't you just do:

Code:
//Step:
x += (keyboard_check(ord('D')) - keyboard_check(ord('A'))) * playerspeed;
y += (keyboard_check(ord('S')) - keyboard_check(ord('W'))) * playerspeed;
player_direction = point_direction(xprevious, yprevious, x, y);
player_moving = (x != xprevious || y != yprevious);

//Draw:
draw_sprite_ext(spr_plegs, image_index, x, y, 1, 1, player_direction, c_white, 1);
draw_sprite_ext(spr_pbody, 0, x, y, 1, 1, point_direction(x,y,mouse_x,mouse_y), c_white, 1);
draw_sprite_ext(spr_phead, 0, x, y, 1, 1, point_direction(x,y,mouse_x,mouse_y), c_white, 1);
 

DukeSoft

Member
Yes, if he had a 100% top-down image and not different angles of the player image.

Also, you'd want to prevent using point_direction() too much, you're calling it twice for the head and body. Also, this solution would mean the player has a spine that can rotate 360 degrees.
 
N

nickvm98

Guest
The player doesn't need a spine, it's like a cyborg. The point_direction() really fixed the problem. Thanks for the help.
 
Top