Little confused with "this and parent" in flash as3. I have movie clip named "mc1" in the main time line, first frame, and movie clip "mc2" inside the movieclip mc1. Button "but" is there inside the movieclip "mc2". Once i clicked the button, have to go to the second frame in main time line.... i tried , is not working...
parent.parent.gotoAndStop(2);
The above script is not working.. pls help me guys...
Here i am attaching sample file.... pls go through it.. in the main time line, the blue box is in first frame, the red box is in second frame. Once i clicked the button inside the movieclip "mc2", red box shouls be visible... pls give me the fair code else explain me.....
You do need to cast to MovieClip to call a MovieClip method. This is because parent is typed as DisplayObjectContainer. DisplayObjectContainer does not define a gotoAndStop method.
Use flashpipe's code.
And instead of saying "it doesn't work" tell us how it doesn't work. Read your error messages, and post them if you are asking about an error. They have meaning, you know.
You do need to cast to MovieClip to call a MovieClip method. This is because parent is typed as DisplayObjectContainer. DisplayObjectContainer does not define a gotoAndStop method.
Use flashpipe's code.
And instead of saying "it doesn't work" tell us how it doesn't work. Read your error messages, and post them if you are asking about an error. They have meaning, you know.
Ya, flashpipe's code is working well,...
actally the error was "Symbol 'Symbol 2', Layer 'Layer 2', Frame 1, Line 5 1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.displayisplayObjectContainer."...
var stg=root; btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ stg.gotoAndStop(2); }
Stop writing code inside in MovieClip. Make it in 1st frame of main timeline or in Document Class. For your case make a MovieClip and target it to go to frame 2
ActionScript Code:
var stg=root; mc1.mc2.btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ stg.gotoAndStop(2); }
var stg=root; btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ stg.gotoAndStop(2); }
Stop writing code inside in MovieClip. Make it in 1st frame of main timeline or in Document Class. For your case make a MovieClip and target it to go to frame 2
ActionScript Code:
var stg=root; mc1.mc2.btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ stg.gotoAndStop(2); }
poltuda
For root it is working, but for parent it is not working ... what script u ill write for parent...?
var stg=parent.getChildAt(0); var clip=stg.getChildAt(1) mc1.mc2.btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ trace(stg) trace(clip) clip.gotoAndStop(2) }
Here, the code is targeting another mc which is in the main timeline to go to and stop at frame 2.
var stg=parent.getChildAt(0); var clip=stg.getChildAt(1) mc1.mc2.btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ trace(stg) trace(clip) clip.gotoAndStop(2) }
Here, the code is targeting another mc which is in the main timeline to go to and stop at frame 2.
poltuda
I got this error,
RangeError: Error #2006: The supplied index is out of bounds.
at flash.display:isplayObjectContainer/getChildAt()
at this_parent_fla::MainTimeline/frame1()
This is what the index number you are giving to target the child object.
getChildAt(0);
getChildAt(1);
if you have the child object out there, you never get the index bounds error. So what you must be sure is of the presence of your target. If you want to make it clear yourself, just create a new fla and write this code bellow in the 1st frame:
var stg=parent;
trace(stg)
var mnTimeline=stg.getChildAt(0);
trace(mnTimeline)
trace(mnTimeline.name)
So the root1 is the child object of the Stage object. Now if you put an instance into the stage it should be the child of mnTimeline. So if you do the following, the output with show the target and the instance name.
var movieClip1=mnTimeline.getChildAt(0);
trace(movieClip1)
trace(movieClip1.name)
This is our mc1 MovieClip, inside of which mc2 and inside of mc2 the btn SimpleButton is situated.
I create another MovieClip in the mnTimeline, which is now the child object of index 1 of the mnTimeline.
var movieClip1=mnTimeline.getChildAt(0);
trace(movieClip1)
trace(movieClip1.name)
var movieClip2=mnTimeline.getChildAt(1);
trace(movieClip2)
trace(movieClip2.name)
This MovieClip has two frames inside and a stop(); action in the 1st frame Action. The second frame contains different visual from the first frame.
Now if this is what you did, there must not be any error of my code instead you missed to give the instance name.
Other dynamic way is not to give any instance name and just make sure the your btn mc is on index 0.
ActionScript Code:
var stg=parent.getChildAt(0); var mc1=stg.getChildAt(0); var mc2=mc1.getChildAt(0); var btn=mc2.getChildAt(0);
var clip=stg.getChildAt(1) btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ trace(stg) trace(clip) clip.gotoAndStop(2) }
This is what the index number you are giving to target the child object.
getChildAt(0);
getChildAt(1);
if you have the child object out there, you never get the index bounds error. So what you must be sure is of the presence of your target. If you want to make it clear yourself, just create a new fla and write this code bellow in the 1st frame:
var stg=parent;
trace(stg)
var mnTimeline=stg.getChildAt(0);
trace(mnTimeline)
trace(mnTimeline.name)
So the root1 is the child object of the Stage object. Now if you put an instance into the stage it should be the child of mnTimeline. So if you do the following, the output with show the target and the instance name.
var movieClip1=mnTimeline.getChildAt(0);
trace(movieClip1)
trace(movieClip1.name)
This is our mc1 MovieClip, inside of which mc2 and inside of mc2 the btn SimpleButton is situated.
I create another MovieClip in the mnTimeline, which is now the child object of index 1 of the mnTimeline.
var movieClip1=mnTimeline.getChildAt(0);
trace(movieClip1)
trace(movieClip1.name)
var movieClip2=mnTimeline.getChildAt(1);
trace(movieClip2)
trace(movieClip2.name)
This MovieClip has two frames inside and a stop(); action in the 1st frame Action. The second frame contains different visual from the first frame.
Now if this is what you did, there must not be any error of my code instead you missed to give the instance name.
Other dynamic way is not to give any instance name and just make sure the your btn mc is on index 0.
ActionScript Code:
var stg=parent.getChildAt(0); var mc1=stg.getChildAt(0); var mc2=mc1.getChildAt(0); var btn=mc2.getChildAt(0);
var clip=stg.getChildAt(1) btn.addEventListener(MouseEvent.CLICK,clickHandler); function clickHandler(evt:MouseEvent):void{ trace(stg) trace(clip) clip.gotoAndStop(2) }
Hope it make sense to you.
poltuda
Thank you so much, i learned many things from here....
Not by the history of creating MovieClip but the history of creating instance of any DisplayObject on stage.
You can create as many object you want in the Library. Flash doesn't have the index until the instance of those object added as a child on stage, dynamically or manually. Flash will count index if you create graphics on stage which is not in the Library, and that will be on index 0 (Zero).