Flash: dynamically adding events code to instances possible?
I want to make a movieclip invisible initially but i dont want to set it manually within the properties in flash because i cant then see it on the scene.
I was hoping i could add some code like s开发者_如何学运维o:
MC Frame one.
this.onClipEvent(load)
{
this._alpha = 0;
}
but I cannot. How can i set the MC _alpha to 0 for all instances without adding it manually to each instance or setting it in the properties?
edit: or creating a class for it just to set the alpha.
If you want to do this by creating a subclass
in actionscript 2, here's a great step-by-step tutorial from Adobe.
http://www.adobe.com/devnet/flash/articles/mc_subclasses_v2_04.html
The tutorial instructs you to add an onEnterFrame event handler, but you can ignore that and simply add the following code to the constructor.
If your classname is Ball
then the code would look like this. (this is from step 4 in the tutorial).
dynamic class Ball extends MovieClip {
function Ball() {
this._alpha = 0;
}
}
Maybe there's something I didn't understood correctly, but you just have to write something like this on your first frame :
yourFirstMovieClip._alpha = 0;
yourSecondMovieClip._alpha = 0;
If your MovieClips names are numbered (mc0, mc1, mc2, mc3...), you can use a loop to set the _alpha property to every clips. Let say you have 5 clips (mc0 to mc4) :
for( var i:Number = 0 ; i < 5 ; i++ )
{
this["mc"+i]._alpha = 0;
}
If not, you can store every clips in an Array and them loop through it :
var clips:Array = [mcFirst, mcSecond, mcThird, mcFourth];
for( var i:Number = 0 ; i < clips.length ; i++ )
{
clips[i]._alpha = 0;
}
I am now using this code, which does what i want. But i hate it.
var once:Boolean;
if (once == null) {
once = true;
this._alpha = 0;
}
Another solution may be to put this line on the first frame of your MovieClip :
_alpha = 0;
Start your animation on the second frame, and add the following line on your last one :
gotoAndPlay( 2 );
So the code on the first frame is only executed one time.
精彩评论