GameScene’s didBegin
is not called?
I have read a high volume of solutions here and many, many other Sites and I cannot figure out why?
What am I doing wrong?
Here are my relevant 2 Code Snippets:
Within GameViewController
, this function is called every time I show the CGScene:
var noCollision: UInt32 = 00000000,
noContact: UInt32 = 00000000
func addGamePiecesToScene(toScene: SKScene) {
// thisSceneName is set before we're called
if thisSceneName == "GameScene" {
/*
BACKGROUND
*/
// GameScene.sks file loads this for us, but
// we still need myRoom to set the data below:
myRoom = SKSpriteNode(imageNamed: roomImg)
myRoom.name = "room"
myRoom.zPosition = 0
myRoom.size = CGSize(width: roomWidth, height: roomHeight)
myRoom.physicsBody = SKPhysicsBody(rectangleOf: myRoom.size)
myRoom.physicsBody?.categoryBitMask = roomCategory
myRoom.physicsBody?.collisionBitMask = noCollision
myRoom.physicsBody?.contactTestBitMask = noContact
/*
MAIN Game Pieces
*/
myPaddle = SKSpriteNode(imageNamed: paddleImg)
myPaddle.name = "paddle"
myPaddle.zPosition = 3
myPaddle.size = CGSize(width: paddleWidth, height: paddleHeight)
myPaddle.position = CGPoint(x: paddlePosX, y: paddlePosY) // = original or moved
myPaddle.physicsBody = SKPhysicsBody(rectangleOf: myPaddle.size)
myPaddle.physicsBody?.categoryBitMask = paddleCategory
myPaddle.physicsBody?.collisionBitMask = roomCategory | ballCategory
myPaddle.physicsBody?.contactTestBitMask = roomCategory | ballCategory
myPaddle.physicsBody!.affectedByGravity = false
myPaddle.physicsBody!.isDynamic = true
toScene.addChild(myPaddle)
//
myBall = SKSpriteNode(imageNamed: ballImg)
myBall.name = "ball"
myBall.zPosition = 4
myBall.size = CGSize(width: ballWidth, height: ballHeight)
myBall.position = CGPoint(x: ballPosX, y: ballPosY) // = original or moved
myBall.physicsBody = SKPhysicsBody(rectangleOf: myBall.size)
myBall.physicsBody?.categoryBitMask = ballCategory
myBall.physicsBody?.collisionBitMask = roomCategory
myBall.physicsBody?.contactTestBitMask = roomCategory
myBall.physicsBody!.affectedByGravity = false
myBall.physicsBody!.isDynamic = true
myBall.physicsBody!.usesPreciseCollisionDetection = true
toScene.addChild(myBall)
/*
EXTRA Game Pieces = bird, rock, trees + tower are loaded by the .sks File
*/
} // if thisSceneName == "GameScene"
} // addGamePiecesToScene
Within my GameScene
is the cited didBegin
function:
func didBegin(_ contact: SKPhysicsContact) {
print("didBegin") // never shows in Console
// Respond to an object hitting other objects based on their names
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
// N.B. – according to Apple docs,
// there is no guarantee which object is bodyA and which is bodyB
let paddleHitWall = ((bodyBName == myPaddle.name) && (bodyAName == myRoom.name)) ||
((bodyAName == myPaddle.name) && (bodyBName == myRoom.name))
let ballHitWall = ((bodyBName == myBall.name) && (bodyAName == myRoom.name)) ||
((bodyAName == myBall.name) && (bodyBName == myRoom.name))
let paddleHitBall = ((bodyBName == myPaddle.name) && (bodyAName == myBall.name)) ||
((bodyAName == myPaddle.name) && (bodyBName == myBall.name))
if paddleHitWall {
ouch()
movePaddle(dx: -dPaddleX, dy: -dPaddleY)
print("paddle hit Wall")
}
else if ballHitWall {
moveBall(dx: -dBallX, dy: -dBallY)
print("ball hit Wall")
}
else if paddleHitBall {
attaBoy()
moveBall(dx: -dBallX, dy: -dBallY)
print("paddle hit ball")
}
} // didBegin
What in the above 2 Code Snippets is wrong and, when corrected, didBegin
is once again called?