for循环不会多次循环执行JS代码

时间:2018-09-27 18:19:51

标签: javascript arrays object for-loop

我正在制作一艘战舰游戏..一段代码需要在敌人的战场上放置随机的战舰。 (5艘战舰,所以我现在需要调用该函数5次)。但是第一个for循环却什么也不做。为什么,我怎么能解决这个问题?预先感谢!

var ships = [
        {
            shipClass: "cruiser",
            shipLength: 3
        },
        {
            shipClass: "battleship",
            shipLength: 4
        },
        {
            shipClass: "submarine",
            shipLength: 3
        },
        {
            shipClass: "destroyer",
            shipLength: 2
        },
        {
            shipClass: "carrier",
            shipLength: 5
        },
    ]
    var currentShipIndex = 0

    function placeEnemyBoat() {
        var currentShipSize = ships[currentShipIndex].shipLength
        var randomInt = Math.floor(Math.random() * numRows * numRows)

        for (var i = 0; i < ships.length; i++){
            if (Math.random() < 0.5) {
                if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "vertical")) {
                    for (var idx = randomInt; idx < randomInt + currentShipSize; idx++) {
                        enemySquares[idx].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                    }
                    currentShipIndex += 1
                }
            }else {
                if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "horizontal")) {
                    for (var index = randomInt; index < randomInt + (currentShipSize * numRows); index += numRows) {
                        enemySquares[index].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                    }
                    currentShipIndex += 1
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我假设您在代码的其他位置定义了numRows,draw,nearnCtx,敌人Squares等。没有更多的代码,这很难说。

为了避免这些未声明的变量,我在for循环和大多数for循环的主体上方注释了一行。我添加了一个简单的console.log语句。

var ships = [
    {
        shipClass: "cruiser",
        shipLength: 3
    },
    {
        shipClass: "battleship",
        shipLength: 4
    },
    {
        shipClass: "submarine",
        shipLength: 3
    },
    {
        shipClass: "destroyer",
        shipLength: 2
    },
    {
        shipClass: "carrier",
        shipLength: 5
    },
]
var currentShipIndex = 0

function placeEnemyBoat() {
    var currentShipSize = ships[currentShipIndex].shipLength
    //var randomInt = Math.floor(Math.random() * numRows * numRows)

    for (var i = 0; i < ships.length; i++){
        console.log(i);
        /*
        if (Math.random() < 0.5) {
            if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "vertical")) {
                for (var idx = randomInt; idx < randomInt + currentShipSize; idx++) {
                    enemySquares[idx].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                }
                currentShipIndex += 1
            }
        }else {
            if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "horizontal")) {
                for (var index = randomInt; index < randomInt + (currentShipSize * numRows); index += numRows) {
                    enemySquares[index].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                }
                currentShipIndex += 1
            }
        }
        */
    }
}

placeEnemyBoat();

但是您所拥有的似乎确实是通过for循环进行了五次迭代。