从 skSpriteNode 位置获取 tileGroup
Getting tileGroup from skSpriteNode position
您好,我正在开发一款发生在棋盘式 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)
}
我自己想出来了。非常简单的解决方案,只需要像这样请求磁贴的名称:
grid.tileGroup(atColumn: randomCol, row: randomRow)?.name == "DirDark Tile"
您好,我正在开发一款发生在棋盘式 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)
}
我自己想出来了。非常简单的解决方案,只需要像这样请求磁贴的名称:
grid.tileGroup(atColumn: randomCol, row: randomRow)?.name == "DirDark Tile"