所以我让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
}
}
}
正如您在screenshot上看到的那样,它会构建地图并添加physicsBodies。当我想通过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")
}
}
}
每当我触摸一个对象时,它都会返回NULL字符串。那么我错过了什么目标?