6

你能JSTileMap在swift中使用吗,如果是,你如何使用它。如果我不能快速使用它,或者它太有问题,那么我还有什么可以用于 .tmx 地图的。注意,我正在使用精灵套件

4

1 回答 1

13

是的,你可以,我昨天才开始使用它,还没有发现问题!首先导入 JSTileMap 文件和 libz.dylib 框架。然后使用以下导入添加桥接头:

#import "JSTileMap.h"
#import "LFCGzipUtility.h"

接下来只需进入您的 SKScene 文件并创建一个 tileMap 变量,如下所示:

var tileMap = JSTileMap(named: "tileMap.tmx")

我发现定位有点棘手,所以我也把它加进去了。

self.anchorPoint = CGPoint(x: 0, y: 0)
self.position = CGPoint(x: 0, y: 0) //Change the scenes anchor point to the bottom left and position it correctly


let rect = tileMap.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
tileMap.position = CGPoint(x: 0, y: 0) //Position in the bottom left
addChild(tileMap) //Add to the scene

编辑

下面是我用来创建一层 SKSpriteNodes 的代码:

func addFloor() {
        for var a = 0; a < Int(tileMap.mapSize.width); a++ { //Go through every point across the tile map
            for var b = 0; b < Int(tileMap.mapSize.height); b++ { //Go through every point up the tile map
                let layerInfo:TMXLayerInfo = tileMap.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.

                if gid == 2 || gid == 9 || gid == 8{ //My gIDs for the floor were 2, 9 and 8 so I checked for those values
                    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

 //You now have a physics body on your floor tiles! :)
                }
            }
        }
    }
于 2014-09-23T18:30:35.473 回答