PDA

View Full Version : [AS3]Functions for Loading Objects From Library



Gazurt
December 1st, 2007, 12:48 PM
I was working on this for two weeks and now I did it :jailbreak

attachMovie function:
function attachMovie(cn:String,nn:String,dp:int) {
var new_mc_class =getDefinitionByName(cn);
//
var new_mc_data =new new_mc_class();
new_mc_data.name=nn;
addChildAt(new_mc_data,dp);
return new_mc_data;
}An Example Code:
var my_lib_obj:MovieClip = attachMovie("my_mc","new_mc",0);
my_lib_obj.x=200;
my_lib_obj.y=300Parametres:
attachMovie(class_name:String,new_instance_name:St ring,depth:int);/////////////


attachBitmap function:
function attachBitmap(cn:String,nn:String,dp:int) {
var new_bt_class =getDefinitionByName(cn);
//
var new_bt_object = new new_bt_class(0,0);
var new_bt_data:Bitmap = new Bitmap(new_bt_object);
new_bt_data.name=nn;
addChildAt(new_bt_data,dp);
return new_bt_data;
}An Example Code:
var my_lib_obj:Bitmap = attachBitmap("my_image","new_image",0);
my_lib_obj.x=200;
my_lib_obj.y=300Parametres:
attachMovie(class_name:String,new_instance_name:St ring,depth:int);Thanks for Felixz (http://www.kirupa.com/forum/member.php?u=80688)

mprzybylski
December 1st, 2007, 08:47 PM
that is very cool, but personally i actually like the AS3 way of doing it better. It makes more sense to me and is cleaner than the older way. looks good though if you want to do it like you did it in AS2 :P

Loyx
December 2nd, 2007, 01:01 PM
Well, at least is good for people that prefer as2 and is being forced to use as3 in some movies (like me till now) ;)

Gazurt
December 2nd, 2007, 01:31 PM
Maybe the functions look like AS2 :) But the good thing is you can type String for Class Name... Only for this I wrote the functions :D

Greggeh
December 6th, 2007, 07:03 AM
okay Im a little bit confused, cant you just give the item from your libary a classname like "LibaryItem" and then in AS:

var myMc:LibaryItem = new LibaryItem()
this.addChild(myMc)

At least thats how I do it. Never tried it with bitmaps though but that should work right ?

mprzybylski
December 6th, 2007, 10:45 AM
yes Greggeh.

the other thing to point out here is that AS3 has its own depth management system so you don't really need all that stuff in there.

Gazurt
December 6th, 2007, 03:07 PM
I just want to use "String".Because of a project I had to do that :)