1
votes

I found this piece of code which apparently sets the 'z-index' of a child object, however I have a movieclip on the stage and a sprite which is being generated from the actions class. I understand they have to be within the same parent? but have no idea how to do this.

parent.setChildIndex(childObject, i)

My pie chart sprite is called into being with this function...

        this.graphics.clear();
        this.graphics.lineStyle(3, 0xFFFFFF);
        this.graphics.beginFill(0xFF0000, 0.5);
        this.drawSegment(this, stage.stageWidth/2, stage.stageHeight/2, piesize, wedge1start, wedge1end);
        this.drawSegment(this, stage.stageWidth/2, stage.stageHeight/2, piesize, wedge1end, wedge2end);
        this.drawSegment(this, stage.stageWidth/2, stage.stageHeight/2, piesize, wedge2end, wedge3end);
        this.drawSegment(this, stage.stageWidth/2, stage.stageHeight/2, piesize, wedge3end, wedge4end);

        this.graphics.endFill();

public function drawSegment(target:Sprite, x:Number, y:Number, r:Number, aStart:Number, aEnd:Number, step:Number = 1):void {
            // More efficient to work in radians
            var degreesPerRadian:Number = Math.PI / 180;
            aStart *= degreesPerRadian;
            aEnd *= degreesPerRadian;
            step *= degreesPerRadian;

            // Draw the segment
            target.graphics.moveTo(x, y);
            for (var theta:Number = aStart; theta < aEnd; theta += Math.min(step, aEnd - theta)) {
                target.graphics.lineTo(x + r * Math.cos(theta), y + r * Math.sin(theta));
            }
            target.graphics.lineTo(x + r * Math.cos(aEnd), y + r * Math.sin(aEnd));
            target.graphics.lineTo(x, y);
    }

currently I just plonked my movieclip on the stage but I'm going to guess I need to call the movieclip from the actions class as well. I think I can manage that but can't figure out how to first create a parent and then call those 2 children into it.

Thanks for any help

1

1 Answers

2
votes

You need to wrap those two graphics into two separate Sprite objects. Those two objects can be added to stage (or this in your case, I don't know what is it). So you will have a parent object with two children objects. Then you can swap them.

Working with graphics disables your option to swap them. It's a single object in which you are drawing.

So you just need to wrap those graphics into another child:

var drawings:Sprite = new Sprite();
drawings.graphics.clear(); // instead of this.graphics.clear()
drawings.graphics.lineStyle(3, 0xFFFFFF);
this.drawSegment(drawings, stage.stageWidth/2 ...) // pass DRAWINGS instead of this

this.addChild(drawings);

This way you have wrapped all those graphics and added them to this. Now you can use:

this.swapChildren(drawings, otherChild); // swaps two objects
this.setChildIndex(drawings, 5); // sets index for drawings only