Giving physics to tiles of SKTileMapNode in Xcode 8

后端 未结 4 1852
青春惊慌失措
青春惊慌失措 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:49

    Apple staff member Bobjt says here that "the right approach" is to add user data to your SKTileDefinition objects and use that to identify and add physics bodies to your tiles.

    So you would add a user data value to a tile definition either programmatically or in the editor, like so:

    Then in code you would check each tile definition to see if it has the user data value. If so, then you need to calculate the tile's position and add a physics body on a new node, parenting it to your tile map. Here is the code for this which Bobjt referred to as "the correct approach":

    self.tileMap = self.childNode(withName: "Tile Map") as? SKTileMapNode  
    guard let tileMap = self.tileMap else { fatalError("Missing tile map for the level") }  
    
    let tileSize = tileMap.tileSize  
    let halfWidth = CGFloat(tileMap.numberOfColumns) / 2.0 * tileSize.width  
    let halfHeight = CGFloat(tileMap.numberOfRows) / 2.0 * tileSize.height  
    
    for col in 0..

    Personally, I think this approach is too fussy. I'm going to try a different approach in my game and if it works I'll post it here. What I'd really like is for Apple to enhance the tile map API so that we can add physics bodies directly to individual tiles. Maybe in the process they could optimize the engine so that physics bodies on individual tiles would be automatically merged to form larger, more optimal shapes in order to improve system performance.

    Update: I filed a request with Apple about this issue, for whatever good might come of it.

提交回复
热议问题