Windows Draw_Sprite_Part

Chaser

Member
Hello, been racking my head around this little problem i have so maybe you guys and gals could help me?

Basically i'm trying to create animations from sprites i have 'built' using the draw_sprite-part functions.

The object i have has no sprite index, nor a image index as a result of having no sprite index. Its just an object with a draw event. The draw event has the following code:

draw_sprite_part(spr_Vigilante,0,0,48,16,16,obj_Player_Prime.x,obj_Player_Prime.y); // top half
draw_sprite_part(spr_Vigilante,0,0,64,16,16,obj_Player_Prime.x,obj_Player_Prime.y+16); // bottom half

This works fine to draw like this but it is not a 'frame', i want to be able to add 4 frames to this object and on each frame draw the sprite using the draw_sprite_part function. So if frame is 0 draw dadada, frame1 draw dadada, frame2 dadad and so on. :) this seems pretty straight forward enough(to me it did) but need to obviously tell gamemaker what the frames are. I'm not to sure whats the best way to do this, create a script maybe? or give the object a BLANK sprite index with four BLANK subimages and have the draw events do the rest because the draw event overwrites any sprite image assigned to the object?. I did try this, and the result worked, sort of, the sprite animates my draw sprite parts but it flashes, which is not what i want. I have been reading other posts and they talk about image_index, setting it to -1, or image_index as gamemaker will automatically run through image speeds but i'm having no luck that way, maybe its simular to what im trying to do but not exactly. I have been reading the manual to, and i'm clearly not getting something.

my logic is this:
if image_index = 0 draw_sprite_part
if image_index = 1 draw_sprite_part etc

my logic is completely flawed so i need someone to check me.:)

The way the Import strip function works in the image editor would be awesome if we could do that manually in code. I don't want to create lots of strips to store, i want to be able to use one sprite sheet and build the sprites using that when an image index or frame is actioned. does that make sense? Think how NES/Master system worked from a bitmap, that's kind of what i'm trying to achieve. I'm not looking for someone to say heres the code you need, or do this, i'm looking for someone to explain to me how it works using gamemaker, and maybe how my thinking is sending me down the wrong path, i want to understand the code i'm using. :)

thanks in advance
 

Nidoking

Member
I don't understand why you can't make the sprites either as individual sprites with four frames (which would mean you'd just use image_index for the second argument to draw_sprite_part) or as separate images (so that you'd choose the sprite_index based on the value of image_index, using the if/else structure you described). You need a sprite to use draw_sprite_part.

If what you're talking about is grabbing each frame of each sprite from one giant sprite sheet, then you'd need to calculate the x and y for each frame of each sprite based on the image_index. I would store those in variables, then just call draw_sprite_part at the end using the values you calculated. That way, you can adjust each calculation until you get the coordinates right for each frame.
 

Chaser

Member
I don't understand why you can't make the sprites either as individual sprites with four frames (which would mean you'd just use image_index for the second argument to draw_sprite_part) or as separate images (so that you'd choose the sprite_index based on the value of image_index, using the if/else structure you described). You need a sprite to use draw_sprite_part.

Yep, your right, I do do it this way normally, but when you have a 10+ characters that have 8 directions and has 20+ states which require different sprites etc i tend to find it would be much better to work from code using one sprite sheet and build the animations that way, more code work i know but i would sooner have that then an IDE sprite list as long as a mile long, secondly i'm not intending to have them in the actual game build, rather call the sprite sheet from a server and build the animations that way. I'm sure il figure it out, thanks for your help. :)
 

CloseRange

Member
I think I understand what you're doing. I had to work with about 50 sheets once each containing all walk cycles along with 20 frames of attacks. A lot easier to do in code than it is to hand separate them out.
You need to create a script that takes a few arguments. The x and y CELL to draw, the sprite to draw, and the x and y position to draw the frame at. If you want you can also add a scale if you want to scale the frames up.
Make this script draw whatever frame you choose (the frame being denoted by the x and y cell)
then make a custom "image_index" variable (not the built in one) and just have it increase by 1 every step, and mod it with the frame count to have it loop:
Code:
frame += 1;
frame %= 4;
that code will make frame go in this pattern: 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 ...
so you can use the frame variable as a means to animate.
Then just calculate what the x and y cell is based on frame, and draw whatever frame you need based on that.
 

Chaser

Member
Thanks @CloseRange, I'm not the only one to have thought of this way then. :) I'l give your code some thought and give it a go, i'm experimenting with Sprite_add and the async_load events. This seems to be going OK at first draft, i am having to load the animation strips to a server, which isn't that bad because i'm exporting the animations anyway to save else where, whats good to know is when i have done that i can actually call them when needed for any project object(so far anyway), and will run the animations without the need of a variable. Sprite collision margins maybe an issue, but i havent got to that bridge yet. :)

I think it would be good for GMS2 to add those functions in its GML, i know we can import and convert in the editor which is awesome, but be really cool to be able to do the same thing in code for those of us that don't want to store a million sprites in our IDE interface. Or maybe there is i just haven't found it (yet) until then we will have to find our own way using our own scripts and things. Cheers!
 
Top