Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have this sprite I have to animate. The sprite is 7 images total but animation is 10 frames (2 positions are repeated).

The order I want to go through the frames is like this:

3 -> 4 -> 5 -> 6 -> 4 -> 3 -> 2 -> 1 -> 0 -> 2.

My problem is how can I skip 1 frame once I reach the end of each direction? The reason I want to skip is to save me from creating duplicate positions in the spritesheet.

I'm doing this in Javascript.

share|improve this question
"The way I want to go through the frames is like this" What is the algorithm here? I'm not really seeing it. – Nicol Bolas Jul 28 '12 at 4:00
no algorithm, just the order to go through the tiles, I guess I was unclear – hustlerinc Jul 28 '12 at 4:02
We can't really help you because we don't know what language or animation engine you may be using. I would use an array of integers to decide the animation frame sequence but that might not be an option depending on the language/engine you're using. – Austin Brunkhorst Jul 28 '12 at 4:05
Didn't think language was relevant, since I'm just after an explanation, but sure I'll add that to the question. – hustlerinc Jul 28 '12 at 4:07
You need to give us how you are animating the sprites as well. – Austin Brunkhorst Jul 28 '12 at 4:18
show 1 more comment

2 Answers

up vote 1 down vote accepted
  1. Record your animation sequence into array;
  2. Remember the step you show at this frame;
  3. When next frame is prepared make the step increase, if step becomes more than animation length - set it to 0 again;
  4. Now you have looped animation.
  5. List item

AnimStep: Byte;
AnimSprite: array [3, 4, 5, 6, 4, 3, 2, 1, 0, 2];
AnimLength: Byte;
Render
{
  ..
  AnimStep = (AnimStep + 1) mod AnimLength;
  RenderSprite(AnimSprite[AnimStep]);
  ..
}
share|improve this answer
Putting the frame order in an array was a facepalm moment. Sometimes you miss the most obvious solutions because of the simplicity, thanks. =) – hustlerinc Jul 28 '12 at 7:06
var animationSequence = [3, 4, 5, 6, 4, 3, 2, 1, 0, 2];
var currentFrame = 0;
var totalFrames = animationSequence.length;

// to advance a frame which will wrap round once it reaches the end
currentFrame = (currentFrame + 1) % totalFrames;

// to get current frame
var animationFrameToPlot = animationSequence[currentFrame];

An example to see it working in jsFiddle

share|improve this answer
can't get the fiddle to work, it just shows a bunch of numbers. +1 for putting the frame number in array though. Can't believe it didn't cross my mind. Also, what does '%' do in JS? – hustlerinc Jul 28 '12 at 7:07
% is the modulo operator. And the jsFiddle is working fine, it's supposed to show the looping iterating process. – Alayric Jul 28 '12 at 7:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.