It's a simple concept. Allow a function to call itself with a new parameter until a condition is met. A tried and true method of solving smaller problems until a larger one is solved. One of the simplest concepts in programming is rarely taken advantage of in Flash, but couldn't possibly fit better within Flash itself seeing how all our objects are like tree structures.
In mathematics and programming, recursion is when a function (or procedure) calls itself. In the following example we will increment a number with each pass until a condition is met. Never forget to place a condition within the recursion to avoid an infinite loop.
myCounter = function (i) {
// make sure we can leave the loop
if (i<=100)
{
trace(i)
i++
myCounter(i)
}
}
myCounter(0);By passing a parameter to the function we can the dig deeper into a problem with each pass. Take for example XML. A simple recursive function call can allow you to pass in a node within the tree and walk through each subsequent level of the node. Try this code in Flash with any XML doc or RSS feed online. (Note that I'm using 'for.. in' as a conditional here and in the remaining examples to end the recursion.)
traceNodes = function(theNode)
{
for(child in theNode.childNodes)
{
var currentNode = theNode.childNodes[child];
if(currentNode.nodeName != null)
{
trace(currentNode.nodeName);
traceNodes(currentNode);
}
}
}
loadrssXML = function() {
rssXML = new XML();
rssXML.ignoreWhite = true;
rssXML.onLoad = parseXML;
rssXML.load("http://www.actionscript.com/asc_news.xml");
}
parseXML = function ()
{
traceNodes(rssXML)
}
loadrssXML();You can walk through almost any object in Flash. Take a multidimensional array for example. With a recursive function, we can walk through each array within the parent array and get, change or delete values etc. All we have to do is pass the new child back to the function and walk through it. Take this function for example. It will walk through everything in a given object/timeline:
traceEverything = function (obj)
{
for (item in obj)
{
trace("Found: " + item + ", which is a " + typeof(obj[item]));
traceEverything(obj[item]);
}
}
traceEverything(this);While recursion on its own is an enormously powerful practice in programming and within Flash as we have seen here, we have access to a lot of information within our objects. Thankfully Flash has given us the expression 'typeOf'. In the ActionScript Dictionary, typeOf states:
'The typeof operator causes the Flash interpreter to evaluate expression; the result is a string specifying whether the expression is a string, movie clip, object, function, number, or Boolean value.'
By evaluating the object that is returned, we can setup our recursion to only give us back needed information. For example, if we wanted to loop through everything on a timeline, but only do something with the strings found we could check for 'string' and then do something with each string.
Something I have used recently is a recursive function that walks through the movieclip object. By passing in an object such as 'this' from the main timeline, we can look though and find each nested sub-sequential clip within that level. Take for example the follow recursive function in combination with typeOf:
traceClips = function (timeline)
{
for (clipName in timeline)
{
if (typeof (timeline[clipName]) == "movieclip")
{
trace(timeline);
traceClips(timeline[clipName]);
}
}
}
traceClips(this);Something as simple as this opens up many possibilities for a Flash developers. Imagine a situation where dynamic clips are attached or created frequently like browser windows on your pc. You could do a recursive function to loop through clips within clips to call a minimize function on all the windows. The sky is the limit here. With a bit of creativity and a few built in expressions, you can automate many processes within your movies and save lots of time.