SOLVED Direction-Sprite Change

E

EnSea

Guest
Basically, I just need some basic coding help.
I can't figure out how to change the sprite based on what direction the enemy is facing.
Side Notes: This is an RPG Dungeon-like game, the coding i need is basic and no overly complicated as I'm fairly new at this.

This is my enemy code:
GML:
if distance_to_object(player)<=Screen{   
      if (collision_line(x,y,player.x,player.y,block,true,true) = noone) {
           direction=point_direction(x,y,player.x,player.y);
           speed = 1;
    }
}
 
B

Beb the Coder

Guest
How do you want to change the Sprite? Do you want it to look in 4 directions, or in 8 or anything else?
 

Joe Ellis

Member
If you want to have separate sprites for each direction like Beb the Coder was saying, this should work:
GML:
sprite_index = spr_player_0_degrees + floor((direction / 360) * 8)
It just divides the angle by 8 and rounds it down, then if you have the sprites in anti clockwise order in the resource tree it simply adds on to the base 0 degrees one
 

Yal

🐧 *penguin noises*
GMC Elder
If you want to have separate sprites for each direction like Beb the Coder was saying, this should work:
GML:
sprite_index = spr_player_0_degrees + floor((direction / 360) * 8)
It just divides the angle by 8 and rounds it down, then if you have the sprites in anti clockwise order in the resource tree it simply adds on to the base 0 degrees one
This will stop working in 2.3, so I'd not recommend using it. Group the spritesets together using a ds_map or something instead (this also allows you to reuse the same sprite if they're the same for some directions by entering the same ID for multiple directions)
 

Joe Ellis

Member
This will stop working in 2.3, so I'd not recommend using it. Group the spritesets together using a ds_map or something instead (this also allows you to reuse the same sprite if they're the same for some directions by entering the same ID for multiple directions)
Oh thanks I didn't know that would happen. Why is it? Do resources not work with indices anymore?
I think a simple array list of the sprites instead would be the best solution: (and is better really anyway cus of some sprite sets not being able to be ordered in a row, or awkward when you've got loads of them)
GML:
sprite_index = current_sprite_set[floor((direction / 360) * 8)]
 

NightFrost

Member
Oh thanks I didn't know that would happen. Why is it? Do resources not work with indices anymore?
Well, technically resource index number order reliance has always been a "hack" as it is not documented behavior. Those always come with the caveat of "may stop working in future time." That time is now.

I guess it comes from the reworking of the resource tree view in 2.3. I've not signed for the beta so I don't know to what extent it goes, but I read the ordering is now unreliable at the very least.
 

Yal

🐧 *penguin noises*
GMC Elder
Oh thanks I didn't know that would happen. Why is it? Do resources not work with indices anymore?
Resources have indices, but they're not ordered by the listing order in the resource tree anymore (essentially making them random). (This is because of the new 2.3 changes that finally removes the resource type divisions in the resource tree)
 
Top