9
« on: November 10, 2007, 05:38:38 am »
well, I needed this for something a while back, and thought it might be useful to someone. For those of you out there who don't want lots of different sprites for your running animations, you can put all of the frames of that .gif file into one sprite in GM (works in 6 and below, not sure about 7 yet, though it probably does) and then loop just a section of those subimages rather simply. First, you'll need a script kind of like this one (I've named it "scr_anim_within(start,tot,time)"):
start=argument0;
tot=argument1
time=argument2;
if((self.image_index>=(start))&&(self.image_index<(start+tot-1))) //if the image_index is >= to the starting subimage and < the last
{
self.image_speed=time; //this starts the animation
}
else
{
self.image_index=start; //sets the animation back to the begining
}
start being the first subimage in your animation, tot being the number of subimages in your animation, and time is how fast you want it to loop.
Also, make sure that the sprite that contains all of the animations has them arranged in a way like this: down, right, up, left. this just makes it easier.
Now, in your player object, you're going to want a variable that holds what direction you are going. It's best to assign it a numeric value for the sake of easier mathematics. I named mine, "dir", simple enough? it can equal 0, 1, 2, or 3. 0=down, 1=right, 2=up, and 3=left. In the "End Step" event, put some code like this (I've commented bits of it so it's easier to understand):
switch(state) //the variable that holds whether or not I'm moving
{
case(0): //if I am standing
sprite_index=spr_plr_stand;
image_index=dir;
image_speed=0;
break;
case(1): //if I am moving
sprite_index=spr_plr_run;
scr_anim_within(8*dir,8,1/3);
break;
}
ok, so the place where it says: scr_anim_within(8*dir,8,1/3) is a little confuseing. See, my sprite has 8 subimages per animation. By useing the dir variable, it can go to the start of any animation cleanly. Example: If dir=0 (If I'm facing down), it will start the animation at subimage 0, and run on until subimage 7, then loop. However, if dir=1 (I'm facing right), the the animation will start at subimage 8 and run until subimage 15.
the other sprite I have is a seperate one for the standing frames (which have no animation). I just set it up similarly to the one with the animations so that all I need to do is set the image_index to dir.
Now, I'm sure there are better ways to do this (I haven't used GM in a while), but this works for me just fine. Hope it helps some of you guys out a bit. It's quite useful for keeping the number of sprites down to a minimum.