简体   繁体   中英

Using a MovieClip as a parameter in AS3

I'm having problems attaching MovieClips to different instances of a class. I'm kinda new to ActionScript 3, honestly, so this question might be a bit noobish. I did the research, though, but haven't found the kind of answer that I expected.

function AddNewElement(clip:MovieClip, array:Array, name:String, firstValue:int, secondValue:int):Element

As you may be able to guess, this is the function I made to create instances of a class in a dynamic way and add them to an array. I'm having problems with the very first parameter, though. How do I pass a MovieClip from my library to this function?

I saw a lot of answers to problems similar to this one stating that each MovieClip should be a class on its own, but since I have like forty MCs and I want to use them all for more or less the same thing I feel that it kills the purpose of classes, really.

How should I approach this?

First you need to give your library symbols unique class names from the linkage section while exporting or later from the "properties" option. You will see this input when you check the "Export for ActionScript" option there. Then you will need to instantiate your library symbol (with the new keyword) and cast it to MovieClip to pass to this function. So

  • AddNewElement(new LibrarySymbolClass() as MovieClip,[],'etc',0,0);
  • AddNewElement(MovieClip(new LibrarySymbolClass()),[],'etc',0,0);

will both let you do what you want.

However, since not all your library elements need to extend the MovieClip class, you would better pick DisplayObject instead of MovieClip . So a better version of your function would be

import flash.display.DisplayObject;

function AddNewElement(clip:DisplayObject, ...):* {
    // some code here
    return clip;
}
var clip:LibrarySymbolClass = AddNewElement(new LibrarySymbolClass() as DisplayObject,[],'etc',0,0);
trace(clip);

Using asterisk in the return value type will let it return the object with its right type (as [object LibrarySymbolClass] in this example).

When you create a MovieClip in Flash it gives you certain options, one of those options is for Flash to create a class for that MovieClip. That being said if you have that option applied to all forty movie clips then you create something like a master movie clip class and have each movie clip class extend the master movie clip class. The only thing is that you would have to create a .as file for each of your 40 movie clips and add extends MasterMovieClip to the class declaration. For example:

public class MasterMovieClip extends MovieClip {
    // All of the variables and methods pertaining to each movie clip go here
}


Then each individual movie clip would resemble this class.

public class IndividualMovieClip_1 extends MasterMovieClip {
    // Just include a constructor, even though you don't have to
}


Now all of your individual movie clips will have the same methods and variables, as long as said methods and variables are public, not private.

With this method you would have to create all 40 classes, however there might be a way in Flash when creating a new movie clip to set which class the movie clip extends and then you wouldn't have to create 40 different classes.



Update:

I re-read your question and thought of something else, that option I talked about in the first sentence, the one about Flash giving the option to create a class. Well if a class is not given then Flash will dynamically create a class at run-time. I think when it dynamically creates a class it does not maintain the same name as the library movie clip, so when your trying to pass the static name of your movie clip to your function, it has no clue what your talking about and throws a runtime error.

Why not to create the movie clips on runtime, aka. creating them in the runtime execution context then instantiating each of them at the moment when you invoke the class. If each of the MC's is different, then either you create a MC class for each of them, giving them a name which end up in a ascending number, then using a for loop you put them in an array like so:

var mc_num:int = 40 // the number of MovieClips
var arr:Array = new Array();

for (var i:int=0; i < mc_num; i++) {
    arr.push("myMovieClip" + String(i)); 
}

..then making reference to each of them by using the array index. I skip the part where you associate the images to MovieClips.

After that you invoke the desired MC as below:

var mc_1:MovieClip = arr[1] as MovieClip;
stage.addChild(mc_1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM