Search code examples
swiftsprite-kitjstilemap

Use the object position of TileMap in Sprite Kit


in TileMap we can use layers and one of them is the Object layer. but how is to use them to gave me his position?

with this code I can see the Position of the object, but it does not have 'a member named position'

let group:TMXObjectGroup = tileMap.groupNamed("Blocks")
        let theObjects: [AnyObject] = group.objectsNamed("Wall")
        for i in theObjects {
            print(i)
        }

The Result of the Code

So i can not save the Position in an Var or Let, how we can?


Solution

  • I'm not entirely sure what you're asking, but if I understood you correctly, this will give you what you want:

    extension JSTileMap {
      func getObjectPositionOnLayer(layerName: String, objectName: String) -> CGPoint {
        var position = CGPoint()
    
        if let objectLayer = groupNamed(layerName) {
          if let object: NSDictionary = objectLayer.objectNamed(objectName) {
            position = CGPoint(
              x: object.valueForKey("x") as! CGFloat,
              y: object.valueForKey("y") as! CGFloat
            )
          }
        }
    
        return position
      }
    }
    

    Example usage:

    // Get player start position from the object named "Start", in 
    // the object layer "Meta Layer".
    let startPoint = map.getObjectPositionOnLayer("Meta Layer", objectName: "Start")
    player.position = startPoint