A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: setChildIndex help

  1. #1
    Junior Member
    Join Date
    Nov 2002
    Posts
    16

    setChildIndex help

    I have button inside a MovieClip called bigDraw that has this code

    but.addEventListener(MouseEvent.ROLL_OVER, move1);

    function move1(evt:MouseEvent):void
    {
    setChildIndex(MovieClip(this.root).bigDraw,-1);
    MovieClip(parent).midDraw.but1.mouseEnabled = false;
    gotoAndPlay("on");
    }

    on the roll over I want bigDraw to become to the top most element in the flash movie but I get error

    "ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.at flash.display:isplayObjectContainer/setChildIndex()"

    Thanks for help.

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    Try this.

    setChildIndex(MovieClip(evt.target), numChildren-1);

  3. #3
    Junior Member
    Join Date
    Nov 2002
    Posts
    16
    I tried it but I get this error.

    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::SimpleButton@55229ec9 to flash.display.MovieClip.
    at big_O_tires_300xv1_fla::bigdraw_17/move1()

  4. #4
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    Try this

    var target = evt.target

    setChildIndex(target , numChildren-1);

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You must call setChildIndex on the parent of the instance you are trying to move around.

    Since you are trying to get bigDraw to move to the top (of just its parent, or everything?) then you can just use addChild instead of setChildIndex. addChild will automatically remove the instance from whatever parent it had, and add it to the top of the children of the parent you're adding it to. Even if the old parent and the new parent are the same.

    Since this code is apparently in bigDraw, you can do this:
    Code:
    function move1(evt:MouseEvent):void{
      this.parent.addChild(this);
      MovieClip(parent).midDraw.but1.mouseEnabled = false;
      gotoAndPlay("on");
    }
    But that second line is bad practice too. Generally you should not reach up and down the display list like that. Instead, listen for the event at the level where the commonality is (in this case, the parent). You may dispatch a new event in bigDraw and listen for that if you do not want to have the parent need to know about the structure of bigDraw or midDraw. I'd create a "disable" method in midDraw and call that from the method in the parent that listens for whatever new event you dispatch from move1.

    To be clear, it should "work" with the bad practice way, it's just tightly coupled and any changes to one will imply corresponding changes to the other instances involved.

  6. #6
    Junior Member
    Join Date
    Nov 2002
    Posts
    16
    Thanks a lot for the tips, do you know a good tutorial to learn this stuff with good practice from 0 up? I tried lynda but dont seem to get it right with their tutorials.

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