Sprite Image Depends On Mouse Direction

Z

ZNewts

Guest
Hello everyone, I apologize for the basic question but I'm a little stuck. I am making a top down thingy and I have 9 images for the player sprite based off of the position of the mouse in relation to the player object.

The only help I've been able to find in my search has been an old reddit thread that's been archived for a while. It hasn't helped and it's the closest I've come so far to finding an answer. That specific thread, the OP was asking about 8 directions only, where as I have the player constrained to 9.

I can't get the sprite to stop animating through all 9 frames, I was hoping to use the step event to make them change following the mouse. Should I have a separate sprite per variation in direction?


switch(round(point_direction(x,y,mouse_x,mouse_y)/40))
I have no idea what I'm doing oh my god
 

NicoDT

Member
Hello everyone, I apologize for the basic question but I'm a little stuck. I am making a top down thingy and I have 9 images for the player sprite based off of the position of the mouse in relation to the player object.

The only help I've been able to find in my search has been an old reddit thread that's been archived for a while. It hasn't helped and it's the closest I've come so far to finding an answer. That specific thread, the OP was asking about 8 directions only, where as I have the player constrained to 9.

I can't get the sprite to stop animating through all 9 frames, I was hoping to use the step event to make them change following the mouse. Should I have a separate sprite per variation in direction?


switch(round(point_direction(x,y,mouse_x,mouse_y)/40))
I have no idea what I'm doing oh my god
Hi, welcome to the forum.
The sprite going through all the animations is because unless you change, the image_speed will be 1, so:
Code:
///Create event
image_speed = 0
Then, you can specify the specific frame with image_index.
But the way you´re doing it is wrong, because "round(point_direction(x,y,mouse_x,mouse_y)/40" can return 10 different values:
round(0/40) will return 0, and round(359/40) will return 9.

A solution to what you want to do is the following.

Code:
var frame = 0
var direc = point_direction(x,y,mouse_x,mouse_y)
if (direc>=340)  frame=0
 else  frame= round(direc/40)
image_index = frame //make sure that the sprites subimages are aligned to the angle
 
G

G Saber

Guest
The video tutorial series that all can see on Yoyogames page covers that stuff and more. Go check it
 
Z

ZNewts

Guest
Hi, welcome to the forum.
The sprite going through all the animations is because unless you change, the image_speed will be 1, so:
Code:
///Create event
image_speed = 0
Then, you can specify the specific frame with image_index.
But the way you´re doing it is wrong, because "round(point_direction(x,y,mouse_x,mouse_y)/40" can return 10 different values:
round(0/40) will return 0, and round(359/40) will return 9.

A solution to what you want to do is the following.

Code:
var frame = 0
var direc = point_direction(x,y,mouse_x,mouse_y)
if (direc>=340)  frame=0
 else  frame= round(direc/40)
image_index = frame //make sure that the sprites subimages are aligned to the angle
Thank you for the generous welcome and thank you for taking the time and helping me out. It's definitely a step in the right direction for me, however it's very finicky at the moment. I'm sorry for being so dumb, I must be missing something or I'm just not getting the logic of it. As of now, at least I got the sprite to stop animating on a loop, the create event was simple enough so thank you for that. However I'm not sure how to make sure the subimages are aligned to the angle. I've only been able to tweak it by entering the sprite editor and dragging the frames around, but it's not exact. Is there a way I can be super specific with it or how can I understand what you're getting through to the computer with the line of code you shared with me?


The video tutorial series that all can see on Yoyogames page covers that stuff and more. Go check it
If you're referring to the tutorial videos made by Shaun Spalding they don't cover my exact issue. I've already recreated his simple top down shooter tutorial. His videos just cover changing the character sprite's rotation according to the mouse but not swapping out specific images. Do you have a link to the specific video you have in mind?
 
G

G Saber

Guest
In other words, you're trying to play a specific animation loop with certain sub-images dependent of where mouse is located? AND you're also trying to pivot image(s) to correctly face mouse location?
 
Z

ZNewts

Guest
In other words, you're trying to play a specific animation loop with certain sub-images dependent of where mouse is located? AND you're also trying to pivot image(s) to correctly face mouse location?
I'm sorry I must have not been clear, I do not want the sprite to rotate facing the mouse. However, I do have 9 sub-images for each direction contained in one sprite.

I have one facing the center for when the mouse is hovering pretty much over the player- probably not to be seen much when in actual play but I intend on that being the idle state when interacting with and NPC/stage item.
so, 9 positions

center
top center
bottom center

top left
center left
bottom left

top right
center right
bottom right.

I intend on the sprite changing to whichever image based on the position of the mouse in relation to the player object.

Sorry I feel like I'm talking in circles but I'm trying to be super specific.

If it helps, it isn't a top down shooty game, just a top down adventure puzzle thingy. In a shooter 9 positions would be a garbage mechanic

