If there are professionals in the development of games on Swift, namely on the SpriteKit framework, then please tell me whether it is possible to make it so that the character would not be able to call on the map layer, in this case the ship should crash into the sand layer

enter image description here

Physics should not be imposed on the sprite, namely, on the LAYER CARD, like everything is visible on the screenshot. Thank!

  • You can somehow look at tutorials on handling collisions and then ask questions if something is not clear? - Max Mikheyenko
  • I mean that you can do a collision detect, but I would like to know if it is possible to impose physics on the SKTileMapNode card, just like on the SKSpriteNode sprite? - webkostya
  • Here's an article on the topic: code.tutsplus.com/tutorials/… - Andrew
  • No, the article is not at all on this topic, I know how to determine whether two sprites collided or not, the question is how to push the sprite with the map (SKTileMapNode) - webkostya

3 answers 3

Create a FieldNode field that affects the sprite on contact https://developer.apple.com/reference/spritekit/skfieldnode

SKFieldNode will be inherited from SKNode "fields" of the card and SKFhysicsBody will be created through texture to SKFieldNode, SKPhysicsBody will use impulse between impulse between two Node (ship and "field") via impulse, details https://habrahabr.ru/ post / 225517 . It is easier to create a "field" than to interrogate each pixel and its physical body with which, in fact, an interaction occurs.

  • Can I have a little more detail? - webkostya
  • Can you write how do you build physics on a map (SKTileMapNode)? - webkostya

Since each tile can be used with physics, you will have to check the user data for each cell and create a SKNode for this cell, assigning to it the corresponding physical body

A source

  • I was able to disassemble each cell, it remains to pull the image out of the cell in order to apply the physical body SKPhysicsBody (texture: ... in principle, I even got the texture, but it doesn't fit tileDefinition? .Textures.first have an idea to pull out the name of the picture and try create a texture, but something I can’t pull out the name) - webkostya
  • Can you tell me how to create physics for each cell by the texture of this cell? - webkostya
  • You, I do not think that a lot of cells in general, assign, respectively, to those who have a "shore". - Daniel Protopopov
  • So how at least for one cell to create physics on the texture of the same cell? - webkostya
  • Don't you know what texture the cell will have? This is a certain set of textures, right? Or is the layer generated independently? - Daniel Protopopov

The issue is resolved. enter image description here

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
The scene itself and the map layer (self.ground) have anchorPoint = CGPoint (x: 0, y: 0)
PS While this is a test version, because it is not yet known what will happen with large maps, well, I think I should add that the physics would fall solely on the extreme cells.

  • one
    Пользователи @gdf1984 и @Daniel Protopopov можете удалить свои ответы, они бесполезны - this is a rather rude statement. Even if you were unable to gain from them, other readers will be able to. - Nick Volynkin
  • @Nick Volynkin Why is it rude, I told the truth, and did not call anyone, what is the rudeness here? After all, these answers really do not carry any useful information, they were written just for the sake of reward) In general, if you see rudeness here, then please delete this question. - webkostya
  • one
    @NickVolynkin I certainly can, but I will not delete, indeed, someone else might find it useful. The author, glad that you yourself have found a solution. - Daniel Protopopov
  • @webkostya think you hurry with telepathy. For the sake of which the authors wrote their answers - only the authors themselves know. - Nick Volynkin