Legacy GM drawing sprite at center of an object/sprite

I'm working on an RPG similar to old school Dragon Quest games and I want to draw an effect animation whenever the player uses a skill or simply attacks on top of the enemy sprite. I want it to be drawn in the center of where the enemy sprite is drawn. I haven't centered any of my sprites (the origin is still 0,0).

I would think that something like this would work, but it doesn't:

Code:
spr_width = sprite_get_width( enemy_spr);
spr_height = sprite_get_height( enemy_spr);
draw_sprite( eff_spr, subimg, enemy_xx + ( spr_width / 2 ), enemy_yy + ( spr_height / 2 ) );
 

Azenris

Member
Without testing myself, maybe:
Code:
var spr_width = sprite_get_width( enemy_spr );
var spr_height = sprite_get_height( enemy_spr );
var eff_spr_width = sprite_get_width( eff_spr );
var eff_spr_height = sprite_get_height( eff_spr );

draw_sprite( eff_spr, subimg, enemy_xx + ( ( spr_width - eff_spr_width ) / 2 ), enemy_yy + ( ( spr_height - eff_spr_height ) / 2 )  );
 
Without testing myself, maybe:
Code:
var spr_width = sprite_get_width( enemy_spr );
var spr_height = sprite_get_height( enemy_spr );
var eff_spr_width = sprite_get_width( eff_spr );
var eff_spr_height = sprite_get_height( eff_spr );

draw_sprite( eff_spr, subimg, enemy_xx + ( ( spr_width - eff_spr_width ) / 2 ), enemy_yy + ( ( spr_height - eff_spr_height ) / 2 )  );
Your code worked, thanks. I was thinking that to get the center I would need to just add half of the enemy sprites width and height to the x and y coordinate. So basically I needed to take into account the effect sprite's width and height as well.
 

TheouAegis

Member
Your code worked, thanks. I was thinking that to get the center I would need to just add half of the enemy sprites width and height to the x and y coordinate. So basically I needed to take into account the effect sprite's width and height as well.
Or if you centered everyone's sprites, just use x and y.

Technically, what matters more than anything is the origins of both sprites. You jumped to the conclusion that the sprite's size matters instead of its origin because everything that was handled up until Thultex's post dealt with an origin of (0,0). With what Thultex was saying, if you centered the effect's sprite, you effectively remove its dimensions from the formula Azenris gave you. Then likewise, if you centered the monster's sprite, you remove its dimensions from the formula as well. The correct formula takes all the origins and dimensions into consideration.

x + (sprite_width/2 - sprite_xoffset) - (fx.sprite_width/2 - fx.sprite_xoffset), y + (sprite_height/2 - sprite_yoffset) - (fx.sprite_height/2 - fx.sprite_yoffset)
 
Alternatively effects can just use a centered offset.
Or if you centered everyone's sprites, just use x and y.

Technically, what matters more than anything is the origins of both sprites. You jumped to the conclusion that the sprite's size matters instead of its origin because everything that was handled up until Thultex's post dealt with an origin of (0,0). With what Thultex was saying, if you centered the effect's sprite, you effectively remove its dimensions from the formula Azenris gave you. Then likewise, if you centered the monster's sprite, you remove its dimensions from the formula as well. The correct formula takes all the origins and dimensions into consideration.

x + (sprite_width/2 - sprite_xoffset) - (fx.sprite_width/2 - fx.sprite_xoffset), y + (sprite_height/2 - sprite_yoffset) - (fx.sprite_height/2 - fx.sprite_yoffset)
Oh! Okay, thanks for explaining that to me because I would've left thinking that was the correct way of doing this. I usually center all my sprites, but given what kind of RPG the game is and the fact that the objects don't move in battle, I was under the impression that I wouldn't need to center them.
 
Last edited:
Top