Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Recursion in Flash

by ilyas usal   | filed under Flash and ActionScript

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

We call recursion the fact of allowing a function to call itself until some condition is met. It is really useful when you're doing complicated math calculations AND when you're trying to draw repetitive patters in Flash. Now I know you guys would love to hear about the math, but life's unfair, I'll only talk about the Flash.

All right, so a recursive function is a function that does stuff, and then calls itself, possibly with a different set of parameter, until we tell it to stop. To illustrate this with a simple example, we are going to create a function that takes an integer as a parameter, outputs it and then calls itself with the next integer, until 5 is reached.

function traceMe(n) {
  if (n<=5) {
 trace("This is the "+n+"th time that the function is run.");
 traceMe(n+1);
 trace("End of the "+n+"th function.");
  }
}
traceMe(1);

[ copy and paste the above code into your actions window ]

Running the above code by pressing Ctrl + Enter will produce the following output:

This is the 1th time that the function is run.

  This is the 2th time that the function is run.

  This is the 3th time that the function is run.

  This is the 4th time that the function is run.

  This is the 5th time that the function is run.

  End of the 5th function.

  End of the 4th function.

  End of the 3th function.

  End of the 2th function.

  End of the 1th function.

Let's have a look at how the function works in detail:

That, my friends, is what is called recursion.

Dangers of Recursion
I'm sure you can all see the danger here: if no condition is set to stop the loop, Flash will call the function over and over and over (and over) within the same frame. So if you feel like crashing your computer (well, not your computer, but at least your Flash movie...), just remove the if (...) line (and the corresponding bracket, of course). By doing that you create an infinite loop. That's why it is very important to set a condition to stop the loop from going on forever.

There are more tutorials similar to this, so be sure to check out the ones that follow this tutorial such as fractals, etc.

Ilyas Usal
{Pictures 1 | 2}

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--