2D循环通过相同的10个值而不是100个

时间:2013-06-11 15:24:07

标签: arrays actionscript-3 loops multidimensional-array

我不太确定如何解决这个问题。基本上,我有一个循环100个按钮(10行10)的算法,并根据随机的起点和终点分配一个数值。单击时,按钮应进入游戏的下一部分,并将自己从地图上移开。为此,我使用了以下代码:

public function createMap()
    {
        var startPointX:int = int(10 * Math.random());  //creates random values that set the x and y values of the start and end points
        var startPointY:int = int(10 * Math.random());
        var endPointX:int = int(10 * Math.random());
        var endPointY:int = int(10 * Math.random());

        var spaceDiffX:int = startPointX - endPointX;   //the difference in X values
        var spaceDiffY:int = startPointY - endPointY;   //the difference in Y values

        //Creation loop - loops through all of the buttons to create them
        for (i = 0; i <= 9; i++)
        {
            mapY[i] = mapX; //Adds the rows into each column

            for (j = 0; j <= 9; j++)
            {
                mapY[i][j] = new LocationButton();  //Creates a new button
                mapY[i][j].x = 6 * 20 * i;  //Sets X position
                mapY[i][j].y = 6 * 10 * j;  //Sets Y position

                //places start point
                if (i == startPointX && j == startPointY)   //checks if this point on the grid is equal to the random start point
                {
                    mapY[i][j].buttonType = 1;  //sets it to the start point
                }
                //places end point
                if (i == endPointX && j == endPointY)   //checks if this point is equal to the random end point
                {
                    mapY[i][j].buttonType = 2;  //sets it to the end point
                }
                //finds path on x axis
                if (i != startPointX && j == startPointY)   //checks if this point is on the same Y value as the start point
                {
                    if (spaceDiffX >= 0)    //checks to see if the start point is on the left or right of the end point, left is negative, right is positive
                    {
                        if (i < startPointX && i >= endPointX)  //checks if i is between the start X and end X
                        {
                            mapY[i][j].buttonType = 3;  //sets it to a "shortest path point"
                        }

                    }
                    else
                    {
                        if (i > startPointX && i <= endPointX)  //checks to see if i is between the start X and end X
                        {
                            mapY[i][j].buttonType = 3;  //sets it to a "shortest path point"
                        }

                    }

                }

                if (j != startPointY && i == endPointX) //checks if this point is on the same X value as the end point
                {
                    if (spaceDiffY >= 0)    //checks to see if the start point is over or under the end point, above is positive, below is negative
                    {
                        if (j < startPointY && j >= endPointY)  //checks if j is between the end Y and the start Y
                        {
                            mapY[i][j].buttonType = 3;  //sets it to a "shortest path point"
                        }

                    }
                    else
                    {
                        if (j > startPointY && j <= endPointY)
                        {
                            mapY[i][j].buttonType = 3;  //sets it to a "shortest path point"
                        }

                    }

                }

                if (mapY[i][j].buttonType != 0) //checks whether or not the button should be added
                {
                    mapY[i][j].addEventListener(MouseEvent.CLICK, createBattleGUI); //adds event listener
                    stage.addChild(mapY[i][j]); //adds it to stage
                }

            }

        }

    }

然而,问题在于eventListener之后的代码是如何起作用的。它调用的函数从这个循环开始:

for (i = 0; i <= 9; i++)
        {
            for (j = 0; j <= 9; j++)
            {
                if (mapY[i][j].buttonType != 0)
                {
                    stage.removeChild(mapY[i][j]);
                }
            }

        }

一旦该循环通过,它应该 - 删除所有按钮。但事实并非如此。我开始跟踪一些变量,并注意到在第二个循环中,它实际上循环遍历相同的10个对象10次,而不是循环遍历所有100个对象。 *表示第一个for循环中的trace语句(在事件之前),没有它的是在第二个for循环中跟踪的按钮:

name - buttonType - i - j
*instance33 1 1 6
*instance53 3 2 6
*instance73 3 3 6
*instance93 3 4 6
*instance113 3 5 6
*instance133 3 6 6
*instance153 3 7 6
*instance173 3 8 6
instance181 0 0 0
instance183 0 0 1
instance185 0 0 2
instance187 0 0 3
instance189 0 0 4
instance191 0 0 5
instance193 0 0 6
instance195 0 0 7
instance197 0 0 8
instance199 0 0 9
instance181 0 1 0
instance183 0 1 1
instance185 0 1 2
instance187 0 1 3
instance189 0 1 4
instance191 0 1 5
instance193 0 1 6
instance195 0 1 7
instance197 0 1 8
instance199 0 1 9

我意识到这是一个很长的问题,但我无法想象我的生活。谢谢你的帮助。

0 个答案:

没有答案