General Actionscript Knowledge
- General knowledge Actionscript code explained
Table of Contents:
- Sounds
- Hit Test
- Drawing API
- Loading External Files
- Reading a TXT File
- Add From Library
1. Sounds
Sounds play an important role in flash games and applications.
First:
Create a new sound object
Code:
s = new Sound();
Second:
Set your sound linkage name in the library in this case it is set to "shotgun". Then add the sound to the sound object.
Code:
s.attachSound("shotgun");
Third:
Play the sound using the start command which uses the following parameters start(offset, loops) the offset changes where the sound clip will start playing. The loops is how many times the clip will get repeated.
Code:
s.start(0,100);
Fourth:
To stop the sound use
Code:
s.stop();
Other common sound commands:
On Complete Event: Runs the code when the sound has finished playing
Code:
s.onSoundComplete = function()
{
trace("Finished");
}
Get the duration of the clip: Returns the total duration of the clip
Code:
s.duration
Setting the Volume: Set the volume from anywhere from 0 to 100%
Code:
s.setVolume(50);
2. Hit Test (Object Collisions)
It is more than likely your current project will use this method of collision detection.
First Option:
Collision between whole objects. This method checks the boundaries of two objects.
Code:
if(_root.object1.hitTest(object2))
{
trace("true");
}
This will check for a collision between the two objects bounding boxes (the red and blue lines the the image below). If any of the the lines overlap then the result is true.

The problem with this though is that complex object it checks empty space.
To be more precise with hitTest you need to check individual points.
Code:
if(_root.object4.hitTest(object3._x-object3._width/2,object3._y+object3._height/2,true))
{
trace("true");
}
The above code will check the point in the below image in red for a collision with the circle object.

3. Drawing API
Have you ever need to draw something with actionscript? maybe draw a line between two points or a square around an object? This is how you do it.
Commands:
lineStyle(thinkness, color "0x"+hex,alpha);
moveTo(x,y); Moves the 'pen' to the x,y coordinates.
lineTo(x,y); Draws a line from the current pen position to the new coordinates.
beginFill(color "0x"+hex, alpha); starts filling the current drawing.
endFill(); stops filling the current drawing.
Examples:

Making a simple line:
Code:
createEmptyMovieClip("line",1);
Creates a new empty movie clip with a depth of 1.
Code:
line._x=line._y=50;
Moves the new clips position to 50,50.
Code:
line.lineStyle(3, 0xff0000, 100);
Sets the line clips to a line thinkness of 3, a color of 0xff0000 (red), 100% alpha.
The color works like this:
You have to type "0x" followed by your hex color code.
Code:
line.moveTo(0, 0);
Moves the pen to the coordinates 0,0 inside the line movie clip.
Code:
line.lineTo(100, 100);
Draws a line from 0,0 to 100,100.
It uses the lineStyle defined above.
Code:
createEmptyMovieClip("box",2);
Creates an empty movie clip with a depth of 2.
Code:
box._x=120;
box._y=100;
Moves the box movie clips position to 120,100
Code:
box.lineStyle(2, 0x00ff00, 100);
Sets the line style of the box movie clip to line thinkness of 2, a color of green, and alpha of 100%.
Code:
box.moveTo(0,0);
Moves the pen to 0,0 inside the box movie clip.
Code:
box.beginFill(0x00ff00,70);
Tells flash to being filling the drawing with a color of green and an alpha of 70%.
Code:
box.lineTo(100,0);
box.lineTo(100,100);
box.lineTo(0,100);
box.lineTo(0,0);
Draws a square with a width of 100 and height of 100.
Code:
box.endFill();
Ends filling the drawing.
One more example is to connect 3 movie clips with lines;
Code:
createEmptyMovieClip("connect",3);
connect._x=0;
connect._y=0;
Creates a empty movie clip called connect with a depth of 3.
Code:
connect.lineStyle(5,0x0000ff,40);
sets the connect movie clips line style to a line thinkness of 5, and a blue color, with 40% alpha.
Code:
connect.moveTo(mc1._x,mc1._y);
connect.lineTo(mc2._x,mc2._y);
connect.lineTo(mc3._x,mc3._y);
Moves the pen to the x and y position of the mc1.
Draws a line to mc2's x and y position.
Then continues drawing the line to mc3's x and y position.
4. Loading External Movies and Images
Every now and then you will need to load an external image or even another swf into your project.
Once you have a swf or image you need loaded into your project use the following code.
In my case i have an animation with the name dots.swf and an image called image.jpg.
Code:
createEmptyMovieClip("dots",1);
dots._x=20;
dots._y=20;
We need to create an empty movie clip with the name of "dots" and give it a depth of 1, then set the X and Y coordinates to 20,20.
Code:
dots.loadMovie("dots.swf");
Now we can load the external swf into our dots movie clip using the loadMovie command.
Code:
createEmptyMovieClip("jpg",2);
jpg._x=100;
jpg._y=50;
Now we need to do the same thing for our image. Create an empty movie clip with the name of jpg and a depth of 2. Then move the x and y to 100,50.
Code:
jpg.loadMovie("image.jpg");
Then using the same loadMovie action, replace "dots.swf" with "image.jpg"
5. Loading Text Files
First create a new txt file with the name "news.txt".
Put the following text inside the file
Code:
theNews=This is the news! It can easily be updated from this text file.
Second you need to create a new dynamic text box on the stage, give it an instance name of "newsBox", also change the box where it says "singleline" to "multiline"

