• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Image angle change sprite help

J

Just a guy

Guest
So, I would like my player to always look the direction the mouse is, but not where the whole image rotates, but the sprites change (like enter the gungeon) does anyone know a way to change the sprite index based on mouse location?
 
You can get the direction towards the mouse using point_direction:
Code:
var_direction = point_direction(x, y, mouse_x, mouse_y);
Then, check that variable to see if it is within a certain range, and assign the sprite index based on that. For example:
Code:
if (var_direction >= 45 && var_direction < 125)
{
  sprite_index = spr_player_face_up;
}
CMAllen's solution is better IMO. \/ \/ \/
 

CMAllen

Member
Code:
var mouse_dir = point_direction(x,y,mouse_x,mouse_y)
Then, depending on how many directions your character can face:
Code:
//4 directions
facing_direction = round(mouse_dir/90);
Code:
//8 directions
facing_direction = round(mouse_dir/45);
The value of facing direction can now be used in a switch() statement or what have you, with its value ranging from 0-4 or 0-8 (depending on which one you used). 0 and the max value for facing direction (be it 4 or 8) can be your switch statements default case.
Code:
//4 directions
switch(facing_direction)
{
    case 1: sprite_direciton = sprite_up; break;
    case 2: sprite_direction = sprite_left; break;
    case 3: sprite_direction = sprite_down; break;
    default: sprite_direction = sprite_right; break;
}
 
J

Just a guy

Guest
Code:
var mouse_dir = point_direction(x,y,mouse_x,mouse_y)
Then, depending on how many directions your character can face:
Code:
//4 directions
facing_direction = round(mouse_dir/90);
Code:
//8 directions
facing_direction = round(mouse_dir/45);
The value of facing direction can now be used in a switch() statement or what have you, with its value ranging from 0-4 or 0-8 (depending on which one you used). 0 and the max value for facing direction (be it 4 or 8) can be your switch statements default case.
Code:
//4 directions
switch(facing_direction)
{
    case 1: sprite_direciton = sprite_up; break;
    case 2: sprite_direction = sprite_left; break;
    case 3: sprite_direction = sprite_down; break;
    default: sprite_direction = sprite_right; break;
}
Thx this is super helpful!
 
Top