如何在swift中使用JSTileMap

编程入门 行业动态 更新时间:2024-10-16 02:28:59
本文介绍了如何在swift中使用JSTileMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你能在swift中使用 JSTileMap ,如果是这样,你如何使用它?如果我不能在swift中使用它,或者它太麻烦了,那么还有什么我可以用于.tmx地图。请注意,我正在使用精灵工具包

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

推荐答案

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

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"

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

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

编辑

以下是我用来创建一个SKSpriteNodes的代码:

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

更多推荐

如何在swift中使用JSTileMap

本文发布于:2023-11-26 21:59:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   swift   JSTileMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!