Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

How to Add a Physics Body to SKTileMapNode

How to Add a Physics Body to SKTileMapNode

With Sprite Kit and Xcode 8 you can now add Tile Maps to your scenes. This is a particularly easy way of constructing levels since you can essentially “paint” tiles for the player to navigate around (in a top down world), or on top of (in a side scroller). The trick to this is adding a physics body to each tile in the SKTileMapNode.

To do this, add the function below to your scene, and call it with the SKTileMapNode passed in as a parameter. You’ll notice some lines are commented out. If you uncomment these (and uncomment the closing bracket) you can use the userData option to pick out only specific tiles you want to have a physics body for.

 

 

 

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com