首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JSTileMap中使用JSTileMap

如何在JSTileMap中使用JSTileMap
EN

Stack Overflow用户
提问于 2014-09-23 12:27:20
回答 1查看 3K关注 0票数 6

您能快速地使用JSTileMap吗?如果可以,您如何使用它?如果我不能快速地使用它,或者如果它太多才多艺,那么还有什么可以用于.tmx地图的吗?注意,我正在使用sprite工具包。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-23 18:30:35

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

代码语言:javascript
复制
#import "JSTileMap.h"
#import "LFCGzipUtility.h"

接下来,只需进入SKScene文件并创建一个tileMap变量,如下所示:

代码语言:javascript
复制
var tileMap = JSTileMap(named: "tileMap.tmx")

我觉得定位有点棘手,所以我也加进去了。

代码语言:javascript
复制
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地板的代码:

代码语言:javascript
复制
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! :)
                }
            }
        }
    }
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25995131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档