Physics body not showing only for one sprite?
Physics body not showing only for one sprite?
The problem I have is when I initialize my sprite, I have another sprite defined the same way and that sprite shows that it has a physics body because I see the green outline on that, but this sprite below has no green outline on it, I think that there is a problem when I create the SKPhysicsBody
or maybe because I create 13 duplicates of the same sprite at random positions as shown in the second snippet of code.
SKPhysicsBody
@objc func CreateNewAsteroid() {
var asteroid : SKSpriteNode?
let moveAsteroidDown = SKAction.repeatForever(SKAction.moveBy(x: 0, y: -1, duration: 0.01))
let rotateAsteroid = SKAction.repeatForever(SKAction.rotate(byAngle: 25, duration: 5))
let asteroidXpos = randomNum(high: self.frame.size.width/2, low: -1 * self.frame.size.width/2)
let asteroidYpos = randomNum(high: 2.5*self.frame.size.height, low: self.frame.size.height/2)
let asteroidOrigin : CGPoint = CGPoint(x: asteroidXpos, y: asteroidYpos)
asteroid = SKSpriteNode(imageNamed: possibleAsteroidImage[Int(arc4random_uniform(4))])
asteroid?.scale(to: CGSize(width: player.size.height, height: player.size.height))
asteroid?.position = asteroidOrigin
asteroid?.run(moveAsteroidDown)
asteroid?.run(rotateAsteroid)
let asteroidRadius : CGFloat = (asteroid?.size.width)!/2
asteroid?.physicsBody? = SKPhysicsBody(rectangleOf: (asteroid?.size)!)
asteroid?.physicsBody?.categoryBitMask = asteroidCategory
asteroid?.physicsBody?.affectedByGravity = false
asteroid?.physicsBody?.isDynamic = false
asteroid?.physicsBody?.allowsRotation = false
asteroid?.physicsBody?.categoryBitMask = asteroidCategory
asteroid?.physicsBody?.collisionBitMask = 0
asteroid?.physicsBody?.contactTestBitMask = shipCategory
asteroidArray.append(asteroid!)
self.addChild(asteroid!)
}
I create 13 of the sprites like this
for _ in 0...13 {
CreateNewAsteroid()
}
1 Answer
1
Instead of making the SKSpriteNode asteroid an optional, make it a non-optional so it does not become a null value by deleting the ?
in var asteroid : SKSpriteNode?
?
var asteroid : SKSpriteNode?
So you want to put var asteroid : SKSpriteNode
var asteroid : SKSpriteNode
After making this change, some errors will show up telling you to delete some !
's and ?
's (Make sure to delete these of course).
!
?
then go to your GameViewController.swift
file and add the line view.showsPhysics = true
under view.showsNodeCount = true
GameViewController.swift
view.showsPhysics = true
view.showsNodeCount = true
When you are done you should see a green outline around the sprite.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Optionals are not magic. An optional is an implicit if not null else do nothing check. The only thing you say by taking off the ? is that asteroid can not be null, which would not fix your problem. Something else happened where you changed your code so that asteroid is no longer null, and you are neglecting to put that part into your answer, which would be the true answer.
– Knight0fDragon
Jun 28 at 12:42