So far NicoDT got me halfway there, I got the sprite to stop animating with a create event and instead change position based on my mouse location, but I explain in my response to them that I'm still having problems with it.

I'm basically there but I can't figure out how to assign frame specific locations or know what orientation the game reads sub images in order of the operation Nico provided.
 
Last edited by a moderator:
G

G Saber

Guest
No, it's fine you're not! I like in-depth details, and I'm sure alot of people on here do too.
What Game Maker version are you using, and what method of the 2, GML or DnD? Please place those 2 in tags at the very least. :)

Okay I'm not the experienced one here for that answer although I'd love to assist to some degree...

I could be wrong, sounds like you would need to create a grid so that you can set parameters for specific actions depending on location of mouse on grid which you would then input into an argument. https://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/13_accessors.html
 
Z

ZNewts

Guest
Ah much obliged, I'll remember to keep my tags handy for next time. I'm using GameMaker Studio 2: v2.0.6.136, with the most current being 2.0.6.146. I'm not sure what the difference is but I'm sure it's negligible, and I'm trying to learn GLM. Some of it I get, most of it I don't understand quite yet.

Would this grid be particular to the viewport or is it set by the room? I don't want the player to be fastened in one location, they can still move about freely using WASD controls which is already implemented. I just need to get the mouse bit part down right and I'm set to start working on the rest of the game.
 

NicoDT

Member
I know understand what you need.
Arrange the subimages like this (anti clockwise)
center - 8
right - 0
top right - 1
top - 2
top left -3
left - 4
bottom left - 5
bottom - 6
bottom right - 7

Code:
var frame = 0
var direc = point_direction(x,y,mouse_x,mouse_y)
if distance_to_point(x,y,mouse_x,mouse_y) < 50  {frame  = 8 } //change the 50 to the distance at which you want to see the center spriten subimage
else {
if (direc>=337.5)  frame=0
 else  frame= round(direc/45)
}
image_index = frame
Doing this will give you the correct frame. In case you didn't know, GM starts angle 0 to the right, 90 is up, and so on (anti clockwise).
So if you have an angle of 80 (you'd want the TOP subimage), 80/45 = 1.77 , when rounding, you get 2 (the top image if you order it as I mentioned before)
 
Z

ZNewts

Guest
I know understand what you need.
Arrange the subimages like this (anti clockwise)
center - 8
right - 0
top right - 1
top - 2
top left -3
left - 4
bottom left - 5
bottom - 6
bottom right - 7

Code:
var frame = 0
var direc = point_direction(x,y,mouse_x,mouse_y)
if distance_to_point(x,y,mouse_x,mouse_y) < 50  {frame  = 8 } //change the 50 to the distance at which you want to see the center spriten subimage
else {
if (direc>=337.5)  frame=0
 else  frame= round(direc/45)
}
image_index = frame
Doing this will give you the correct frame. In case you didn't know, GM starts angle 0 to the right, 90 is up, and so on (anti clockwise).
So if you have an angle of 80 (you'd want the TOP subimage), 80/45 = 1.77 , when rounding, you get 2 (the top image if you order it as I mentioned before)

Hot diggity damn you are the best person alive! Thank you thank you! Switching the sprites to that order works perfectly.

I actually feel like I'm getting this too.
One little snag, while I am understanding the logic behind the line: if distance_to_point(x,y,mouse_x,mouse_y) < 50 {frame = 8 }
I don't get is why I have a red exclamation point to the left saying "unexpected unary operator <" ?
That has me lost but everything else seems to work.
 

NicoDT

Member
Hot diggity damn you are the best person alive! Thank you thank you! Switching the sprites to that order works perfectly.

I actually feel like I'm getting this too.
One little snag, while I am understanding the logic behind the line: if distance_to_point(x,y,mouse_x,mouse_y) < 50 {frame = 8 }
I don't get is why I have a red exclamation point to the left saying "unexpected unary operator <" ?
That has me lost but everything else seems to work.
I'm glad it's working.
I never had that problem (I'm using GM 1.4) , but I think there's a solution here
https://forum.yoyogames.com/index.php?threads/very-annoying-syntax-error-box.11855/
 
Z

ZNewts

Guest
I'm glad it's working.
I never had that problem (I'm using GM 1.4) , but I think there's a solution here
https://forum.yoyogames.com/index.php?threads/very-annoying-syntax-error-box.11855/
Just an update, I got it to work. Turns out it was the distinction working between versions. The line in question:

if distance_to_point(x,y,mouse_x,mouse_y) < 50 {frame = 8 }

Only needed two arguments instead of 4. I changed it to this first

if distance_to_point(x,y) < 50 {frame = 8 }

Which got rid of the compiling error, however the player was stuck to the center frame. I went back and changed it to this.

if distance_to_point(mouse_x,mouse_y) < 50 {frame = 8 }

Which ended up working flawlessly, and 50 is the perfect range if I might add. Thank you again for the support and sending me in the right direction. Good luck to all of you!
 
Top