从skSpriteNode位置获取tileGroup

时间:2019-09-21 18:04:18

标签: swift sktilemapnode

您好,我正在研究一款在棋盘风格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)

}

1 个答案:

答案 0 :(得分:0)

我自己弄清楚了。非常简单的解决方案,只需要像下面这样请求图块的名称即可:

grid.tileGroup(atColumn: randomCol, row: randomRow)?.name == "DirDark Tile"