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]);
                }
            }

        }

そのループが通過したら、すべてのボタンを削除する必要があります。しかし、そうではありません。いくつかの変数のトレースを開始したところ、2 番目のループで、100 個のオブジェクトすべてをループするのではなく、実際には同じ 10 個のオブジェクトを 10 回ループしていることに気付きました。* は最初の for ループ (イベントの前) の trace ステートメントを示し、* がないものは 2 番目の 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