Wintermute Engine Forum

Wintermute Engine => Feature requests, suggestions => Topic started by: fireside on June 28, 2008, 04:32:00 AM

Title: sprite stepping
Post by: fireside on June 28, 2008, 04:32:00 AM
This would be a nice feature for sprites.  Right now, you can play a sprite animation, or you can loop.  It would be cool if there was a step, so you could call a step function and then it would only play the next frame and hold on it until the step function was called again. 
Title: Re: sprite stepping
Post by: warmblood on July 03, 2008, 04:10:31 PM
The beauty and power of WME is that you can do some of these things yourself*.  Just add this method to the script for the entity you want to step:

Code: [Select]

method StepSprite(stepFrames, looping)
{
   var theSprite=this.GetSpriteObject();
   var numFrames=theSprite.NumFrames;
   var nextFrame=theSprite.CurrentFrame + stepFrames;
   if (nextFrame >= numFrames) {
      if (looping)
         theSprite.CurrentFrame=0;
      else
         theSprite.CurrentFrame=numFrames-1;
   }
   else {
      if (nextFrame < 0) {
         if (looping)
            theSprite.CurrentFrame=numFrames-1;
         else
            theSprite.CurrentFrame=0;
      }
      else
         theSprite.CurrentFrame=nextFrame;
   }

   return theSprite.CurrentFrame;
}

EDIT: If stepFrames is +, the method steps forward through the sprite frames. If it is -, the method steps backward through the sprite frames.
If looping is true, when an "end" is reached (first frame or last frame) it loops back to the other end. If false, it stops at the end.


To test it:
1) In a new scene, create a non-looping sprite with at least 3 or 4 frames.
2) Create a sprite entity in your scene using this sprite.
3) Attach a script to the entity. The script should contain the code above.
4) In the same script, include the following event handler:

Code: [Select]
on "leftClick"
{
  var step=1;
  var loop=true;
  var ret = this.StepSprite(step,loop);
  Game.Msg("CurrentFrame = " + ret);
}

   

5) Run the example and click on the entity. You should step through the sprite frames one at a time, looping back to the beginning when you reach the end.
6) Try different values for step:
            if step=2   the sprite should step 2 at a time
            if step=-1  the sprite should step backwards one frame at a time.
            if step=-2  the sprite should step backwards two frames at a time.
7) If you change the variable loop to false, the sprite won't loop but otherwise you can step freely between frames.

I hope that helps.

EDIT: *Of course you probably knew that, but I hope it helps others who also want the functionality but don't want to wait!  :D




Title: Re: sprite stepping
Post by: fireside on July 08, 2008, 03:08:55 AM
I'll try it out, thanks.