0

我不太确定如何解决这个问题。基本上,我有一个算法循环遍历 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 循环(事件之前)中的跟踪语句,没有它的表示在第二个 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

我意识到这是一个有点长的问题,但我无法终生解决这个问题。谢谢你的帮助。

4

0 回答 0