0

所以我让 Swift 与 Tiled 一起工作。它在我的屏幕上绘制了一张 Isomatic 地图,我将物理实体添加到它自己的地砖上。

var tiledMap = JSTileMap(named: "tiledMap.tmx")

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    let rect = tiledMap.calculateAccumulatedFrame() //This is not necessarily needed but returns the CGRect actually used by the tileMap, not just the space it could take up. You may want to use it later

    tiledMap.position = CGPoint(x: self.size.width/2, y: self.size.height/2) //Position in the center
    addFloor()
    addChild(tiledMap) //Add to the scene
}

func addFloor() {
    for var a = 0; a < Int(tiledMap.mapSize.width); a++ { //Go through every point across the tile map
        for var b = 0; b < Int(tiledMap.mapSize.height); b++ { //Go through every point up the tile map
            let layerInfo:TMXLayerInfo = tiledMap.layers.firstObject as TMXLayerInfo //Get the first layer (you may want to pick another layer if you don't want to use the first one on the tile map)

            let point = CGPoint(x: a, y: b) //Create a point with a and b
            let gid = layerInfo.layer.tileGidAt(layerInfo.layer.pointForCoord(point)) //The gID is the ID of the tile. They start at 1 up the the amount of tiles in your tile set.
            let node = layerInfo.layer.tileAtCoord(point) //I fetched a node at that point created by JSTileMap
            node.physicsBody = SKPhysicsBody(rectangleOfSize: node.frame.size) //I added a physics body
            node.physicsBody?.dynamic = false
        }
    }
}

正如您在此屏幕截图中看到的那样,它构建了地图并添加了physicsBodies。当我想通过触摸与地砖进行交互时,问题就开始了。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location: CGPoint! = touch.locationInNode(self)

        let nodeAtPoint = self.nodeAtPoint(location)

        if (nodeAtPoint.name != nil) {
            println("NODE FOUND: \(nodeAtPoint.name)")
        } else {
            println("NULL")
        }
    }
}

每次我触摸一个对象时,它都会返回 NULL 字符串。那么我错过了什么目标?

4

0 回答 0