Giving physics to tiles of SKTileMapNode in Xcode 8

后端 未结 4 1849
青春惊慌失措
青春惊慌失措 2021-02-09 20:20

I am learning Swift, and as a project I am working on a tile based 2D game similar to super mario where my character will walk and jump on tiles.

The latest version of X

4条回答
  •  生来不讨喜
    2021-02-09 20:48

    I'm not sure there's a surefire way to do this yet... but here's two ways to think about how to try apply physics to a tilemap.

    Option 1: Apply SKNodes to each positions of each tile on your map, and apply an appropriate physicsbody to that SKNode based on the content and state of that tile.

    Option 2: Use the position information of each tile to add an array of physicsbodies to the SKTileMapNode, and position each of them accordingly.

    I'm imagining a gravity down, Mario style platformer, with this kind of terrain in need of physics bodies for the ground:

    Lifted transcript from Apple's WWDC about tiles and physics:

    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.


    Makes it sound very simple, but takes a far greater mind than mine to figure out what is being done, and how it's being done.

提交回复
热议问题