J
J
John2017-04-07 16:38:04
Sprites
John, 2017-04-07 16:38:04

How to correctly change the zPosition of a character on the map?

Good afternoon, tell me please, can anyone do this, how to correctly change the zPosition of a character on the map? If we enumerate each cell of the map, it turns out that we go through all the cells and set the zPosition of our character to the last one. The video shows that everything is fine with the bottom cell, but the top one overlaps the character, since the coordinates are calculated from the bottom cell.
Video

override func didMove(to view: SKView) {
    self.stone = self.childNode(withName: "//stone") as! SKTileMapNode
    self.player = self.childNode(withName: "//player") as! SKSpriteNode
}

override func update(_ currentTime: TimeInterval) {
    var position = self.player.position
          position.y -= 32

    for column in 0..<self.stone.numberOfColumns {
        for row in 0..<self.stone.numberOfRows {
                
            let definition = self.stone.tileDefinition(atColumn: column, row: row)
                
            guard let _ = definition?.userData?.value(forKey: "edge") else {
                continue
            }
                
            let bounds = self.stone.centerOfTile(atColumn: column, row: row)
                
            self.player.zPosition = 0
                
            if position.y <= bounds.y {
                 self.player.zPosition = 1
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question