Trouble making SKSpriteNode bounce off anotherSKSpriteNode using Bit Masks and func didBegin(..)
Here are some brief Code Snippets:
For brevity, let's consider 4 SKSpriteNode's
The Bit Masks
and the .png
Strings are defined within AppDelegate
, along with:
var noCollision: UInt32 = 00000000
var noContact: UInt32 = 00000000
var roomCategory: UInt32 = 00000001
var playerCategory: UInt32 = 00000010
var ballCategory: UInt32 = 00000100
var UCategory: UInt32 = 00001000
Next Snippet appears within GameViewController
which calls .addChild
for each Node:
myRoom = SKSpriteNode(imageNamed: "room.png")
myRoom.size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
myRoom.physicsBody = SKPhysicsBody(rectangleOf: myRoom.size)
myRoom.physicsBody?.categoryBitMask = roomCategory
myRoom.physicsBody?.contactTestBitMask = noContact
myPlayer = SKSpriteNode(imageNamed: "player.png")
myPlayer.size = CGSize(width: playerWidth, height: playerHeight)
myPlayer.physicsBody = SKPhysicsBody(rectangleOf: myPlayer.size)
myPlayer.physicsBody?.categoryBitMask = playerCategory
myPlayer.physicsBody?.collisionBitMask = noCollision
myPlayer.physicsBody?.contactTestBitMask = noContact
myBall = SKSpriteNode(imageNamed: "ball.png")
myBall.size = CGSize(width: ballWidth, height: ballHeight)
myBall.physicsBody = SKPhysicsBody(rectangleOf: myBall.size)
myBall.physicsBody?.categoryBitMask = ballCategory
myBall.physicsBody?.collisionBitMask = noCollision
myBall.physicsBody?.contactTestBitMask = roomCategory | UCategory
myUStake = SKSpriteNode(imageNamed: "ustake.png")
myUStake.size = CGSize(width: U1Width, height: U1Height)
myUStake.physicsBody = SKPhysicsBody(rectangleOf: myU1.size)
myUStake.physicsBody?.categoryBitMask = UCategory
myUStake.physicsBody?.collisionBitMask = noCollision
myUStake.physicsBody?.contactTestBitMask = ballCategory
Finally, here's my func didBegin
func didBegin(_ contact: SKPhysicsContact) {
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
let playerHitBall = ((bodyBName == myPlayer.name) && (bodyAName == myBall.name)) ||
((bodyAName == myPlayer.name) && (bodyBName == myBall.name))
let ballHitU = ((bodyBName == myBall.name) && (bodyAName == myU1.name)) ||
((bodyAName == myBall.name) && (bodyBName == myU1.name))
if playerHitBall {
// moveBallIfHitsWall()
// attaBoy()
print("player hit ball")
}
if ballHitU {
// attaBoy()
// moveBallIfHitsUStake(theUStake: UStakes[0])
print("ball hit U1")
}
} // didBegin
No print statements show in the Debugger.
Anybody that has some light to shine on my error or errors would be greatly appreciated.
Thanks in advance.
Totally messed up the formatting of my Reply to my own Question - My Bad!
No print statements show in the Debugger.
I found my Boo Boos.
Has to do with selecting the correct Bit Mask settings within didBegin.
Said selection is premised on the fact that the Bit Mask is the Category of which objects hit it.
For example:
the Room is hit by the Ball or the Player,
the UStake is hit by just the Ball.
Guess what, the Player is hit by nobody.
Once that set into my addle brain, the flood lights came on:
myRoom.contactTestBitMask = playerCategory | ballCategory
myUStake.contactTestBitMask = ballCategory
myPlayer.contactTestBitMask = noContact
If some Moderator would kindly delete my 1st Reply above, I would be grateful.