开发者

Previous array MC position

I'm basically trying to create a tree diagram. I've created the 1st level and I wanted to put it into an array so the next level would be able to get the properties from the previous level/array.

But now it seems like I cannot push the circles with its own unique"name" into circlesNewArray. what's the problem here?

I'm still not sure if the way i construct my code for dynamic intance/data/mc with max 2 separation is correct or not, as I think it involves 2 to 3 array to keep it gouped among same level when data loads, and regroup again when doing level separation.

If you have any better idea it would be greatly appreciated too. thanx!

private function dataLevel():void {

        rootLevel=new Array();
        rootLevel.push(20,200);

        newArray=new Array();

        var no1:Number=1;
        var no2:Number=2;

        if (rootLevel is开发者_如何学C Array==true) {
            createBranch(rootLevel);


            for (var j:Number=0; j<rootLevel.length; j++) {
                newArray[j]=new Array();
                newArray[j].push(j);
                createBranch(newArray[j]);
            }
        }
    }


    //-------------------------------------------------------------------


    private function createBranch(runningObj:Object):void {

        circlesNewArray = new Array();
        var i2=i-1;

        for (var i:Number=0; i<runningObj.length; i++) {

            circles = new MovieClip();
            var empty=null;
            circles.graphics.beginFill(0xFF2222);
            circles.graphics.drawCircle(empty,empty,10);
            circles.graphics.endFill();

            //Passing 2 cirls in an Array
            circlesNewArray[i]=new Array();
            circles.name="circles"+i;

            circlesNewArray[i].push(circles[i]);

            if (runningObj==rootLevel) {
                circles.x=trunks.x - (Math.floor(Math.random()*100)-50);
                circles.y=trunks.y - (Math.floor(Math.random()*100));
            } else if (runningObj!=rootLevel) {
                circles.x=circlesNewArray[i2]-(Math.floor(Math.random()*100)-50);
                circles.y=circlesNewArray[i2]-(Math.floor(Math.random()*100));
            }

            //trace(circles.x, circles.y);

            circles.buttonMode=true;
            circles.addEventListener(MouseEvent.CLICK, clickTarget);

            addChild(circles);
        }
    }


You are accessing the variable i before declaring it. Hence i2 will be assigned NaN. Unfortunately, the compiler ignores this kind of errors. In flash, all the local variables of a method, irrespective of where you declare them in the code, are declared at the beginning of the method but they are assigned the values only once the code reaches the respective line of execution.

var i2=i-1;//value of i and hence that of i2 is NaN.
for (var i:Number=0; i<runningObj.length; i++)

Since i2 is NaN the call to access circlesNewArray[i2] fails. If you want i2 to have the value (i - 1) call i2 = i - 1; from within the for loop.

Btw, what are you trying to do with this line of code?

circles.x=circlesNewArray[i2]-(Math.floor(Math.random()*100)-50);

You are pushing arrays into circlesNewArray and then accessing them as Numbers?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