swiftsktilemapnode

Getting tileGroup from skSpriteNode position


Hello I am working on a small game that takes place on a checkerboard style SKtilemap. When spawning enemies, I would like them only to appear on the light tiles (as well as only in the top half of the map and not on the player character). To do this I am iterating with a while loop to try to check what kind of tileGroup is under the randomly selected row and column. Unfortunately I am getting an error that reads:

Expression type '(Int, Int) -> SKTileGroup?' is ambiguous without more context

When I try to move this tileGroup check to a different function I don't get this error, but the printed tileGroup looks like this: SKTileGroup: 0x600003ce3b10> instead of the tileGroup name.

Thank you!

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)

}

Solution

  • I figured it out myself. Very simple solution, just needed to request the name of the tile like this:

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