A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: this and parent

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    79

    this and parent

    Hi,

    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...

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Where did you put that code? Presumably it's in an event listener. Where is that listener defined?

  3. #3
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,930
    In as3 you have to say something like:

    Code:
    MovieClip(this.parent.parent).gotoAndStop(2);
    Hope that helps!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  4. #4
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by 5TonsOfFlax View Post
    Where did you put that code? Presumably it's in an event listener. Where is that listener defined?
    the code there in time line... inside the movie clip "mc2"...

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    79
    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.....
    Attached Files Attached Files

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I can't open flas, so I can't open your file.

    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.

  7. #7
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by flashpipe1 View Post
    In as3 you have to say something like:

    Code:
    MovieClip(this.parent.parent).gotoAndStop(2);
    Hope that helps!
    This code is working well... Is this default for AS3, instead of "parent.parent.gotoAndStop(2)" in AS2?

  8. #8
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by 5TonsOfFlax View Post
    I can't open flas, so I can't open your file.

    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."...

  9. #9
    Senior Member
    Join Date
    May 2010
    Posts
    178
    ActionScript Code:
    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
    Last edited by poltuda; 07-15-2011 at 03:09 PM.

  10. #10
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by poltuda View Post
    ActionScript Code:
    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...?

  11. #11
    Senior Member
    Join Date
    May 2010
    Posts
    178
    ActionScript Code:
    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
    Last edited by poltuda; 07-15-2011 at 04:27 PM.

  12. #12
    Senior Member
    Join Date
    May 2010
    Posts
    178
    ActionScript Code:
    var stg=parent.getChildAt(0);
    var clip=stg.getChildAt(1)
    var mc1=stg.getChildAt(0)
    var mc2=mc1.getChildAt(0)

    mc2.getChildAt(0).addEventListener(MouseEvent.CLICK,clickHandler);
    function clickHandler(evt:MouseEvent):void{
        trace(stg)
        trace(clip)
        trace(mc1)
        trace(mc2)
        clip.gotoAndStop(2)
    }


    poltuda

  13. #13
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by poltuda View Post
    ActionScript Code:
    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()

  14. #14
    Senior Member
    Join Date
    May 2010
    Posts
    178
    The supplied index is out of bounds.

    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

  15. #15
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by poltuda View Post
    The supplied index is out of bounds.

    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....

  16. #16
    Member
    Join Date
    Feb 2010
    Posts
    79
    Quote Originally Posted by bhagavath View Post
    Thank you so much, i learned many things from here....
    And one more doubt,

    How the compiler recognized getChildAt(0), getChildAt(1), getChildAt(2)....?

    By history of creating movieclips?

    I don't know whether you understood are not... that what i am asking.....

  17. #17
    Senior Member
    Join Date
    May 2010
    Posts
    178
    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).

    Have a great fun.


    poltuda

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center