• 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!

Legacy GM 4-Direction Sprite Face Mouse?

I

Inamortus

Guest
Hey all,

I've been working on a project, and have added the sprites I've made into the game, but I can't figure a way to change the sprite based on the mouse postion, ie. changing the sprite index to be spr_playerUp if the mouse's position is above the player, to spr_playerRight if it is to the right of the player. Anything I've found while searching for a solution is purely for rotating the character to follow the mouse, which obviously isn't what I want with a 4-Directional sprite.

Would anyone be able to help me out with this?

Thanks in advance.
 
A

Andrea Dominante

Guest
I guess it would be something like this
Code:
mx = mouse_x
my = mouse_y

//Right left
if  mx > oPlayer.x
sprite_index = spr_right
else if mx < oPlayer.x
sprite_index = spr_left

//Up down
if my > oPlayer.y
sprite_index = spr_up
else if my < oPlayer.y
sprite_index = spr_down
EDIT: removed the equals in >=, does not make any sense, sorry :p
Also, consider that this works best if the player sprite origin is centered, but I think that was obvious!
 
I

Inamortus

Guest
Thanks for the help; it half works. It will only change to Right/Left if the mouse is in a very specific postion, anyhwere else and it's either Up or Down. Any idea why this is?
 

Surgeon_

Symbian Curator
Try this:

Code:
//This code goes in the player object:

mx=mouse_x;
my=mouse_y;

var s;

switch ((point_direction(x,y,mx,my)+45) div 90) {

case 1: s=spr_player_up; break;
case 2: s=spr_player_right; break;
case 3: s=spr_player_down; break;

default: s=spr_player_left; break;

}

sprite_index=s;
Also, if you don't understand how this works, feel free to ask.

-Surgeon_
 
S

Snail Man

Guest
It's just a flaw in the code; try this:
Code:
var sprite_list;
sprite_list[0] = spr_playerRight;
sprite_list[1] = spr_playerUp;
sprite_list[2] = spr_playerLeft;
sprite_list[3] = spr_playerDown;
dir = point_direction(x,y,mouse_x,mouse_y);
sprite_index = round((dir)/90);
 
A

Andrea Dominante

Guest
Thanks for the help; it half works. It will only change to Right/Left if the mouse is in a very specific postion, anyhwere else and it's either Up or Down. Any idea why this is?
Well, because (stupid me :mad:) I forgot to specify every mouse_x and mouse_y combination. Basicly, it behaves like that because you either have a mouse_x > x and a mouse_y > y or viceversa, thing I did forget to add in the code. Try Surgeon_'s or Snail Man's solution; more efficent and less sloppy less mine. :D
 
I

Inamortus

Guest
Try this:

Code:
//This code goes in the player object:

mx=mouse_x;
my=mouse_y;

var s;

switch ((point_direction(x,y,mx,my)+45) div 90) {

case 1: s=spr_player_up; break;
case 2: s=spr_player_right; break;
case 3: s=spr_player_down; break;

default: s=spr_player_left; break;

}

sprite_index=s;
Also, if you don't understand how this works, feel free to ask.

-Surgeon_
That's perfect! Thank you so much. I think I understand the basics of it. It sets a variable to different values based on which area of the screen (split into 45 degree sections) the mouse is in?

EDIT: Also, quickly, would it later be possible to put several of these into if statements to change sprites based on a character selection variable?
 

Surgeon_

Symbian Curator
Yes, exactly. And if you acquire more directional sprites, you can replace "div 90" with "div 45", add some more cases and then you got turning in 8 directions. And so on... Anyway, I'm glad this worked out for you :)

-Surgeon_
 
Top