在 Sprite Kit 中使用 TileMap 的对象位置

Use the object position of TileMap in Sprite Kit

在 TileMap 中我们可以使用层,其中之一就是对象层。 但是怎么用它们给我定位呢?

使用这段代码我可以看到对象的位置,但它没有 有 '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

所以我无法将 Position 保存在 Var 或 Let 中,我们怎么办?

我不完全确定你在问什么,但如果我理解正确的话,这会给你你想要的:

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

用法示例:

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