Next enter the following actionscript code on the first frame:
Code:
loadText = new LoadVars();
loadText.load("news.txt");
loadText.onLoad = function(success) {
if (success) {
newsBox.text = this.theNews;
}
};
- The first line, loadText = new LoadVars(); creates a new object that will load variables from an external file.
- The next line, loadText.load("news.txt"); tells flash to load data from the file.
- The line, loadText.onLoad=function(success) { line creates a new function to be called on the loadText onLoad event. The function is sent one parameter (success).
- The next line, if(success) checks if success is equal to true mean that the file has been loaded successfully.
- The line, newsBox.text=this.theNews; sets the text value of our text box to the value of the loadText objects theNews variable. theNews variable comes from the very first part of the text file.
Thats it, test it out by pressing ctrl+enter, make sure that the .swf and .txt files are in the same directory or change the loadText.load("news.txt"); to point to the relative path of the text file.
6. Adding From the Library
When making games or applications that use a certain movie clip over and over, or multiple copy's of the movie clip, it is necessary to attach movie clips from the library.
When you attach a movie clip from the library you have to give the new movie clip a new name and a depth value.
The depth value its like layers, so if an object has a depth of 10, all other objects with a depth of anything less than 10 will be displayed under the object with a depth of 10 or more.
When you are attaching movie clips you have to make sure they each have their own depth value. If you attach a new movie clip on a depth where there is another movie clip. the old movie clip is replaced with the newer one.
First thing you will need to do is create a new movie clip, give it a name of anything. Then delete it from the stage.
Next open the library panel and right click on your new movie clip and then select linkage.
Fill out the dialog window like this:

Click ok and we can begin the actionscript.
Click on the first frame in the library and open the actions panel.
Enter the following
Code:
num=5;
the variable num will tell how many movie clips we are going to create.
Code:
for(i=0;i<num;i++)
{
This starts a FOR loop, it will run through the code each time the condition is true (i<num). Once it runs through the code it will add 1 to i until the condition is false.
Code:
newN="mc"+i;
This creates a new variable with the value of "mc" plus what i is equal too. If i is equal to 4 then newN will equal "mc4".
Code:
this.attachMovie("MyMC",newN,2+i);
This line is what attaches the movie clip to the stage. Because the code is running on the first frame of the timeline "this" is basically the same as "_root". The paramiters of the attachMovie command go like this, attachMovie(linkage name,new name, depth); So in the above code "MyMC" is the linkage name we gave the moviec clip. newN is the variable we created the line before, and the depth is equal to 2+i so each will have a different depth.
Code:
this[newN]._x=50;
this[newN]._y=50+(i*70);
this[newN].speed=i/2;
this[newN] calls the movie clip that which newN is equal too currently. So the first line sets the newly created movie clips x position too 50.
The second line sets the y position too 50+(i*70)
and the list line creates a new variable called speed inside the new movie clip and sets it equal too i/2.
Code:
this[newN].onEnterFrame=function()
{
this._x+=this.speed;
}
This last part creates a new onEnterFrame event for the new movie clip. Which mean that the movie clip will run the code assigned every frame. So every frame it will add the movie clips value of speed to its x value.
Because the function is running from the movie clip it means we can just use "this" instead of this[newN] because the code is no longer running from the _root of the file.
Code:
}
finish off the for loop.
On this line
Code:
this.attachMovie("MyMC",newN,2+i);
You are manually telling flash what the depth will be. An alternative way to do it is have flash assign the depth, like this
Code:
this.attachMovie("MyMC",newN,this.getNextHighestDepth());
Were done push ctrl + enter to try it out.
That's all for this tutorial. I know there is a lot yet to cover. Please post feedback, and ideas for future tutorials.
Advertisement: