A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] setChildIndex question- i know there's a simple solution i'm not seeing

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    88

    resolved [RESOLVED] setChildIndex question- i know there's a simple solution i'm not seeing

    Hello,

    I'm back and working on teaching myself more AS3 to expand on the stuff i've learned already and the great advice you've given me in the past, in the down between projects.

    so here's what i'm trying to do. simple. making a small "game" as an exercise. I currently have 4 boxes (blue, red, green, purple) on the screen. Each an instance of box_mc, instance named "boxMC", "boxMC1", etc.

    You can drag each of them around. That works.

    My question is that when i click on a box, I would like to set the Z-order to the top most layer and could be dragged over, and let go, the others. I used the setChildIndex, but I can only apply it to just a particular instance...

    Code:
    setChildIndex(boxMC2, numChildren-1);
    ...but not dependent on which the box that is being clicked. This is what I'm currently trying, but with no luck:

    Code:
    function initDragger(mc:MovieClip):void
    {
    	mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    	mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    	setChildIndex(mc, numChildren -1);
    	//trace(numChildren);
    }
    
    function mouseDownHandler(event:MouseEvent):void
    {
    	event.currentTarget.startDrag();
    	
    }
    function mouseUpHandler(event:MouseEvent):void
    {
    	event.currentTarget.stopDrag();
    }
    
    // Set up drag
    initDragger(boxMC);
    initDragger(boxMC1);
    initDragger(boxMC2);
    initDragger(boxMC3);
    any help would be great! thank you very much in advance!
    `shields

  2. #2
    Señor Member Mavrisa's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    506
    function mouseDownHandler(event:MouseEvent):void
    {
    setChildIndex(event.currentTarget, numChildren-1);
    event.currentTarget.startDrag();
    }

    think that should solve your problem.
    Haikus are easy
    But sometimes they don't make sense
    Refrigerator

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    88
    Quote Originally Posted by Mavrisa View Post
    function mouseDownHandler(event:MouseEvent):void
    {
    setChildIndex(event.currentTarget, numChildren-1);
    event.currentTarget.startDrag();
    }

    think that should solve your problem.
    tried that, but get this error:
    Code:
    1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
    I've attached my file if it helps any. it's not fancy. just a learning file.
    Attached Files Attached Files
    Last edited by krashe1313; 07-01-2009 at 02:52 PM.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    55
    This is an extract of my code: (The attached fla works as well)

    PHP Code:
    // These items will get the drag functionality
    addDrag(box);

    private function 
    addDrag($mc:MovieClip):void
    {
        
    $mc.addEventListener(MouseEvent.MOUSE_DOWNdoDrag);
        
    $mc.addEventListener(MouseEvent.MOUSE_UPkillDrag);
    }

    private function 
    doDrag(e:MouseEvent):void
    {
        var 
    dragger e.currentTarget;
            
    dragger.startDrag(true);
        
    this.setChildIndex(dragger, (this.numChildren 1));
    }
    private function 
    killDrag(event:MouseEvent):void
    {
        var 
    dragger e.currentTarget;
           
    dragger.stopDrag();

    Attached Files Attached Files
    Last edited by Mpjraaij; 07-01-2009 at 03:07 PM. Reason: Attached file

  5. #5
    Member
    Join Date
    Jul 2009
    Posts
    88
    wow- excellent- thank you for the example and file. i really appreciate it! i'm looking it over right now!

    thanks!

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