Copy link to clipboard
Copied
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!
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;
}
Copy link to clipboard
Copied
Hi,
Can you paste your code here.
Regards,
Kartik.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
Sorry am not finding any difference it is showing the same output
Copy link to clipboard
Copied
What kind of output you want?
As i have seen your code you are doing it correct apart from the line 1 in for loop which i have corrected.
Regards,
Kartik.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
Hi,
This program is not working, when i trace e.currentTarget.name am getting the output as mc, can you please give me another solution.I think problem with for loop
Because when i press any of the mc the out put is coming as mc4
Copy link to clipboard
Copied
Can you please change this line myMovieClip.mc.addEventListener(MouseEvent.MOUSE_DOWN,showans); to myMovieClip.addEventListener(MouseEvent.MOUSE_DOWN,showans); and try again.
Copy link to clipboard
Copied
Sorry it is not working
Copy link to clipboard
Copied
Please explain the problem clearly. I have some doubts, What do you mean by mc? Whether it is myMovieClip or something else. And different action means for each mc you want to create a seperate action right?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Thanks Its working fine
Copy link to clipboard
Copied
You are welcome.
Please mark at the answer as Correct Answer.