How to use JSTileMap in swift

烈酒焚心 提交于 2019-12-09 11:56:38

问题


Can you use JSTileMap in swift, and if so, how do you use it. If I can't use it in swift, or if it is too buggy, then is there anything else I can use for .tmx maps. note, i am using sprite kit


回答1:


Yes you can, I just started using it yesterday and haven't found a problem yet! Start by importing the JSTileMap files and the libz.dylib framework. Then add a bridging header with the following imports:

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

Next simply go into you SKScene file and create a tileMap variable as shown below:

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

I found positioning a little bit tricky so ill add that in too.

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

EDIT

below is the code I used to create a floor of 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! :)
                }
            }
        }
    }


来源:https://stackoverflow.com/questions/25995131/how-to-use-jstilemap-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!