|
START LEARNING
FLASH NOW
|
|
Get instant access to over
45 minutes of FREE Flash
tutorials on video
and our newsletter.
|
.
.
.
. .
|
Flash MX - Common Errors
In this tutorial I will be going through some common errors and show
you how to resolve these errors. Finding errors and fixing them is known as
"debugging" in programming. It can be frustrating at times, but it is
an integral part of programming and the errors dont go away as you get better
at programming. The problems only get more complex!
I will start with naming movieclips and textfields, as this causes heaps of headaches and confusion. Then explain
where scripts should go.
Naming MovieClips
MovieClips in Flash MX can be named in a variety of ways. Lets go through them all.
- Draw a square and select it. Click on the Key "F8" or menuItem "Insert" - "Convert to Symbol".
-
Click on the "MovieClip" Behavior. In the "Name" textfield, type the name. Here our name will be "mc_square".
- Open up your Properties window ("Windows" - "Properties"). Make sure our square movieclip is selected. In the
Instance Name field type "square1". This will be our instance name of the square movieclip on the stage.

- Deselect the square mc(movieclip) and create a new layer. Lock the layer that has the square mc on it.
Double click on the new layer name and rename it "Control". With this layer selected,
open up your Actions Window (F9) and put this script in the window.
_root.square1._x = 300;
_root.square1._y = 250;
As you can see, I have use "_root." before our mc name. This is a good practice to get into. "_root." means the main timeline.
I have direct access to the properties of this movieclip on the stage. I can move it using the _x and _y properties.
- Play around with the script , using _alpha and any other mc properties.
// load
_root.onLoad = function(){
_root.square1._alpha = 90;
_root.square1._xscale = 200;
_root.square1._yscale = 150;
}
// loop
_root.onEnterFrame = function(){
_root.square1._x +=1;
_root.square1._xscale -= 1;
_root.square1._alpha -= 1;
}
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com
duplicateMovieClip naming
When we use duplicateMovieClip command, we are duplicating a movieclip already on the stage. It must be on the
stage to duplicate it. Use other commands for mc's that are only located in the library. We will go through them later.
- Delete the script in the Actions Window. Then put this code in there..
_root.onLoad = function(){
duplicateMovieClip(_root.square1,"square2",2);
_root.square2._x = 350;
_root.square2._y = 200;
}
// move both squares up and down in loop
_root.onEnterFrame = function(){
_root.square1._y += 1;
_root.square2._y -= 1;
}
The template for duplicateMovieClip is as follows:
duplicateMovieClip(oldName, newName, depth);
Make sure that you give the duplicated movieclip a different depth or level to the other/s.
If you have 2 movieclips on the same depth, then the top last one overridedes the other.
Every movieclip needs to be on its own depth.
-
So. There is our second usage (in programming) of our mc names. Let's make up a rule:
When duplicating MovieClips , use the instance name that we set in the properties window.
And, to duplicate a movieclip, the original needs to be on the stage already, and have an instance name.
Do you own a website that needs some creative elements? Get on the web to find information about Albany web design, as well as ecommerce.
Naming MovieClips using attachMovie
-
Open your Library window (Key F11). You should have one mc in there, called "mc_square".
-
Right click on this mc in the library and select "linkage" from the menu item that pops up. A "Linkage Properties"
window will open. Tick on the line "Export for Actionscript" and "Export in first frame". In the Identifier textbox,
write the actionscript name "box".
- Unlock the square_mc layer and delete the mc on the stage.
-
Select the Control layer, delete the code there, and put this code in there instead :
_root.attachMovie("box", b1, 1);
Notice that the b1 mc appears in the top left corner of the stage. Actually it has the centre of the movieclip
on the top left corner. The centre of the mc is (0,0).
- Now that we have the box on the stage, we can manipulate it around. Try some code to do this:
_root.attachMovie("box", "b1", 1);
_root.onEnterFrame = function(){
_root.b1._x += 1;
_root.b1._y += 1;
}
The template for attachMovie is as follows:
someMovieClip.attachMovie(idName,newName,depth)
createEmptyMovieClip
Also, attachMovie can be used in conjunction sometimes with the createEmptyMovieClip method.
The template for this method is as follows:
myMovieClip.createEmptyMovieClip (instanceName, depth)
Delete the code in the Actions window again and put this in:
_root.createEmptyMovieClip("empty_mc", 2);
_root.empty_mc.attachMovie("box", "b1",3);
_root.onEnterFrame = function(){
_root.empty_mc.b1._x +=1;
_root.empty_mc.b1._y +=1;
}
What we have done in this script is create an empty movie clip. The empty mc create is connected to the _root.
Then, the box mc can be attached to this empty mc. And we can call _root.empty_mc.b1.
As we have the setup above, empty_mc is a "container" for the box mc. This is a commonly used device. Let's see what we can do
with this.
Delete the script already there and insert this one:
// create empty mc
_root.createEmptyMovieClip("empty_mc", 2);
// attach b1 to empty mc
_root.empty_mc.attachMovie("box", "b1",3);
// attach b2 to empty mc
_root.empty_mc.attachMovie("box", "b2",4);
_root.onEnterFrame = function(){
// move the container clip
_root.empty_mc._x +=1;
_root.empty_mc._y +=1;
// rotate b1 clockwise
_root.empty_mc.b1._rotation +=5;
// rotate b2 anticlockwise
_root.empty_mc.b2._rotation -=5;
}
So, what happened? Pretty amazing, huh? What we did was move the container clip , which moved the whole shebang.
Then we rotated b1 and b2 by targeting the specific clip inside the container. Using a container clip allows us to
have control over a number of clips, or all the clips on the stage.
Having a container clip allows us to delete all mc's on the stage at once by using
container.removeMovieClip();
Otherwise we would have to go through all the mc's on the stage and delete them individually.
e.g.
_root.empty_mc.b1.removeMovieClip();
_root.empty_mc.b2.removeMovieClip();
Instead we can remove both clips in one go by:
_root.empty_mc.removeMovieClip();
Error Messages
The most common error message is something like this:
Scene=Scene 1, Layer=control, Frame=1: Line 1:
Clip events are permitted only for movie clip instances
onClipEvent(load){
What does it all mean? Let's try and replicate it first.
So we have solved our problem, right? Well not so fast. As we have it here, we have scripts on each movieclip
we have on the stage. The scripts are all over the place. We usually have to do this if we only have Flash 5.
But in Flash MX we can put the script all in one place which makes it easier to manage and debug. We can do it this way
in Flash MX. Put this script in the control layer. Delete any scripts on MovieClips.
_root.onLoad = function(){
// initialise the square mc
_root.square1._x = 100;
_root.square1._y = 100;
// and initialise any other mcs around
_root.square2._x = 140;
_root.square2._y = 100
}
Recommended Book
Flash Cartoon Animation: Learn From The Pros
by Glenn Kirkpatrick, Kevin Peaty, Glen Kirkpatrick
Buy now !
|
.
|