0

player function

@objc func playerFunction(){
    
    player = SKSpriteNode(imageNamed: "Player")
    player.name = "Hero"
    player.size = CGSize(width: 80, height: 80)
    player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
    player.physicsBody?.affectedByGravity = false
    player.physicsBody?.isDynamic = false
    player.physicsBody?.allowsRotation = false
    player.physicsBody!.categoryBitMask = 1
    player.physicsBody!.collisionBitMask = 0
    player.physicsBody!.contactTestBitMask = 2 | 5 | 6 | 7
    player.physicsBody?.velocity.dx = 0
    player.physicsBody?.velocity.dy = 0
    player.zPosition = 1
    player.position = CGPoint(x: self.frame.width / 2 - 700, y: self.frame.height / 2 - player.frame.height)
    self.addChild(player)
    
}

platform method

@objc func platformFunction (){

    platform_One = SKSpriteNode(imageNamed: "Platform")
    platform_One.physicsBody = SKPhysicsBody(rectangleOf: platform_One.size)
    platform_One.physicsBody?.affectedByGravity = false
    platform_One.physicsBody?.isDynamic = false
    platform_One.zPosition = 4
    platform_One.position = CGPoint(x: 1 + self.frame.width / 2 - 570, y: self.frame.height / 2 - 260)
    self.addChild(platform_One)
    
    
    platform_Two = SKSpriteNode(imageNamed: "Platform")
    platform_Two.physicsBody = SKPhysicsBody(rectangleOf: platform_Two.size)
    platform_Two.physicsBody?.affectedByGravity = false
    platform_Two.physicsBody?.isDynamic = false
    platform_Two.physicsBody!.categoryBitMask = 7
    platform_Two.physicsBody!.collisionBitMask = 1
    platform_Two.physicsBody!.contactTestBitMask = 1
    platform_Two.zPosition = 5
    platform_Two.position = CGPoint(x: 1 + self.frame.width / 2 - 420, y: self.frame.height / 2 - 260)
    self.addChild(platform_Two)

The player method and platform method code is added above and the player doesn't land on top of the platform instead the player goes through the platform even though the physics have been stated as above.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
OptiCode22
  • 21
  • 6
  • 1
    You haven't set the `collisionBitMask` for either the player or the first platform. This may be the issue. Also, the masks of `2 | 5 | 6 | 7` is not the correct way of doing things - the numbers should be powers of 2 (e.g. 1, 2, 4, 8, 16, ...). Refer to [this answer](https://stackoverflow.com/a/31111039/9607863) for more information. – George Apr 18 '21 at 01:04
  • How do I set this correctly ? – OptiCode22 Apr 18 '21 at 13:04
  • 1
    They are bitwise masks (search on Google to find more about what those are). Basically they should be powers of 2 as mentioned in last question, and follow the link to an answer in my comment above which should solve this problem. – George Apr 18 '21 at 17:09
  • 1
    I believe at least one of your physics bodies needs to have the isDynamic flag set to true, otherwise collisions will not be recognised between them. – JohnL Apr 19 '21 at 11:03

0 Answers0