0

So I got Swift to work with Tiled. It draws an Isomatic map on my screen and I added the physics bodies to the floor tiles it self.

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
        }
    }
}

As you can see on this screenshot, it builds the map and add the physicsBodies. The problem starts, when I want to interact with floor tiles by touche.

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")
        }
    }
}

Every time when i touch an object, it returns the NULL string. So what aim I missing?

  • First eliminating the obvious: do your nodes have assigned names? (Also, what do you hope to achieve with physics and isometric tiles? SpriteKit physics works only in 2D—in the plane of the screen—not in the pseudo-3D of most isometric tilemap games.) – rickster Jan 02 '15 at 16:13
  • My goals is to interact with the tile when an user clicks on the tile (for example, collecting point or other things who are displayed on the tile itself). so i think i might have gone a bit to deep down the rabbit hole. i've added a name property in tiled to the tiles them selfs, but actually a name is not the goal. i want an identifier (so is there a better way). The same thing is going on for the physics. i don't need them, it was just a way to trying to solve the problem of registering a touch event on a node (again if there are better ways, please let me know). – Arend-Jan Jan 03 '15 at 12:49
  • Best to make sure your tile names import from Tiled correctly before you depend on that. Check the `name` property in your `addFloor` function. Is it what you expect then? – rickster Jan 03 '15 at 16:11
  • You don't need physics to catch touch events. You're doing the right thing for touch handling in `touchesBegan` already. – rickster Jan 03 '15 at 16:13

0 Answers0