J
J
John2017-02-16 17:14:02
macOS
John, 2017-02-16 17:14:02

How to make a character collision?

If there are professionals in the development of games on Swift, namely on the SpriteKit framework, then
please tell me if it is possible to make it so that the character could not enter the map layer, in this case the ship should crash into the sand layer
c3b1c6211e404af9ba013a8ca86b0175.JPEG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Donkovtsev, 2017-02-16
@Demetriy

In short, you need to enable physics for sand and ship, set its properties (by default, in my opinion, it will consider physics by analogy with ala mario games, i.e. your ship will always fall down, so you need to disable this ) and set the properties so that the sand and the ship can collide with each other.
Here is a tutorial that seems to fit https://www.raywenderlich.com/123393/how-to-create...

J
John Doe, 2017-02-24
@Homchenkokostya

Issue resolved. You can iterate over each cell and impose an empty node on it, and, accordingly, impose physics on each node according to the texture of the cell itself.
ef9c5fd27bc8466f458f179cf08794de.jpg

class GameScene: SKScene {
    var ground : SKTileMapNode!

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

        let tileSize = self.ground.tileSize

        for col in 0..<self.ground.numberOfColumns {
            for row in 0..<self.ground.numberOfRows {
                let definition = self.ground.tileDefinition(atColumn: col, row: row)

                guard let texture = definition?.textures.first else {
                    continue
                }

                let x = CGFloat(col) * tileSize.width + tileSize.width / 2.0
                let y = CGFloat(row) * tileSize.height + tileSize.height / 2.0

                let tileNode = SKNode()
                tileNode.position = CGPoint(x: x, y: y)
                tileNode.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
                tileNode.physicsBody?.affectedByGravity = false
                tileNode.physicsBody?.isDynamic = false

                self.addChild(tileNode)
            }
        }
    }
}

In this example, the scaleMode of the scene is .resizeFill (since we are writing for macOS)
The scene itself and the map layer (self.ground) have anchorPoint = CGPoint(x: 0, y: 0)
PS While this is a test option, because it is not yet known what it will be with large maps, well, I think it is necessary to add that physics would fall exclusively on the extreme cells.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question