2

I'm a swift 5 newcomer and I have a problem with the physics in my game! I'm not sure if I really do it right but I can't fix it on my own... My problem is that I‘m not be able to create a physics body with this code! I always get an error like „Physics Body: Could not create physics body!“ I already found a workaround where my physics Body is like a box but I want to have a physics body where the borders like the same as my car, but nothing works...I already hear about there is a bug but the workaround of the Apple Developer Forum didn‘t work for me... I will be really thankful for an Answer!

'''

    enemies = (GKRandomSource.sharedRandom().arrayByShufflingObjects(in: enemies) as! [String])
    let enemy = SKSpriteNode(imageNamed: enemies[0])
    enemy.setScale(1.5)
    
    let positionX = arc4random_uniform(2) == 0 ? self.frame.size.width / 4 : self.frame.size.width * 3/4
    
    enemy.position = CGPoint(x: positionX, y: self.frame.size.height + enemy.size.height)
    enemy.zRotation = CGFloat (Double.pi)
    
    enemy.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: enemies[0]), size: enemy.size)
    
    enemy.physicsBody?.affectedByGravity = false
    enemy.physicsBody?.isDynamic = true
    
    enemy.physicsBody?.categoryBitMask = enemyCategory
    enemy.physicsBody?.collisionBitMask = driverCategory

'''

YungDiver
  • 21
  • 2
  • Welcome to SO. What is the exact problem? – Mathias Jul 19 '20 at 07:36
  • Hello! My problem is that I‘m not be able to create a physics body with this code! I always get an error like „Physics Body: Could not create physics body!“ I already found a workaround where my physics Body is like a box but I want to have a physics body where the borders like the same as my car, but nothing works...I already hear about there is a bug but the workaround of the Apple Developer Forum didn‘t work for me... – YungDiver Jul 19 '20 at 10:02
  • Please edit the question instead of commenting. This will improve reading your question a lot. – Mathias Jul 19 '20 at 10:24

1 Answers1

0

Looking at the Apple Developer Forum there is a thread about it and it looks like a bug in Swift. But still some guys found solution for that issue.

  • Thanks for the quick answer! I already tried this but it still don`t working for me.. I actually found a workaround but there I have borders like a box but I want that the borders are the same as at the car! My workaround: ``` driver.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: driver.size.width / 2.5, height: driver.size.height / 1.25)) ``` – YungDiver Jul 18 '20 at 21:57
  • Did you tried [this](https://stackoverflow.com/a/60016825/11471446) one? – Łukasz Łabuński Jul 22 '20 at 09:27
  • Thanks Lukas! Unfortunately it didn't work for me... – YungDiver Jul 26 '20 at 20:13