我正在使用磁贴编辑器,我已经创建了一个简单的,有点大的地图,如下所示。磁贴大小为64 x 64.地图大小为58 x 32。
我想知道游戏角色的当前位置。让我称它为播放器(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)
,游戏角色会在行动中失踪,从地图上消失。那么如何将节点移动一个图块呢?
答案 0 :(得分: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)