• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] Change Sprite Origin With Code?

TheBroman90

Member
My enemies consist of multiple body parts. The parts have their sprite origin set so they follow the body and animates correctly.

But when I kill the enemy I want the body parts to fly all over the place. Then I want the body parts sprite origin to be centered instead so they rotate from the middle. I found this thing called sprite_set_offset that can change the origin of a sprite. But it says: This function affects the sprite resource so that all further instances with this sprite will have the same offset.

I only want to affect the sprite used by the object, not the sprite resource. Is this possible?
 

Tthecreator

Your Creator!
You will have to manually draw the sprite.
Practically add a draw event, put draw_sprite() inside of it, and add a translation to the x and y arguments.

I hope that can put you in the right direction, if you have trouble with the code or D&D just say it.
 

Jobo

Member
GMC Elder
As you said,
Code:
sprite_set_offset(ind, xoff, yoff);
but it's on the sprite. Not the draw state. As Tthecreator says, you must draw it with the offset yourself in that case. Off the top of my head, look into lengthdir_x/y.
 

TheBroman90

Member
Thanks. I'm familiar with lengthdir, but I'm not sure how to use it in this case to offset the rotation. Any suggestions?
 

Tthecreator

Your Creator!
Well if you want to make the sprites go in a circle, you can make a counter that acts as the angle and then use lengthdir_x and lenghtdir_y to convert that angle into a x, y pixel offset.
I hope that didn't sound too vague.
If you just want something random, you could just also have it move completely randomly if you want.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
If you only need to draw a sprite with a different origin, here's a script:
Code:
/// draw_sprite_origin_ext(sprite, subimg, ox, oy, x, y, xs, ys, rot, col, alpha)
//                              0       1   2   3  4  5   6   7    8    9     10
var q = argument0;
var ox = argument2 - sprite_get_xoffset(q);
var oy = argument3 - sprite_get_yoffset(q);
var sx = argument6;
var sy = argument7;
var d = argument8;
draw_sprite_ext(q, argument1,
    argument4 - sx * lengthdir_x(ox, d) - sy * lengthdir_x(oy, d - 90),
    argument5 - sx * lengthdir_y(ox, d) - sy * lengthdir_y(oy, d - 90),
    sx, sy, d, argument9, argument10);
web demo
 
C

Christopher Rosa

Guest
you can make your own _origin variables.
GML:
draw_sprite_ext(spr_enemy,image_index,x+x_origin,y+y_origin,image_xscale,image_yscale,0,image_blend,1);

x_origin = 10;
 
Top