• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ help w sprite_set_offset

GML:
#event create
additionaloffsetx = 0;
additionaloffsety = 0;

#event step
if mouse_check_button_pressed(mb_left) and position_meeting(mouse_x, mouse_y, id)
{
    tracking = 1;
    if mouse_x != x and mouse_y != y
    {
        var offsetxx = -(x-mouse_x);
        var offsetyy = -(y-mouse_y);
        sprite_set_offset(sprite_index, offsetxx+additionaloffsetx, offsetyy+additionaloffsety);
        additionaloffsetx = offsetxx;
        additionaloffsety = offsetyy;
    }
}

if mouse_check_button_released(mb_left) and tracking = 1
{
    tracking = 0;
}

if tracking = 1
{
    x = mouse_x;
    y = mouse_y;
}
i do not think i properly understand how sprite_set_offset works

im trying 2 make a draggable object
u click, it sets the origin of the sprite where u clicked within the image itself, then follows ur cursor, when u release, it ends where it is
u click again, it accounts 4 the previous offset, sets the new offset, follows ur cursor, releasing ends it
u click again, it is not setting the new offset correctly
i would like 2 know y this isnt working, &/or how precisely sprite_set_offset works, bc it appears 2 not compound the offset naturally, it is setting the offset based on the sprites original sprite origin, not off the new offset, but im failing 2 account 4 this w my above code but i genuinely cant figure out y, thx 4 help
EDIT: accidentally wrote additionaloffsety - 0; instead of additionaloffsety = 0;, bc the code im asking 4 help w/ uses the latter
 

Nidoking

Member
I think you're failing to take the existing offset into account when you store the previous offsets. The additionaloffsetx and y should be the same as what you passed into the sprite_set_offset function. In the first run, that's true because the additional offsets are both zero, so it appears to work the second time. However, the variables are incorrectly set the second time through, so it looks wrong the third time.

I don't understand why you would need to track them at all, though, since you can use sprite_get_xoffset and sprite_get_yoffset to get these values directly from the sprite. Then just add the new offset and set to that.
 
I think you're failing to take the existing offset into account when you store the previous offsets. The additionaloffsetx and y should be the same as what you passed into the sprite_set_offset function. In the first run, that's true because the additional offsets are both zero, so it appears to work the second time. However, the variables are incorrectly set the second time through, so it looks wrong the third time.

I don't understand why you would need to track them at all, though, since you can use sprite_get_xoffset and sprite_get_yoffset to get these values directly from the sprite. Then just add the new offset and set to that.
ok, thx, since u helped me ill help u
"I don't understand why you would need to track them at all, though"
bc i didnt know abt sprite_get_xoffset & sprite_get_yoffset, its rly that simple
 

TheouAegis

Member
Why are you trying to change the offset and not simply just move the object or whatever to the mouse's coordinates? You can keep track of the discrepancy between the mouse and the origin.

Or alternatively you could reset xstart and ystart in the object when you pick it up, then move the object to the mouse, then when the mouse is released set the object back to its start coordinates if the mouse has moved far enough away.
 
Top