Flash MX prototyping allows you to extend your movies in ways you never thought possible. By using prototyping you can share an effect or function among several movies without the need to replicate the code. This leads to your movies being more efficient, smaller in size and easier to code. Prototyping is not for the faint of heart so if you’re new to ActionScript or just programming in general you may want to brush up on the basics first. This article will provide the foundations to begin prototyping for yourself as well as provide some solutions to commonly asked questions in the forum.
Flash MX Prototyping Basics - A Side Note on Paths (Page 2 of 4 )
An important factor when writing prototypes is getting your paths correct. In the centerAlign function, the _x and _y are set using this as the target. That's because it's written in the scope of the movie clip and not _root where the code is actually written. Let's run through some examples of what can go wrong.
You have a movie clip that has a child movie clip and they both have the same instance name, let's call them both subMenu. You write a new prototype in the first key frame of _root that should change the position of the child clip, like expanding a sub menu. Inside the prototype you have:
subMenu._x += 100;
That's not actually referring to the child as you might imagine, instead it is referring to the main movie clip in _root. To get the scope right, you must refer to it like:
this.subMenu._x += 100;
A similar problem occurs when using variables. If the variable is not used in the right scope, all kinds of chaos can arise. A typical example of variable problems might be when a user reports "I'm sure the data is getting added correctly but when I try and use it it's not there". That's because the data has been successfully added to a variable in a different scope.
MovieClip.prototype.mover = function() { this.createEmptyTextField("debugText", 1, 0, 0, 100, 10); this.debugText.variable = debugVar; debugVar = "This is in the wrong scope"; // The correct way to address this // this.debugVar = "This is the correct scope"; } myMovie.mover(); // This will return undefined trace(myMovie.debugVar); // This shows that the sentence we meant to assign to the text box in myMovie was actually created in the parent. Trace(debugVar);