Reputation: 37
I am building a game in SpriteKit where a player is trying to avoid collision with objects. objects are being created every 3 to 5 seconds and i'm trying to figure out a way to remove from parent once it collides with the bottom of the screen but for some reason it goes to game over when it happens Please see code below in landscape.
import SpriteKit
class PlayScene: SKScene, SKPhysicsContactDelegate {
enum ColliderType: UInt32 {
case none = 0
case player1 = 1
case object = 2
case border = 4
case remove = 8
}
gameOver = false
let ground = SKNode()
ground.position = CGPoint(x: 0, y: 0)
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width + 500, height: 1))
ground.physicsBody?.isDynamic = false
ground.physicsBody?.categoryBitMask = ColliderType.remove.rawValue
ground.physicsBody?.contactTestBitMask = ColliderType.object.rawValue
ground.physicsBody?.collisionBitMask = ColliderType.none.rawValue
character1.physicsBody!.categoryBitMask = ColliderType.player1.rawValue
character1.physicsBody!.contactTestBitMask = ColliderType.object.rawValue
character1.physicsBody!.collisionBitMask = ColliderType.object.rawValue | ColliderType.border.rawValue
object1.physicsBody!.categoryBitMask = ColliderType.object.rawValue
object1.physicsBody!.contactTestBitMask = ColliderType.player1.rawValue | ColliderType.remove.rawValue
object1.physicsBody!.collisionBitMask = ColliderType.player1.rawValue | ColliderType.border.rawValue | ColliderType.object.rawValue
if contact.bodyA.categoryBitMask == ColliderType.object.rawValue || contact.bodyB.categoryBitMask == ColliderType.object.rawValue {
gameOver == true
}
if contact.bodyA.categoryBitMask == ColliderType.remove.rawValue || contact.bodyB.categoryBitMask == ColliderType.remove.rawValue {
object1.removeFromParent()
// it runs this code as well as code above collision object with object but i want contact remove with object
}
Upvotes: 1
Views: 442
Reputation: 10674
Try to post your full code for this in the future, instead of some random bits thrown together.
Your collision code doesnt make much sense. It should look more like this where you check if player->object and object->remove or vice versa
func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
// This way you only need to do 1 if statement for each collision.
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Player hit Object or Object hit Player
if (firstBody.categoryBitMask == ColliderType.player.rawValue) && (secondBody.categoryBitMask == ColliderType.object.rawValue) {
gameOver == true
}
// Object hit Remove or Remove hit Object
if (firstBody.categoryBitMask == ColliderType.object.rawValue) && (secondBody.categoryBitMask == ColliderType.remove.rawValue) {
// Remove object
firstBody.node?.removeFromParent()
}
}
I suggest you read some more tutorials about sprite kit collision, there are plenty available, if this is confusing to you.
Hope this helps
Upvotes: 1