3

I've got in Library few MovieClips and I want to make function which is loading them into stage. My problem is I can not found different solution than making separate functions for every MovieClip. I'm looking for something like that:

function addAnyClip(name){
  //create new object 'name'
  stage.addChild(chosenObject);
  rest of code
}

because it's better than:

function addClip1(){
  var mc1:Clip1 = new Clip1;
  stage.addChild(mc1);
  ///rest of code
}

function addClip2(){
  var mc1:Clip2 = new Clip2;
  stage.addChild(mc2);
  ///rest of code
}

function addClip3(){
  var mc1:Clip3 = new Clip3;
  stage.addChild(mc3);
  ///rest of code
}
...

2 Answers 2

5

Look into using getDefinitionByName

You would do something like the following:

var mcClass:Class = getDefinitionByName("NameOfClipInLibrary")

And then just create anew object that is of the class type mcClass

Here are a couple of links to help explain how to use it... http://www.jesseknowles.com/blog/dynamically_attaching_movieclips_in_as3/

http://www.emanueleferonato.com/2011/03/31/understanding-as3-getdefinitionbyname-for-all-eval-maniacs/

Sign up to request clarification or add additional context in comments.

Comments

1

this will consolidate what you showed above:

function addChildOfType(type:Class):void{
    var mc:type = new type();
    stage.addChild(mc);
}

to use this just call:

addChildOfType(Clip1);
addChildOfType(Clip2);
addChildOfType(Clip3);

EDIT:

if your library is an externally loaded swf, then @M. Laing is correct in how to get them. If your library is your flash library in the same flash file, then this answer will fix you up.

1 Comment

you are welcome! happy coding.. oh and mark this answer as the one you wanted if you are good to go!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.