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.
1
0
522