列和行索引高于 SKTileMapNode 中的列和行数

Posted

技术标签:

【中文标题】列和行索引高于 SKTileMapNode 中的列和行数【英文标题】:Higher column and row index than number of column and row in SKTileMapNode 【发布时间】:2019-03-16 21:53:41 【问题描述】:

我想创建一个场景,其中有按钮可以在 SKTileMapNode 内将光标从一个瓷砖移动到另一个瓷砖。我在 Spritekit 场景编辑器的 extern .sks 文件中创建了一个 10x8 SKTileMapNode。要将光标位置设置在与磁贴完全相同的位置,我想注册磁贴的特定列和行索引,然后找出他的位置。使用函数numberOfColumnstileRowIndex 给我比numberOfRowstileRowIndex 更高的值,它们是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)
        
    

【问题讨论】:

【参考方案1】:

使用tileColumnIndex/tileRowIndex 获取索引时,您需要检查平铺地图中的位置。在您的示例中,您给出了场景的位置,这可能不一样,具体取决于瓦片地图的位置。

如果您使用地图中的触摸位置,您应该会返回正确的索引。

let positionInMap = touch.location(in: map)
let column = map.tileColumnIndex(fromPosition: positionInMap)
let row = map.tileRowIndex(fromPosition: positionInMap)

您的示例中touchesBegan() 的更新版本

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)
    if(touchedNode.name == "Tile Map Node") 
        let map = touchedNode as! (SKTileMapNode)
        let positionInMap = touch.location(in: map) // Get the touched position in map
        print(map.tileColumnIndex(fromPosition: positionInMap))
        print(map.tileRowIndex(fromPosition: positionInMap))
        print(map.numberOfColumns)
        print(map.numberOfRows)
        print(map)
    

为确保您的光标在使用centerOfTile 定位时与图块对齐,请将其作为子项添加到图块地图中,以确保它们位于同一坐标系上。否则,您必须在光标位置添加必要的偏移量。

tilemap.addChild(cursor)

【讨论】:

以上是关于列和行索引高于 SKTileMapNode 中的列和行数的主要内容,如果未能解决你的问题,请参考以下文章

二维数组中的列和行?

迭代 Pandas Dataframe 中的列和行

如何将数据从 python 列表中的列和行写入 csv 文件?

SQL Server Reporting Services 2008 中的列和行分组

为 R 中的列和行创建一个“for”循环

重新排序矩阵元素以反映朴素python中的列和行聚类