Below is my class "Floor.swift", below which is basically a bunch of walls. I have objects coming out of the top of the screen, and as soon as the Floor column and SKSpriteNodes collide, I would like to remove SKSpriteNode. Below is my Floor class.
import Foundation
import SpriteKit
class Floor: SKNode {
override init() {
super.init()
let leftWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 5, height: 50))
leftWall.position = CGPoint(x: 0, y: 50)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 5, height: 50))
rightWall.position = CGPoint(x: 375, y: 50)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)
let bottomWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 500, height: 10))
bottomWall.position = CGPoint(x: 150, y: -5)
bottomWall.physicsBody = SKPhysicsBody(rectangleOf: bottomWall.size)
bottomWall.physicsBody!.isDynamic = false
self.addChild(bottomWall)
self.physicsBody = SKPhysicsBody(bodies: [leftWall.physicsBody!, rightWall.physicsBody!, bottomWall.physicsBody!])
self.physicsBody?.categoryBitMask = floorCategory
self.physicsBody?.contactTestBitMask = nailDropCategory | pointCategory | lifeCategory
self.physicsBody?.collisionBitMask = balloonCategory
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemted")
}
}
The GameScene class under "func didBegin (_ contact: SKPhysicsContact)" I wrote:
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == nailDropCategory | pointCategory | lifeCategory) && (contact.bodyB.categoryBitMask == floorCategory) {
print("COLLISION")
}
}
As you can see, in my Floor class, I set the object as "self.physicsBody = SKPhysicsBody (body :)" with all my SKSpriteNode. But for some reason, I did not find that it was so. I made my class "Floor" "contactTestBitMask" for each object class objectCategory, pointCategory, lifeCategory. Is this a question that Iām stuck for a minute, any ideas?
Update:
, , ! , "balloonCategory", , . , ?
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case balloonCategory | nailDropCategory:
print("nailDrop and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
self.run(SKAction.playSoundFileNamed("lostlifesound.mp3", waitForCompletion:false))
lifeLost -= 1
case balloonCategory | pointCategory:
print("point and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
totalPoints += 2
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
case balloonCategory | lifeCategory:
print("life and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
lifeLost += 1
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
default:
contact.bodyB.node!.removeFromParent()
print("Removed \(String(describing: contact.bodyB.node!.name))")
}
}