我想创建一个场景,我有按钮可以在 SKTileMapNode 内将光标从一个瓷砖移动到另一个瓷砖。.sks
我在 Spritekit 场景编辑器的外部文件中创建了一个 10x8 SKTileMapNode 。要将光标位置设置在与磁贴完全相同的位置,我想注册磁贴的特定列和行索引,然后找出他的位置。使用函数numberOfColumns
并tileRowIndex
给我比numberOfRows
and更高的值tileRowIndex
,它们是 10 和 8。由于这个问题,我的光标显示在错误的位置。它甚至没有在任何瓷砖位置上对齐。
class Spielfeld: SKScene {
var x: Int = 0 // Anzahl Felder in x-Richtung
var y: Int = 0 // Anzahl Felder in y-Richtung
var tilemap: SKTileMapNode = SKTileMapNode()
var up: SKSpriteNode = SKSpriteNode()
var down: SKSpriteNode = SKSpriteNode()
var left: SKSpriteNode = SKSpriteNode()
var right: SKSpriteNode = SKSpriteNode()
var cursor: SKSpriteNode = SKSpriteNode(imageNamed: "Cursor1Blue")
override func sceneDidLoad() {
up = self.childNode(withName: "Up") as! SKSpriteNode
down = self.childNode(withName: "Down") as! SKSpriteNode
left = self.childNode(withName: "Left") as! SKSpriteNode
right = self.childNode(withName: "Right") as! SKSpriteNode
tilemap = self.childNode(withName: "Tile Map Node") as! SKTileMapNode
cursor.position = tilemap.centerOfTile(atColumn: 5, row: 5)
cursor.zPosition = 0.1
self.addChild(cursor)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first! as UITouch
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
print(touchedNode.name!)
if(touchedNode.name == "Tile Map Node") {
let map = touchedNode as! (SKTileMapNode)
print(map.tileColumnIndex(fromPosition: positionInScene)) // higher than 10
print(map.tileRowIndex(fromPosition: positionInScene)) // higher than 8
print(map.numberOfColumns) // 10
print(map.numberOfRows) // 8
print(map)
}
}
}