by Mirza Hatipovic
In this example i will extend the MovieClip class and create custom methods similar to createEmptyMovieClip, createTextField and attachMovie. The difference is that you don't have to worry about the depth, you only need to provide the instance name. The new methods are the following:
easyCreateEmptyMovieClip(instanceName);
easyCreateTextField(instanceName);
easyAttachMovie(identifier, instanceName, [init_obj]);
First we add the easyCreateEmptyMovieClip method. Note that all the code will be placed on the first frame of a blank layer. Here's the code for the first method:
MovieClip.prototype.easyCreateEmptyMovieClip = function(instance_name) {
var depth_count = 0;
for (var clip in this) {
if (typeof this[clip] == "movieclip" || this[clip] instanceof TextField) {
if (this[clip].getDepth()>0) {
depth_count++;
}
}
}
return this.createEmptyMovieClip(instance_name, ++depth_count);
};
Just like the «original» method, the method returns the reference to the newly created clip.
Now on to the easyCreateTextField method:
MovieClip.prototype.easyCreateTextField = function(instance_name, x, y, width, height) {
var depth_count = 0;
for (var clip in this) {
if (typeof this[clip] == "movieclip" || this[clip] instanceof TextField) {
if (this[clip].getDepth()>0) {
depth_count++;
}
}
}
return this.createTextField(instance_name, ++depth_count, x, y, width, height);
};
The final method is the easyAttachMovie method:
MovieClip.prototype.easyAttachMovie = function(identifier, instance_name, init_obj) {
var depth_count = 0;
for (var clip in this) {
if (typeof this[clip] == "movieclip" || this[clip] instanceof TextField) {
if (this[clip].getDepth()>0) {
depth_count++;
}
}
}
if (arguments.length == 3) {
return this.attachMovie(identifier, instance_name, ++depth_count, init_obj);
} else {
return this.attachMovie(identifier, instance_name, ++depth_count);
}
};
Just like the built-in attachMovie method, the method accepts an optional third argument (initObj).
Here are some code examples:
//create empty movie clips and don't worry about the depth
_root.easyCreateEmptyMovieClip("line1_mc");
line1_mc.moveTo(100, 100);
line1_mc.lineStyle(1, 0x000000, 100);
line1_mc.lineTo(200, 50);
//second clip
_root.easyCreateEmptyMovieClip("line2_mc");
line2_mc.moveTo(200, 200);
line2_mc.lineStyle(1, 0xff0000, 100);
line2_mc.lineTo(200, 50);
//third clip
_root.easyCreateEmptyMovieClip("line3_mc");
line3_mc.moveTo(300, 300);
line3_mc.lineStyle(1, 0xffff00, 100);
line3_mc.lineTo(200, 50);
//create a textfield and don't worry about the depth of the previous mc's
_root.easyCreateTextField("message_txt", 200, 200, 200, 50);
message_txt.border = true;
message_txt.text = "hi";
//same for attachMovie without providing the depth
_root.easyAttachMovie("circle", "circle1_mc");
_root.easyAttachMovie("circle", "circle2_mc");
circle2_mc._x = 200;
Please download the source .fla code and study the provided example code.