首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比SKTileMapNode中的列数和行数高的列和行索引

比SKTileMapNode中的列数和行数高的列和行索引
EN

Stack Overflow用户
提问于 2019-03-17 05:53:42
回答 1查看 210关注 0票数 0

我想创建一个场景,在该场景中,我可以使用按钮在SKTileMapNode中从一个磁贴移动到另一个磁贴。我在Spritekit场景编辑器的外部.sks文件中创建了一个10x8的SKTileMapNode。为了准确地设置光标的位置,我希望注册该块的特定列和行索引,然后找出它的位置。使用函数numberOfColumnstileRowIndex可以得到比numberOfRowstileRowIndex更高的值,这两个值分别是10和8。由于这个问题,我的光标显示在错误的位置。它甚至没有在任何平铺位置上对齐。

代码语言:javascript
复制
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)
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-17 12:11:05

当使用tileColumnIndex/tileRowIndex获取索引时,您需要对照磁贴地图中的位置进行检查。在您的示例中,您提供了场景的位置,该位置可能不同,具体取决于平铺贴图的位置。

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

代码语言:javascript
复制
let positionInMap = touch.location(in: map)
let column = map.tileColumnIndex(fromPosition: positionInMap)
let row = map.tileRowIndex(fromPosition: positionInMap)

来自您的示例的touchesBegan()的更新版本

代码语言:javascript
复制
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定位平铺时光标与平铺对齐,请将其作为子对象添加到平铺贴图,以确保它们位于相同的坐标系上。否则,您将不得不向光标位置添加必要的偏移量。

代码语言:javascript
复制
tilemap.addChild(cursor)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55201858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档