Legacy GM [SOLVED] Changing Sprite Index Based on Mouse Direction

D

dawson2223

Guest
Trying to figure out how to change the sprite index based on the mouse being between the degrees of
0-90
91-179
180-269
270-359

Any Ideas on how? So far all I can find is how to rotate the sprite, not change between sprites.
 

obscene

Member
If you take the angle and divide it by 90, and then floor it, the results will be either 0, 1, 2 or 3.

floor (value / 90)

Easy to take those numbers and pick a sprite.
 
D

dawson2223

Guest
If you take the angle and divide it by 90, and then floor it, the results will be either 0, 1, 2 or 3.

floor (value / 90)

Easy to take those numbers and pick a sprite.

How do I register what degree the mouse is currently on?
 
D

dawson2223

Guest
point_direction()
Like this?


if floor(point_direction(x,y,mouse_x,mouse_y/90))=0 {
sprite_index = 0
}

else if floor(point_direction(x,y,mouse_x,mouse_y/90))=1 {
sprite_index = 1
}

else if floor(point_direction(x,y,mouse_x,mouse_y/90))=2 {
sprite_index = 2
}

else floor(point_direction(x,y,mouse_x,mouse_y/90))= 3 {
sprite_index = 3
}
 

obscene

Member
That may work but it's terrible inefficient. You are doing the same calculations over and over again. All those point_direction() and floor calls will slow your game down quick.

You can do it once like this...

Code:
var dir=floor(point_direction(x,y,mouse_x,mouse_y/90))
And then normally you could use dir in your if and else statements.

BUT. The whole point of this getting values 0 1 2 or 3 from this was so you could do this....

Code:
image_index=floor(point_direction(x,y,mouse_x,mouse_y/90))
That one line does the same thing all your code was doing :p
 
D

dawson2223

Guest
That may work but it's terrible inefficient. You are doing the same calculations over and over again. All those point_direction() and floor calls will slow your game down quick.

You can do it once like this...

Code:
var dir=floor(point_direction(x,y,mouse_x,mouse_y/90))
And then normally you could use dir in your if and else statements.

BUT. The whole point of this getting values 0 1 2 or 3 from this was so you could do this....

Code:
image_index=floor(point_direction(x,y,mouse_x,mouse_y/90))
That one line does the same thing all your code was doing :p
Oh wow haha! I put in that code but now it changes the sprite index every degree?
 

obscene

Member
Oh... I just copied your code over and didn't notice your mistake.

You have to divide the direction 90 by not, not just the y part....

Code:
image_index=floor(point_direction(x,y,mouse_x,mouse_y)/90)
That SHOULD be right.
 
D

dawson2223

Guest
Oh... I just copied your code over and didn't notice your mistake.

You have to divide the direction 90 by not, not just the y part....

Code:
image_index=floor(point_direction(x,y,mouse_x,mouse_y)/90)
That SHOULD be right.
Oops! Yeah that was right!!! :D
 
Top