0

您好,我正在开发一个在棋盘式 SKtilemap 上进行的小游戏。当生成敌人时,我希望它们只出现在光砖上(以及只出现在地图的上半部分而不是玩家角色上)。为此,我使用 while 循环进行迭代,以尝试检查随机选择的行和列下的 tileGroup 类型。不幸的是,我收到一条错误消息:

表达式类型'(Int, Int) -> SKTileGroup?' 没有更多上下文是模棱两可的

当我尝试将此 tileGroup 检查移动到其他函数时,我没有收到此错误,但打印的 tileGroup 看起来像这样:SKTileGroup: 0x600003ce3b10> 而不是 tileGroup 名称。

谢谢!

func setupEnemies() {
    // sizes enemy
    enemy = SKSpriteNode(imageNamed: "enemy")
    let height = enemy.size.height
    let scaleFactor = grid.tileSize.height / height
    enemy.setScale(scaleFactor)

    let numberOfRows = UInt32(grid.numberOfRows)
    let numberofCols = UInt32(grid.numberOfColumns)

    // initializes enemy position
    var randomRow = Int(arc4random_uniform(numberOfRows))
    var randomCol = Int(arc4random_uniform(numberofCols))

    // checks to make sure enemy position is in the top half of the board, is not located on the player character, and is only on light tiles.
    while randomRow < (numberOfRows / 2) || (randomRow == grid.tileRowIndex(fromPosition: knight.position) && randomCol == grid.tileColumnIndex(fromPosition: knight.position)) || (grid.tileGroup(atColumn: randomCol, row: randomRow) == "dirtDark Tile") {
        randomRow = Int(arc4random_uniform(numberOfRows))
        randomCol = Int(arc4random_uniform(numberofCols))


    }


    // add enemy to board.
    enemy.position = grid.centerOfTile(atColumn: randomCol, row: randomRow)
    addChild(enemy)

}
4

1 回答 1

0

我自己想通了。非常简单的解决方案,只需要像这样请求磁贴的名称:

grid.tileGroup(atColumn: randomCol, row: randomRow)?.name == "DirDark Tile"
于 2019-09-22T15:25:36.777 回答