你可以在swift中使用JSTileMap
,如果是這樣,你如何使用它。如果我不能快速使用它,或者它太麻煩了,那麼我還有什麼可以用於.tmx地圖的東西。請注意,我正在使用sprite套件如何在swift中使用JSTileMap
回答
是的,你可以,我剛剛開始使用它,並沒有發現問題呢!首先導入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! :)
}
}
}
}
我得到了迅速的代碼來將瓷磚地圖塊變成SKSpriteNodes,但你沒有要求它,所以我沒有打擾張貼。只是問你是否願意。 – 2014-09-23 18:33:02
非常感謝你!如果你可以添加它,那應該是很好的,但這僅僅是無用的! – 2014-09-23 19:24:24
@MarkCalhoun不是問題,我爲你添加了額外的代碼,希望它被證明足夠好。 – 2014-09-23 19:30:04
- 1. JSTileMap(.tmx)vs Xcode SKTileMapNode
- 2. 如何在Swift中使用位操作?
- 3. 你如何在Swift中使用CGEventTapCreate?
- 4. swift - 如何在NSTimer中使用FIRE()
- 5. 如何在swift數組中使用minElement
- 6. 如何在Swift中使用Affdex SDK?
- 7. 如何在Swift中使用MKSnapshotter(iOS)
- 8. 如何在Swift 3.0中使用函數
- 9. 如何在Swift中使用精度?
- 10. 如何在JSQMessagesViewController中使用mapbox swift
- 11. 如何在swift中使用NMSSH
- 12. 如何在Swift中使用NSWindowOcclusionState.Visible
- 13. 如何在Swift中爲UserDefaults使用KVO?
- 14. 如何在swift中使用SKConstraint
- 15. 如何在Swift框架中使用Cocoapods?
- 16. 如何在Swift 2.0中使用enumerateNodeWithName?
- 17. 如何在swift中使用_STRUCT_MCONTEXT
- 18. 如何在Swift中使用centralManager willRestoreState?
- 19. 如何在Swift中使用NSStreamEvents?
- 20. 如何在Swift 3中使用UnsafeMutablePointer?
- 21. 如何在Swift中使用JTCalendar?
- 22. 如何在Swift 4中使用#keyPath()?
- 23. 如何在Swift/Alamofire中使用multipartFormData?
- 24. 如何在swift中使用dispatch_sync?
- 25. 如何在Swift中使用ViewWithTag?
- 26. 如何使用 - (NSString *)encodeWrappedBase64ForData在swift中?
- 27. 如何在Swift 3中使用CGContextDrawImage?
- 28. 如何在swift 3.0中使用UnSafeRawPointer
- 29. 如何使用SWIFT
- 30. 如何使用本地Swift方式在Swift 3中讀取Plist?
[TilemapKit](http://tilemapkit.com)完全兼容斯威夫特。只是說';) – LearnCocos2D 2015-09-11 10:58:24