Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Animating a section of sub-images within one sprite [GM]  (Read 1127 times)

0 Members and 1 Guest are viewing this topic.
Animating a section of sub-images within one spr...
« on: November 10, 2007, 05:38:38 am »
  • Official Forum Hippie/Writer
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 200
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)"):
Code: [Select]
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):

Code: [Select]
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.
« Last Edit: November 10, 2007, 04:46:33 pm by Bearer »
Logged

mit

Re: Animating a section of sub-images within one...
« Reply #1 on: November 10, 2007, 04:40:12 pm »
  • QBASIC programmer since age 4. Take that, world.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 1079
Mmm, I actually remember someone requesting how to do this a few years back. I think I made an example of it too.

We had a little discussion about how it'll marginally reduce your file size, since for every sprite GM will store its name, origin, bounding box, etc, which is pointless if all your character's movement sprites have the same properties.



Ah, here it is: http://www.kousougames.co.uk/data/downloads/counter.php?file=OneSprite
Pretty similar idea.
Logged
Programmer / Spriter / Level designer / Game Director / Web Designer / Music Sequencer for
Random Highscore table:

Play the Kousou Arcade today!
  • Kousou Games
Re: Animating a section of sub-images within one...
« Reply #2 on: November 10, 2007, 08:32:39 pm »
  • Official Forum Hippie/Writer
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 200
Actually, I'm pretty sure it was ME that requested it in the first place, lol.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.04 seconds with 43 queries.

anything