A
Alaska Minds
Guest
GM Version: Studio 2 (Possibly later versions of Studio 1)
Target Platform: Any
Download: None (Code below)
Links: round(), point_direction(), switch, sprite_index, image_speed
Summary:
Hello all I'm going to show you how to change your Object's sprite, based on direction.
It all starts with chopping 360 degrees into eight 45 degree octants, each switch case represents a direction based on that octant.
Think of an octant as the space of a pie piece, where the pie is split in 8 pieces. Each piece of pie represents the direction, the Object being the center of the pie. You can also think of it like a compass (image below). Each octant is the space between N & NE, NE & E, E & SE,....
Below the code checks the current position (x and y) against the previous position (xprevious and yprevious) and calculates direction. Using round(), we are rounding up or down (i.e. 5.8=6 & 5.2=5) the calculated direction using the point_direction function. We use that value to determine the current octant by using the switch function to set the Object's sprite in 8 directions by dividing the value by 45 creating 8 cases. (direction value is a cycle of 360 degrees)
Case 0 and 8 both mean the same direction (0 degrees and 360 degrees).
I just used both 0 and 8 for direction right/east
Also in the code we start and stop the sprite animation, using image_speed.
Enter the code below in your Objects End Step Event.
Target Platform: Any
Download: None (Code below)
Links: round(), point_direction(), switch, sprite_index, image_speed
Summary:
Hello all I'm going to show you how to change your Object's sprite, based on direction.
It all starts with chopping 360 degrees into eight 45 degree octants, each switch case represents a direction based on that octant.
Think of an octant as the space of a pie piece, where the pie is split in 8 pieces. Each piece of pie represents the direction, the Object being the center of the pie. You can also think of it like a compass (image below). Each octant is the space between N & NE, NE & E, E & SE,....
Below the code checks the current position (x and y) against the previous position (xprevious and yprevious) and calculates direction. Using round(), we are rounding up or down (i.e. 5.8=6 & 5.2=5) the calculated direction using the point_direction function. We use that value to determine the current octant by using the switch function to set the Object's sprite in 8 directions by dividing the value by 45 creating 8 cases. (direction value is a cycle of 360 degrees)
Case 0 and 8 both mean the same direction (0 degrees and 360 degrees).
I just used both 0 and 8 for direction right/east
Also in the code we start and stop the sprite animation, using image_speed.
Enter the code below in your Objects End Step Event.
Code:
// Calculate direction based on previous x and y value and current x and y value; Switch 360 degrees into 8 45 degree octants (cases) by dividing point_direction value by 45
if x != xprevious || y != yprevious {
// If Character is moving, start sprite animation
image_speed = 1;
switch(round(point_direction(xprevious,yprevious,x,y)/45)){
case 0:
sprite_index = spr_right;
break;
case 1:
sprite_index = spr_up_right;
break;
case 2:
sprite_index = spr_up;
break;
case 3:
sprite_index = spr_up_left;
break;
case 4:
sprite_index = spr_left;;
break;
case 5:
sprite_index = spr_down_left;
break;
case 6:
sprite_index = spr_down;
break;
case 7:
sprite_index = spr_down_right;
break;
case 8:
sprite_index = spr_right;
break;
}
// If Character is not moving, stop sprite animation
}else {
image_speed = 0;
}
Last edited by a moderator: