gpt4 book ai didi

ios - 如何在 swift 中使用 JSTileMap

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:06 26 4
gpt4 key购买 nike

你能在 swift 中使用 JSTileMap 吗?如果可以,你如何使用它。如果我不能快速使用它,或者它有太多错误,那么还有什么我可以用于 .tmx map 的。请注意,我正在使用 sprite 工具包

最佳答案

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

#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! :)
}
}
}
}

关于ios - 如何在 swift 中使用 JSTileMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com