1

我正在玩磁贴编辑器,我用它创建了一个简单的、有点大的地图,如下所示。瓦片大小为 64 x 64。地图大小为 58 x 32。

在此处输入图像描述

我想知道游戏角色的当前位置。让我称它为播放器(as SKNode)。我想知道这个家伙的当前位置,以便我可以找出它的任何相邻瓷砖是否不是障碍物。以下代码行给了我 (7, -3)。

background = self.childNode(withName: "World") as! SKTileMapNode
let position = background.convert(player.position, from: player)
let column = background.tileColumnIndex(fromPosition: position)
let row = background.tileRowIndex(fromPosition: position)
print(column, row)

,这表明这些坐标指向相机的中心,由红色圆圈表示。下面给了我 (10, 2)。

let position = background.convert(player.position, from: self)
let column = background.tileColumnIndex(fromPosition: position)
let row = background.tileRowIndex(fromPosition: position)

那是左下角的位置。

现在,如果我想将游戏角色向上移动 1 格,请编写...

let position = background.convert(player.position, from: player)
let column = background.tileColumnIndex(fromPosition: position)
let row = background.tileRowIndex(fromPosition: position)
let destination = background.centerOfTile(atColumn: column, row: row + 1)
let action = SKAction.move(to: destination, duration: 1)
player.run(action)

,游戏角色将在行动中失踪,从地图上消失。那么如何将节点移动一格呢?

4

1 回答 1

0

也许试试这个:

    let currentPlayerPosition = background.convert(player.position, from: player)
    let column = background.tileColumnIndex(fromPosition: currentPlayerPosition)
    let row = background.tileRowIndex(fromPosition: currentPlayerPosition)
    let destination = background.centerOfTile(atColumn: column, row: row + 1)

    // the new line I added
    let newPlayerPosition = background.convert(destination, to: player)

    let action = SKAction.move(to: newPlayerPosition, duration: 1)
    player.run(action)
于 2017-04-14T17:51:35.917 回答