6

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

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
Mark Calhoun
  • 545
  • 1
  • 5
  • 10

1 Answers1

13

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! :)
                }
            }
        }
    }
Jack Chorley
  • 2,309
  • 26
  • 28
  • Ive got swift code to turn tile map blocks into SKSpriteNodes too but you didn't ask for it so I didn't bother posting. Just ask if you'd like it. – Jack Chorley Sep 23 '14 at 18:33
  • Thank you SO MUCH!!! It should be nice if you could add that in, but this alone is beyond helpful! – Mark Calhoun Sep 23 '14 at 19:24
  • @MarkCalhoun not a problem, I added the extra code for you, hopefully it is documented well enough. – Jack Chorley Sep 23 '14 at 19:30
  • Just fyi, this keep happening when it loads dyld: Symbol not found: _swift_isaMask Referenced from: /private/var/mobile/Containers/Bundle/Application/BB357D81-4743-411E-A9D2-0BF725‌​08A23E/Platformer.app/Platformer Expected in: /private/var/mobile/Containers/Bundle/Application/BB357D81-4743-411E-A9D2-0BF725‌​08A23E/Platformer.app/Frameworks/libswiftCore.dylib in /private/var/mobile/Containers/Bundle/Application/BB357D81-4743-411E-A9D2-0BF725‌​08A23E/Platformer.app/Platformer – Mark Calhoun Sep 24 '14 at 01:00
  • Do you have an Xcode project you could send me as example? if not thats fine, just an idea. – Mark Calhoun Sep 24 '14 at 01:11
  • Sorry for the late reply, i wasnt able to check stackoverflow yesterday. I can send you a project ive been playing around with uf you like, but i havent documented much of it. There isnt much code so it shouldnt matter. Give me a couple of hours and ill post a dropbox link – Jack Chorley Sep 25 '14 at 06:01
  • 1
    Here is a link to the project as promised. Ignore the name, it was something I was working on. I put in some sample artwork and physics bodies so you can see how it all works together. The only thing I have changed is the GameScene.swift file. The rest is simply how it is generated. Also, I used the Xcode 6.0 from the app store to create it, not the developer portal: https://www.dropbox.com/s/nnf2jrewcgtz1ih/ChangeType.zip?dl=0 – Jack Chorley Sep 25 '14 at 06:44
  • Thank you! The problem I'm having right now is that it can't find the textures, but this should clear that up! – Mark Calhoun Sep 25 '14 at 15:29
  • @MarkCalhoun I just thought I would send a quick message making sure that everything worked ok. I assume that there were no problems and you managed to resolve and issues? – Jack Chorley Sep 28 '14 at 20:55
  • well, there is one problem, whenever i try to load my own view on my own project, it cuts off some of the map. I can only see the tiles above a certain area – Mark Calhoun Sep 30 '14 at 16:00
  • That, and when i look at your project everything goes out to the edge (and looks bigger?), while mine stop after a certain point even though there are tiles there. – Mark Calhoun Sep 30 '14 at 19:00
  • Ok, two questions: Firstly, what version of Xcode are you using. Secondly, do you have some way of uploading your project, if so, i will take a look – Jack Chorley Oct 01 '14 at 17:10
  • alright, it took a while but i was able to fix it. some of the files werner being loaded properly – Mark Calhoun Oct 02 '14 at 02:28