draw_sprite for animated sprites

RODO

Member
hello guys, I just need to take a doubt, something much more basic

how do i create or draw in the animated sprite case? I was looking in the documentation but I didn't find anything to help me, and I also didn't want to create an "obj" to just draw something basic
1608348761308.png
(example to better understand what I want to do)


I appreciate any help
 

kburkhart84

Firehammer Games
Just FYI, if there aren't a bunch of them, sometimes its not a bad thing to go ahead and add an extra object. It ends up opening some avenues of things that are harder to do otherwise. For example, its much easier to have a collision event with a separate object(especially using parent/child objects where appropriate).

That said, all you need to do is do the same thing that Gamemaker does internally to animate a sprite. Make an extra variable, start it as 0, increment it each step by whatever the frame/animation speed should be, and then draw that frame(using the floor function on the value) using draw_sprite(). Then, once the variable gets to the highest frame amount, take it back to 0 again.
 
Didn't try it, you might get an error trying to access a -1 or +1 over your sprite number, but on the top of my head, I'd try something like that"

GML:
var col;
if((col) && (alarm[0] == -1)){          //first time we collide
var _num = sprite_number(spr_E_interage);       //set alarm to number of sprites
alarm[0] = _num;

} 

if((col) && (alarm[0] != -1)){                             //if we already collided

var _num = sprite_number(spr_E_interage);     //get sprite number
var _spr_ind = (_num - alarm_get(alarm[0]));   //substract sprite number with remainder of alarm THIS IS YOUR SPRITE INDEX

//draw with _spr_ind as sprite index              

}
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Does the sprite you are trying to draw have the same animation settings as the sprite that has been assigned to the instance? If so, you can use image_index and it will animate exactly the same way.

Otherwise, you can make your own image_index.
 

RODO

Member
Just FYI, if there aren't a bunch of them, sometimes its not a bad thing to go ahead and add an extra object. It ends up opening some avenues of things that are harder to do otherwise. For example, its much easier to have a collision event with a separate object(especially using parent/child objects where appropriate).

That said, all you need to do is do the same thing that Gamemaker does internally to animate a sprite. Make an extra variable, start it as 0, increment it each step by whatever the frame/animation speed should be, and then draw that frame(using the floor function on the value) using draw_sprite(). Then, once the variable gets to the highest frame amount, take it back to 0 again.

interesting, I really didn’t want to use an OBJ because I think I’m doing it wrong or that it can cause problems to have too many OBJ, but thank you so much for the information
 

RODO

Member
Does the sprite you are trying to draw have the same animation settings as the sprite that has been assigned to the instance? If so, you can use image_index and it will animate exactly the same way.

Otherwise, you can make your own image_index.
hmmm I would even use image_index but my game at one point doesn’t allow me to use it because I use an inventory system that uses image_index, so that would hurt, but with everything, you gave me a great idea, thank you very much

Didn't try it, you might get an error trying to access a -1 or +1 over your sprite number, but on the top of my head, I'd try something like that"

GML:
var col;
if((col) && (alarm[0] == -1)){          //first time we collide
var _num = sprite_number(spr_E_interage);       //set alarm to number of sprites
alarm[0] = _num;

} else

if((col) && (alarm[0] != -1)){                             //if we already collided

var _num = sprite_number(spr_E_interage);     //get sprite number
var _spr_ind = (_num - alarm_get(alarm[0]));   //substract sprite number with remainder of alarm THIS IS YOUR SPRITE INDEX

//draw with _spr_ind as sprite index              

}

I will try to use this code, thanks for the help
 

RODO

Member
(UPDATE)
hello guys, so i just used the ideas you measured and even managed to get a result, however, you could take a doubt out of an error that i don't understand why it occurs:

1608375247773.png
the code above she basically understood that when I touch the object_item, I can take the item and use it in my inventory

1608375351993.png
then, to this code to check if I’m close to the item and that its “E” icon will appear to interact and pick up the item, a kind of indicative to the player

the problem is: it shows the icon on top of the normal obj_item, makes animation and destroys itself when I leave close, however, when more than one obj_item, it starts to bug and does not show the "E" icon or it freezes the animation (more bugs but they boil down to that).

I really don't know what is going on that I would like more help with, and sorry to come here again, I really would like to fix it by myself but this is difficult. Thanks for any help
 
You have to find the instance ID of your item. Otherwise, how will the machine know which one of the 1000's of the elixirs in your game you want destroyed?
Some options:

GML:
var _itm = instance_nearest(x, y, obj_elixir_innteration);        //THIS IS VERY IMPORTANT!

with(_itm){
    instance_destroy();
}
No need to supply argument that way, unless you feel very VERY fancy with the Destroy Event, but I"m not getting into that, even I never used that "skip destroy event argument" once
 

RODO

Member
You have to find the instance ID of your item. Otherwise, how will the machine know which one of the 1000's of the elixirs in your game you want destroyed?
Some options:

GML:
var _itm = instance_nearest(x, y, obj_elixir_innteration);        //THIS IS VERY IMPORTANT!

with(_itm){
    instance_destroy();
}
No need to supply argument that way, unless you feel very VERY fancy with the Destroy Event, but I"m not getting into that, even I never used that "skip destroy event argument" once

I understand, this is a very interesting command, thank you very much
 
I understand, this is a very interesting command, thank you very much
When checking functions, keep an eye in the manual in the rectangle when it says Return: something
You want one that says Return: Instance id. Every one of those will return an instance id which you can then use in that fashion.
From there, it's just a matter of picking the right function that suits you the most.
 
Top