加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 在Xcode 8中为SKTileMapNode的tile添加物理

发布时间:2020-12-14 19:18:05 所属栏目:百科 来源:网络整理
导读:我正在学习 Swift,作为一个项目,我正在开发一个类似于超级马里奥的基于平铺的2D游戏,我的角色会走路并跳上瓷砖. 最新版本的Xcode和Sprite Kit可以直接在Xcode中创建Tile Map. 在新的Xcode和Sprite工具包的演示中,这个人展示了一个类似于我正在做的游戏. http
我正在学习 Swift,作为一个项目,我正在开发一个类似于超级马里奥的基于平铺的2D游戏,我的角色会走路并跳上瓷砖.

最新版本的Xcode和Sprite Kit可以直接在Xcode中创建Tile Map.

在新的Xcode和Sprite工具包的演示中,这个人展示了一个类似于我正在做的游戏.

https://developer.apple.com/videos/play/wwdc2016/610/(大约20分钟).

他提到我给Tiles用户数据属性,并且在代码中我们搜索具有该用户数据的所有tile并给它们一些物理属性,以便角色可以碰撞或与它们交互(在我的情况下,我的角色不会掉落)或者穿过瓷砖).

所以基本上,这个想法是给那些瓦片一个物理体,但这不能用SKphysicsBody来完成.所以必须有另一种方式,因为我是Swift的新手,我很想念它.

如果有人知道这一点,我将非常感谢帮助.

如果问题不清楚,请告诉我,因为我也是堆栈溢出的新手.

解决方法

我不确定有什么可靠的方法可以做到这一点……但是这里有两种思考如何尝试将物理应用于tilemap的方法.

选项1:将SKNodes应用于地图上每个图块的每个位置,并根据该图块的内容和状态将适当的物理实体应用于该SKNode.

选项2:使用每个图块的位置信息将一个物理实体数组添加到SKTileMapNode,并相应地定位它们.

我想象一个重力下降,马里奥风格的平台游戏,这种地形需要物理机构的地面:

enter image description here

摘自苹果公司WWDC关于瓷砖和物理学的成绩单:

And you’ll note that I’m colliding with the tiles here.

And I achieve this by leveraging custom user data that we can put on
each of our tiles.

Here,I’ll show you in our tile set.

Select one of the variants here.

And you can see that we have some user data over here.

And I just have a value called edgeTile which is a Boolean,and I set
to 1.

So,in code,I’m going through the tile map in our platform demo here,
and I’m looking for all of these edge tiles.

And whenever I find one,I create some physics data to allow the
player to collide with it.

And since it is just in our tile map here,say I wanted to get over
this large wall here.

When I run the game,you’ll see that my guy can’t actually jump high
enough to get over it.

And he really wants to because that red button over there looks really
tempting.

I really want to push that thing.

So,since we’re just generating our physics data from our tiles and
our user data,all we can do is just going here and erase these tiles
and build and run our game again.

The tiles are now gone and we can now move through there.

And we didn’t have to change code or anything.

We just used the data that we pulled from the tile map to set up our
tile.

It’s that simple.

听起来很简单,但是比我的想法要大得多,弄清楚正在做什么,以及如何做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读