lundi 12 décembre 2016

How can I set a public variable using getChildIndex in as3?

I am new to stackoverflow, and this is my first question.

I recently started programming with as3, so I don't know too much about as3 yet, but...

I was coding a very simple program that lets you move around a circle, with also a rectangle in the stage. I wanted to make the circle get in front of the rectangle while you are dragging it, but when you released the mouse, the circle would be sent back.

The thing is, that I don't know how to set a public variable using the getChildIndex method. I don't really care about the rest of the code. I'm mainly interested in how can I make the getChildIndex method work with a public variable.

Here is my code:

package code

    import flash.display.MovieClip;
    import flash.display.Sprite;

    public class Main extends MovieClip
        public var myCircleIndex:int = getChildIndex(myCircle);

        public function Main()
            myCircle.addEventListener(MouseEvent.MOUSE_DOWN, mouseClicking);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

        public function mouseClicking(e:MouseEvent): void
            setChildIndex(myCircle, numChildren-1);

        public function mouseReleased(e:MouseEvent): void
            setChildIndex(myCircle, myCircleIndex);

I'm using an instance ("myCircle") that I created directly in the stage as a movie clip.

The problem is in the public var I set at the beginning, it doesn't let me get the child index of myCircle, but if I put the same line inside a function, it works.
I know I could directly put the index number of myCircle in the last line (and erasing the public var myCircleIndex), but I figured out that there would be a way of using the getChildIndex for a public var in a class.

I wish you could tell me how to use the getChildIndex in a public variable inside a class, don't need to worry about the rest.

Thank you for reading,

Aucun commentaire:

Enregistrer un commentaire