As promised I will cover topics mentioned in the first article in addition to several new concepts:
*Health Bars
*The Distance Formula
*Projectiles
*Rotation

Jumping right in...

Health Bars
Health bars can be extremely helpful in writing games. Although called health bars you can use this concept to represent any value in a game, time remaining, hp, mp, reload times, ammo, whatever you need.

Alright, so when working with healthbars, we will use two variables: health and maxhealth.

Code

health = 50;
maxhealth = 100;

The code:
Lines 1 & 2 declare two variables, health and maxhealth. The variable health will change as a character gets wounded, but maxhealth will always remain the same. So lets say the character has 50 health and 100 maxhealth, we know they are at 50% (50/100 * 100) health.

But what if the two numbers are not so easy to calculate? Maybe 26/31? Turning this into a percent is fairly easy. You probably do this every time you get a test back from a teacher. Divide the two numbers, then multiply by 100.

So 26/31 * 100 = 83.8709677%

A number with decimals like the number above will not work with the healthbars we are about to create. So we can use the Math.round(number) to round this number off nicely.

Explanation:
Line 1: Tells flash to run this script every frame (enterFrame)
Line 2: Uses the variables to calculate current health (And adds one. Base zero issue, don't worry about this)
Line 3: Sets the frame of the movieclip to the currenthealth number
Line 4: Closes the enterFrame event

Congratulations, you've got your healthbar :)

So pretty much, turn variables into a percent, round the number to an integer, and tell healthbar to go to that frame.

The Distance Formula

Being able to find the distance between two points is very useful. Which enemy is closest? Where is the closest repair station?

Line 1: Tells flash to run this code when the movieclip loads (load)
Line 2: Defines a function. Functions are groups of code that can be run together to perform tasks.
Line 3: Comment.
Line 4: Sets the variable DeltaXSquared to the difference of the x values of the points squared.
Line 5: Sets the variable DeltaYSquared to the difference of the y values of the points squared.
Line 6: Sets the variable Distance to the square root of the sum of DeltaXSquared and DeltaYSquared
Line 7: Returns the value Distance. This is how information is retrieved from functions.
Line 8: Ends the function
Line 9: Ends the load handler
Line 10: Tells flash to execute the following code on every frame (enterFrame)
Line 11: Calls the function from line 2, feeding it values of the x and y locations of the two points
Line 12: Sets the text on the stage to the distance
Line 13: Ends the enterFrame handler.

Thats it! Although directly this may not seem useful, we can apply this other concepts. Such as...

The example shown above is quite complicated, and will use just about everything youv'e learned so far, including movieclip duplication and the distance formula. Also a little bit of algebra helps a lot :)

The plan:
1) We are going to have a movieclip named &quot;bullet&quot; which will be black dot. It will be duplicated over and over again to be fired at the mouse.
2) We are going to have a movieclip named &quot;tower&quot; which will fire the bullets every so often.
3) The variable _root.i will give each bullet a unique name

Create a movieclip named bullet, drag it onto the stage and name it bullet
Create a movieclip named tower, drag it onto the stage and name it tower

The How: Each frame we add one to count, and once this happens 10 times we duplicate the movie clip with a new name

The Code:
Line 1: The load event handler
Line 2: Sets the variable count to 0
Line 3 Sets the variable maxcount to 10
Line 4: Ends load handler
Line 5: Starts enterFrame handler
Line 6: Adds 1 to the variable count
Line 7: Checks to see if count is greater and maxcount
Line 8: If it is, it duplicates &quot;bullet&quot;, gives it a new name &quot;bullet&quot;+i and puts it on its own layer
Line 9: Adds 1 to _root.i for a new unique name
Line 9: Sets count back to 0
Line 10: Ends if statement
Line 11: Ends enterFrame handler

So there is our tower, which will duplicate our bullet every 10 frames.

The How: Each frame, we find the distance between the bullet and the mouse, divide that by the speed at which the bullet moves each frame to figure out how many frames it is going to take to complete the journey. Then we only think about 1 such &quot;click&quot; or how far it moves on one frame. We take the total distance of the two points, divide that by 'clicks' to find how far the bullet should move on each axis each frame. Then the bullet moves this amount each frame.

That is a big chunk of code, but its actually pretty simple. Lets break it apart and look at it. (you might want to download the file to look at while reading this)

Line1: Declares the on load handler.
Lines 2-3: Moves the new bullet to the tower
Line 4: Sets the variable 'speed' to 6. Change this to change how fast the bullet will move.
Lines 5-9: So in the distance formula you remember x1, y1 ,x2 and y2? Now we define these as the x and y locations of the mouse and the tower so we can launch a projectile between the two points.
Line 10: Calls the function distance to find the distance of the target.
Line 11: Sets 'changex' to the difference of the x locations between the bullet and tower
Line 12: Sets 'changey' to the difference of the y locations between the bullet and tower
Line 13: Sets 'clicks' to the distance divided by speed. So pretty much we find the rate (Distance = rate * time) or the number of times the bullet is going to move to arrive at its destination.
Line 14: Now we set 'movex' (How much x is going to change each frame) to changex/clicks. This is how much x is going to change during 1 &quot;click&quot; or move forward. Multiply by -1, don't worry about this.
Line 15: Now we set 'movey' (How much y is going to change each frame) to changey/clicks. This is how much y is going to change during 1 &quot;click&quot; or move forward. Multiply by -1, don't worry about this.
Lines 16-20: The distance formula in function form.
Lines 22-25: Each frame the bullet moves 'movex' on the x axis and moves 'movey' on the y axis.

Thats it for the advanced code!

Place

Code

_root.i=0;

on the first frame to initialize the creation of the bullets, and you're done!