0

在 TileMap 中,我们可以使用图层,其中之一是对象图层。但如何利用他们给我他的位置?

使用此代码,我可以看到对象的位置,但它没有“名为位置的成员”

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

代码的结果

所以我不能将位置保存在 Var 或 Let 中,我们怎么能?

4

1 回答 1

0

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
于 2015-07-27T07:26:06.433 回答