The following code generates an array then adds all the numbers in the array together. Really quite useless.
However the methods used to create this snippet are extremely useful.
Here I break them down.
var randomNumber:int = new int();
var myArray:Array = new Array();
var total:int = new int();
var totalNumbers:int = new int();
totalNumbers = 10;
First of all I declare some variables by doing this the variables can be used in and outside of functions. This is called variable scope
function generateNumbers(){
for(var i:uint; i<totalNumbers; i++){
Here I start a loop with a variable as the number to count to. This lets you modify the code easily
randomNumber = Math.ceil((Math.random()*(5^i^3)));
Here is the code that generates the random numbers. Math.ceil() rounds the numbers up to an interger. Math.random() generates a random number with is then multiplied by 5 to the power of "i"
myArray.push(randomNumber);
The push() function in an array add the value in the argument as a new value in the array.
}
for(var il:uint; il<totalNumbers; il++){
total += myArray[il];
Here the variable "il" counts up until it equals "totalNumbers" whilst it does that it cycles through the array and adds the values to the variable "total" using the operator +=
Calling the function
trace(myArray);
trace(total);
Here I trace to the output panel in flash to check the function is working. It is useful for debugging to put trace() functions in to see where the code is failing
So as you can see AS3 has many useful functions to help cut the time spent coding. Use these and you're on your way to becoming an ace programmer.
Custom classes are one of the new features in AS3 and they are very useful to competent Actionscript 3.0 coders.
The main idea of the custom class is just like that of a custom function. Both of which were designed to:
- Help organize code
- Rid of the need for repeating code
- Increase application performance
On a very basic level, both classes and functions both do this, however classes allow the user to be much more dynamic with there code.
An empty class looks like this:
package
{
{
public function NewClass()
{
}
}
}
The order of code for a class looks like this:
1.) Declare ‘package’
2.) Imports (Everything needs to be imported in a package / class)
3.) Declare class name + its parent type (”MovieClip, Sprite, EventListener)
4.) Main function (Run on class initiation)
This is the basics of custom class syntax, in the next tutorial, i will explain the use of these classes and how the improve you’re coding.
If you’re reading this tutorial, you should already be able to create movieclips and export them for use with actionscript. If you can’t, follow this tutorial first:
Flash CS3 Creating MovieClips
In this tutorial, we will use the simple example of adding a movieclip to the screen. In order to do this you must first create a variable and set its type as a movie clip.
In our previous examples our code would have looked like this:
var myVar:TextField = new TextField()
When setting our variable to become a movieclip all we have to do is replace textfield with the name we gave our variable when we exported. If you read our previous tutorial instead of TextField we would type MyMovieClip
var myVar:MyMovieClip = new MyMovieClip()