2014-09-23 63 views
5

你可以在swift中使用JSTileMap,如果是這樣,你如何使用它。如果我不能快速使用它,或者它太麻煩了,那麼我還有什麼可以用於.tmx地圖的東西。請注意,我正在使用sprite套件如何在swift中使用JSTileMap

+0

[TilemapKit](http://tilemapkit.com)完全兼容斯威夫特。只是說';) – LearnCocos2D 2015-09-11 10:58:24

回答

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 

EDIT

下面是我用來創建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! :) 
       } 
      } 
     } 
    } 
+0

我得到了迅速的代碼來將瓷磚地圖塊變成SKSpriteNodes,但你沒有要求它,所以我沒有打擾張貼。只是問你是否願意。 – 2014-09-23 18:33:02

+0

非常感謝你!如果你可以添加它,那應該是很好的,但這僅僅是無用的! – 2014-09-23 19:24:24

+0

@MarkCalhoun不是問題,我爲你添加了額外的代碼,希望它被證明足夠好。 – 2014-09-23 19:30:04