YungDiver
YungDiver

Reputation: 21

Swift 5 (PhysicsBody: Could not create physics body)

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

'''

Upvotes: 2

Views: 61

Answers (1)

Łukasz Łabuński
Łukasz Łabuński

Reputation: 587

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.

Upvotes: 0

Related Questions