Recursion in Flash


Recursion is a programming technique that is used in many important applications such as “sorting”, “parsing”, and “searching” among many others.

 

Many programmers shy away from using this techniques because the logic behind it can be quite abstract. However, the basic idea of recursion is a function that calls itself in a finite number of times. Finiteness is a very important part to consider when dealing with recursion, since it is very easy to get an infinite loop when doing recursion. Recursive calls utilize the stack, as such the first call to the recursive function will eventually finish last (LIFO).

 

In this post, I am going to show one of the most widely used recursion example, i.e. drawing a tree branch. However, if you are interested to see how recursion can be used for “sorting” (QuickSort algorithm in this case - one of the faster sorting algorithm), feel free to download it here.

 

Here is a preview of the final product:

 


 

To begin, let’s take a look at the complete code first:

 

 var pi:Number = Math.PI;

function drawBranch(start_x:Number, start_y:Number, color:Number, length:Number, angle:Number, thickness:Number, level:Number):Void {
if (level > 0) {
this.lineStyle(thickness, color, 100);
this.moveTo(start_x, start_y);
var end_x:Number = start_x-length*Math.cos(angle);
var end_y:Number = start_y-length*Math.sin(angle);
this.lineTo(end_x, end_y);

--level;
drawBranch(end_x, end_y, color << 1, length/1.4, angle+pi/4, thickness/2, level);
drawBranch(end_x, end_y, color << 1, length/1.4, angle-pi/4, thickness/2, level);
}
}

drawBranch(100, 120, 0x993300, 20, pi/2, 8, 10);

 

Let’s look at the following line

var pi:Number = Math.PI;

The purpose of this is just to store value of Math.PI. The reason I put it here and not inside of the function is for optimization by avoiding multiple Math.PI calls

 

The next line of code is

function drawBranch(start_x:Number, start_y:Number, color:Number, length:Number, angle:Number, thickness:Number, level:Number):Void {

It is just the function declaration with a whole bunch of parameters that is dynamic according to the recursion level.

 

The next line of code is

if (level > 0) {

This is a vital part of the code. This is what defines the finiteness of your recursive function. Without this, your recursive function will go in an infinite loop.

 

The next line of codes is

 this.lineStyle(thickness, color, 100);
this.moveTo(start_x, start_y);
var end_x:Number = start_x-length*Math.cos(angle);
var end_y:Number = start_y-length*Math.sin(angle);
this.lineTo(end_x, end_y);

This is what draws the tree branch. It utilizes the Flash drawing API. There are many good tutorials out there if you are not familiar with them. But basically this.lineStyle(thickness:Number, color:Number, alpha:Number) is what defines the style of your line stroke. this.moveTo(x:Number, y:Number) position the starting point of your line. this.lineTo(x:Number, y:Number) position the ending point of your line. So your line will drawn from those 2 points that you define here.

 

The next line of code is

--level;

This is another vital part of the code. This code is what makes the finiteness rule (in this case, if (level > 0)) of this code possible.

 

The next line of codes is

 drawBranch(end_x, end_y, color << 1, length/1.4, angle+pi/4, thickness/2, level);
drawBranch(end_x, end_y, color << 1, length/1.4, angle-pi/4, thickness/2, level);

This is the recursive call. Notice how it is calling the function itself. Also notice the difference between the 2 lines. One has angle+pi/4 and the other has angle-pi/4. The only reason for calling it twice is because your tree will be 1 sided otherwise.

 

1 comment

Flash behind HTML


Sometimes integrating your Flash movie into the HTML page may cause some layout issues such as your HTML dropdown menu appearing below the Flash movie.

 

Those are caused because by default browsers place embedded content on the topmost layer.

 

A fast fix to the above problem would be to add the “wmode” and setting the value to “transparent” in the embed tag. (Note: The “wmode” tag is not supported in some browsers)

 

However, what if the background color of your Flash movie is not the same as the color of your HTML page? (Might not be the best example, but for argument sake)

 

This is when the putting “Flash behind HTML” technique might come in handy.

To achieve this, we need to utilize layering in the HTML page, and another possible value for the “wmode” tag, which is “opaque”.

 

Layering in CSS has pretty much the same concept like layering in Flash. To do layering using CSS, we need to use a special attribute called the z-index. The smaller the z-index, the lower in the stack they are. So stuff that are put in layer 2 (or z-index:2) will block those that are put in layer 1 (or z-index: 1).

 

Do not use negative z-index, because it won’t show up on Mozilla. If there are 2 objects with the same layer, whichever comes later will be on top of the one that comes before it.

 

This is the HTML to embed your Flash movie behind the HTML (to test it just grab the code below and make a simple Flash movie and name it “yourmovie.swf”):

 


 <div style="position:relative; font-size:30px; z-index:5;">LAYER 5</div>
 <div style="position:relative; font-size:30px; z-index:2">LAYER 2 before flash</div>
 <div style="position:absolute; top:0; left:0; font-size:120px; z-index:2;">
 	<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="200" height="150" id="recursion_tree_branch" align="middle">
 		<param name="allowScriptAccess" value="sameDomain" />
 		<param name="wmode" value="opaque" />
 		<param name="movie" value="yourmovie.swf" />
 		<param name="quality" value="high" />
 		<param name="bgcolor" value="#e7e7e7" />
 		<embed src="yourmovie.swf" mce_src="yourmovie.swf" wmode="opaque" quality="high" bgcolor="#e7e7e7" width="200" height="150" name="yourmovie" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
 	</object>
 </div>
 <div style="position:relative; font-size:30px; z-index:4">LAYER 4</div>
 <div style="position:relative; font-size:30px; z-index:2">LAYER 2 after flash</div>
 <div style="font-size:30px; z-index:1">LAYER 1</div>

 

7 comments