Looping frames with currentFrame & totalFrames AS3 Snippet

Looping frames with currentFrame & totalFrames AS3 Snippet
16th January 2012 Shane

In this tutorial we modify our swipe code so that, a left swipe on our last frame will loop to the first frame and a right swipe on our first frame will loop to the last frame.

I’m using the hideLogo_completeC.fla file from the previous tutorial sample files and have added a conditional statement inside our swipe code, in which we use the “currentFrame” and “totalFrames” properties to loop through the frames.

Note: Directions are reversed on a mobile device, so a swipe left would go to the next page and a swipe right would go to the previous page.

I have added the following conditional statement to our swipe right code.

if(currentFrame == 1)
{
gotoAndStop(totalFrames);
}
else
{
prevFrame();
}

Above we are asking Flash to check if the play head is on the first frame, if so, then move the play head to the very last frame, otherwise move to the previous frame.”

Here’s the code snippet for swipe left:

if(currentFrame == totalFrames)
{
gotoAndStop(1);
}
else
{
nextFrame();
}

Here we are asking flash to check if the play head is on the last frame, if so then go to the first frame, otherwise go to the next frame.

Note: Because we used the “currentFrame” and “totalFrames” properties, and not specific frame numbers, we can add as many frames as we like to our test app, and the code will always work, check out the sample code for more info.

To access code samples, Click Here.
Tutorial files were created using Flash CS5 Professional version 11.0.2.489.

Contents of Archive (2 Files)
loopFramesC.fla (fully commented test app)
loopFrames.txt (info)

Copyright © Shane Hogan
Do not copy this article.

My first iOS App ScaleViz created entirely in ActionScript 3.0, went live July 2011, and shows how music theory relates to your guitar fretboard, you can check it out here http://appstore/scaleviz I have also created Annual Reviews for companies like Mind, FAQ's for Epson UK, taught ECDL and I'm a zombie in the feature film Wasteland UK My hobbies include playing the guitar, watching horror films, astronomy, snorkeling, hypnosis NLP and expanding my knowledge in all areas of programming and graphic design.