hi,
I was just about to ask this question, glad this has been answered. However, in my project I need to add multiple instance of the class from library dynamically, I know how to do it in AS2 way, can anyone elighten me with AS3 way, specifically with this new addchild(movieclip) function?
thanks!
import flash.display.MovieClip;
for (var i:int=0; i<12; i++) {
// accessing the mc from libray here qmc is linkage name
myMovieClip = new qmc();
myMovieClip.x=stage.stageWidth/10;
myMovieClip.y=stage.stageHeight/3;
addChild(myMovieClip);
}
i think all 12 mc are loading in same place, i want to display the mc one by one
You can use it in following way :-
for (var i:int=0; i<12; i++) {
var myMovieClip:qmc = new qmc();// accessing the mc from libray here qmc is linkage name
myMovieClip.x=stage.stageWidth/10;//assigning x property
myMovieClip.y=stage.stageHeight/3;//assigning y property
addChild(myMovieClip);//adding to stage
}
hi i got the solution for my problem. it is worlking fine.
But i got another problem i don't know how to control the each mc.I want to perform different action with those mc.
for (var i:int=1; i<(len+1); i++) {
myMovieClip = new qmc();
if (i==1) {
var yval:int=stage.stageHeight/3;
} else {
yval=yval+myMovieClip.height+5;
}
myMovieClip.x=stage.stageWidth/10;
myMovieClip.y=yval+10;
addChild(myMovieClip);
myMovieClip.qtext.text=i+". "+ques_arr[i-1];
myMovieClip.atext.autoSize="center"
myMovieClip.atext.text=ans_arr[i-1];
myMovieClip.atext.visible=false;
// here is the problem
myMovieClip.mc.addEventListener(MouseEvent.MOUSE_DOWN,showans);
}
Hello,
Please try the below mentioned code :-
for (var i:int=1; i<(len+1); i++) {
myMovieClip = new qmc();
if (i==1) {
var yval:int=stage.stageHeight/3;
} else {
yval=yval+myMovieClip.height+5;
}
myMovieClip.x=stage.stageWidth/10;
myMovieClip.y=yval+10;
addChild(myMovieClip);
myMovieClip.qtext.text=i+". "+ques_arr[i-1];
myMovieClip.atext.autoSize="center"
myMovieClip.atext.text=ans_arr[i-1];
myMovieClip.atext.visible=false;
myMovieClip.name = "mc"+i // adding name to movieclip
// here is the problem
myMovieClip.mc.addEventListener(MouseEvent.MOUSE_DOWN,showans);
}
function showans(e:MouseEvent):void
{
trace(e.currentTarget.name)//here you will get the name of movieclip you have defined above here you can put a condition check. output will be like "mc1" "mc2" ...
//here you can write your code to handle different movieclip events
}
mc is instance of name one button it is there inside the myMovieClip.
For the my myMovieClip when i gave the different name like this myMovieClip.name = "mc"+i
am getting the myMovieClip name as mc1 ,mc2,mc3,mc4
I want to perform one action when i press mc1.mc the myMovieClip.atext should be visible true like wise for mc2..
But it is not happening
First check whether you gave the instance name for the button as "mc" in your myMovieClip.
Then write your code same
myMovieClip.mc.addEventListener(MouseEvent.MOUSE_DOWN,showans); // You can write CLICK event also...
In showans, You have to write like this.
function showans(e:MouseEvent):void
{
e.currentTarget.parent.atext.visible = true;
}
North America
Europe, Middle East and Africa
Asia Pacific