Swift didBeginContact() isn't called - swift

When I put a println() in my didBeginContact, nothing prints. Therefore, I assume that my didBeginContact doesn't work. Why? I also have an enum (Collider Type) in which rawValues of Cup and Star and listed.
This is my GameScene file.
import Foundation
import SpriteKit
class PlayScene: SKScene, SKPhysicsContactDelegate {
let cup = SKSpriteNode(imageNamed: "cup")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.backgroundColor = UIColor(red: 192, green: 192, blue: 192, alpha: 1)
cup.setScale(0.5)
cup.position = CGPointMake(self.size.width + cup.size.width / 2, self.size.height/2)
self.addChild(cup)
let actualDuration = 2.0
let actualY = size.height / 2
let actionMove = SKAction.moveByX(-550, y: 0, duration: actualDuration)
let reverseActionMove = actionMove.reversedAction()
let sequence = SKAction.sequence([actionMove, reverseActionMove])
let endlessAction = SKAction.repeatActionForever(sequence)
self.cup.physicsBody = SKPhysicsBody(rectangleOfSize: self.cup.size)
self.cup.physicsBody?.dynamic = false
self.cup.physicsBody?.categoryBitMask = ColliderType.Cup.rawValue
self.cup.physicsBody?.collisionBitMask = ColliderType.Star.rawValue
//Code to keep stars in screen
//let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//borderBody.friction = 0
//self.physicsBody = borderBody
cup.runAction(endlessAction)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let star = StarNode.star(touch.locationInNode(self))
star.setScale(0.3)
self.addChild(star)
}
}
func didBeginContact(contact: SKPhysicsContact) {
println("test")
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case ColliderType.Star.rawValue | ColliderType.Cup.rawValue:
println("star with da cup")
default:
return
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
This is my StarNode file.
import Foundation
import UIKit
import SpriteKit
class StarNode: SKSpriteNode, SKPhysicsContactDelegate {
class func star(location: CGPoint) -> StarNode {
let sprite = StarNode(imageNamed:"star")
sprite.xScale = 0.075
sprite.yScale = 0.075
sprite.position = location
sprite.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "star"), size: sprite.size)
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.categoryBitMask = ColliderType.Star.rawValue
sprite.physicsBody?.collisionBitMask = ColliderType.Cup.rawValue
if let physics = sprite.physicsBody {
physics.affectedByGravity = true
physics.friction = 0.12
physics.allowsRotation = true
physics.dynamic = true
physics.linearDamping = 0.75
physics.angularDamping = 0.75
sprite.name = "star"
}
return sprite
}
}

You need to set the contactTestBitMask to your SKSpriteNodes. The collisionBitMask says only which categories of bodies can collide with the node but doesn't call the didBeginContact method.
self.cup.physicsBody?.contactTestBitMask = ColliderType.Star.rawValue
//Star
sprite.physicsBody?.contactTestBitMask = ColliderType.Cup.rawValue
The contactTestBitMask documentation says it:
A mask that defines which categories of bodies cause intersection notifications with this physics body.

Related

Error with SKPhysicsContactDelegate?

I am trying to program a game with Spritekit in Swift. The aim is to escape with his character oncoming rectangles. Now I've made a mistake with the SKPhysicsContactDelegate (didBegin ()) method, so the figure's contact with one of the rectangles is not recognized. Can someone help me find the mistake? This isn't a duplicate because this time I used a different code!
import SpriteKit
struct PhysicsCategory {
static let none : UInt32 = 0
static let all : UInt32 = UInt32.max
static let rechteck : UInt32 = 0b1 // 1
static let figur : UInt32 = 0b10 // 2
}
class PlayScene: SKScene, SKPhysicsContactDelegate{
let figur = SKSpriteNode(imageNamed: "Punkt.jpg")
#objc func addRechteck(){
let rechteckRechts = SKSpriteNode(imageNamed: "Rechteck.gif")
rechteckRechts.physicsBody = SKPhysicsBody(rectangleOf: rechteckRechts.size) // 1
rechteckRechts.physicsBody?.isDynamic = true // 2
rechteckRechts.physicsBody?.categoryBitMask = PhysicsCategory.rechteck // 3
rechteckRechts.physicsBody?.contactTestBitMask = PhysicsCategory.rechteck // 4
rechteckRechts.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
let rechteckLinks = SKSpriteNode(imageNamed: "Rechteck.gif")
rechteckLinks.physicsBody = SKPhysicsBody(rectangleOf: rechteckLinks.size) // 1
rechteckLinks.physicsBody?.isDynamic = true // 2
rechteckLinks.physicsBody?.categoryBitMask = PhysicsCategory.rechteck // 3
rechteckLinks.physicsBody?.contactTestBitMask = PhysicsCategory.rechteck // 4
rechteckLinks.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
let groesse = arc4random_uniform(5)+1
print(groesse)
switch groesse {
case 1:
rechteckLinks.xScale = 0.5
rechteckRechts.xScale = 1.5
case 2:
rechteckLinks.xScale = 1.5
rechteckRechts.xScale = 0.5
case 3:
rechteckLinks.xScale = 1
rechteckRechts.xScale = 1
case 4:
rechteckLinks.xScale = 1.25
rechteckRechts.xScale = 0.75
case 5:
rechteckLinks.xScale = 0.75
rechteckRechts.xScale = 1.25
default:
print("Fehler in der Wahrscheinlichkeit!!!")
}
rechteckRechts.position = CGPoint(x: frame.minX + (rechteckRechts.size.width / 2), y: frame.maxY)
rechteckLinks.position = CGPoint(x: frame.maxX - (rechteckLinks.size.width / 2), y: frame.maxY)
let moveDown = SKAction.moveBy(x: 0, y: -5000, duration: 20.0)
rechteckLinks.run(moveDown)
rechteckRechts.run(moveDown)
self.addChild(rechteckRechts)
self.addChild(rechteckLinks)
}
override func didMove(to view: SKView) {
physicsWorld.gravity = .zero
physicsWorld.contactDelegate = self
figur.xScale = 0.4
figur.yScale = 0.4
figur.position = CGPoint(x: frame.midX, y: frame.maxY / 4)
figur.physicsBody = SKPhysicsBody(rectangleOf: figur.size)
figur.physicsBody?.isDynamic = true
figur.physicsBody?.categoryBitMask = PhysicsCategory.figur
figur.physicsBody?.contactTestBitMask = PhysicsCategory.rechteck
figur.physicsBody?.collisionBitMask = PhysicsCategory.none
figur.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(figur)
self.backgroundColor = SKColor.white
let wait1 = SKAction.wait(forDuration: 3)
let timer = SKAction.repeatForever(SKAction.sequence([wait1, SKAction.run {
self.addRechteck()
}]))
self.run(timer, withKey: "addRechteck")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ){
let location = touch.location(in: self)
if figur.contains(location){
figur.position = location
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ) {
let location = touch.location(in: self)
if figur.contains(location){
figur.position = location
}
}
}
}
func figurDidColissionWithRectangle(figur: SKSpriteNode, rechteck: SKSpriteNode) {
print("Hit")
figur.removeFromParent()
rechteck.removeFromParent()
}
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
// 1
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 2
if ((firstBody.categoryBitMask & PhysicsCategory.rechteck != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.figur != 0)) {
if let rechteck = firstBody.node as? SKSpriteNode,
let figur = secondBody.node as? SKSpriteNode {
figurDidColissionWithRectangle(figur: figur, rechteck: rechteck)
}
}
}
}
In the method addRecktech(), you've told rechteckRechts' physics body to only test for contact with itself:
rechteckRechts.physicsBody?.contactTestBitMask = PhysicsCategory.rechteck //4
I suggest changing this line of code to the following, to tell it to test for contact with figur:
rechteckRechts.physicsBody?.contactTestBitMask = PhysicsCategory.figur //4
I believe because there's only a 'one-way' test for contact between rechTeck and figur in your current code, some contact events are being missed.

In SpriteKit SKPhysicsContactDelegate contact is not detected

I am trying to program a game with Spritekit in Swift. The aim is to escape with his character oncoming rectangles. Now I've made a mistake with the SKPhysicsContactDelegate (didBegin ()) method, so the figure's contact with one of the rectangles is not recognized. Can someone help me find the mistake?
import SpriteKit
enum bodyType: UInt32 {
case rechteckRechts = 1
case rechteckLinks = 2
case figur = 4
}
class PlayScene: SKScene, SKPhysicsContactDelegate {
let figur = SKSpriteNode(imageNamed: "Punkt.jpg")
#objc func addRechteck(){
let rechteckRechts = SKSpriteNode(imageNamed: "Rechteck.gif")
rechteckRechts.physicsBody = SKPhysicsBody()
rechteckRechts.physicsBody?.isDynamic = false
rechteckRechts.physicsBody?.categoryBitMask = bodyType.rechteckRechts.rawValue
let rechteckLinks = SKSpriteNode(imageNamed: "Rechteck.gif")
rechteckLinks.physicsBody = SKPhysicsBody()
rechteckLinks.physicsBody?.isDynamic = false
rechteckLinks.physicsBody?.categoryBitMask = bodyType.rechteckLinks.rawValue
let groesse = arc4random_uniform(5)+1
print(groesse)
switch groesse {
case 1:
rechteckLinks.xScale = 0.5
rechteckRechts.xScale = 1.5
case 2:
rechteckLinks.xScale = 1.5
rechteckRechts.xScale = 0.5
case 3:
rechteckLinks.xScale = 1
rechteckRechts.xScale = 1
case 4:
rechteckLinks.xScale = 1.25
rechteckRechts.xScale = 0.75
case 5:
rechteckLinks.xScale = 0.75
rechteckRechts.xScale = 1.25
default:
print("Fehler in der Wahrscheinlichkeit!!!")
}
rechteckRechts.position = CGPoint(x: frame.minX + (rechteckRechts.size.width / 2), y: frame.maxY)
rechteckLinks.position = CGPoint(x: frame.maxX - (rechteckLinks.size.width / 2), y: frame.maxY)
let moveDown = SKAction.moveBy(x: 0, y: -5000, duration: 20.0)
rechteckLinks.run(moveDown)
rechteckRechts.run(moveDown)
self.addChild(rechteckRechts)
self.addChild(rechteckLinks)
}
override func didMove(to view: SKView) {
physicsWorld.gravity = .zero
physicsWorld.contactDelegate = self
figur.xScale = 0.4
figur.yScale = 0.4
figur.position = CGPoint(x: frame.midX, y: frame.maxY / 4)
figur.physicsBody = SKPhysicsBody()
figur.physicsBody?.isDynamic = false
figur.physicsBody?.categoryBitMask = bodyType.figur.rawValue
self.addChild(figur)
self.backgroundColor = SKColor.white
let wait1 = SKAction.wait(forDuration: 3)
let timer = SKAction.repeatForever(SKAction.sequence([wait1, SKAction.run {
self.addRechteck()
}]))
self.run(timer, withKey: "addRechteck")
}
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case bodyType.figur.rawValue | bodyType.rechteckLinks.rawValue:
print("contact")
case bodyType.figur.rawValue | bodyType.rechteckRechts.rawValue:
print("contact")
default:
return
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ){
let location = touch.location(in: self)
if figur.contains(location){
figur.position = location
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ) {
let location = touch.location(in: self)
if figur.contains(location){
figur.position = location
}
}
}
}
You don’t appear to have set the collision and contactTest bit masks. These are properties of the physics body. The collisionBitMask controls which objects bounce off which other objects. By default, everything bounces off everything else.
The contactTestBitMask is the important one in this siutuation, as it controls which objects invoke ‘didBegin‘ when contacting other objects. By default, this is 0 which means that no contacts are registered. See my answer here : https://stackoverflow.com/a/40596890/1430420 for a detailed explanation.
In your specific example, you appear to want to know when figur makes contact with either rechteckLinks or rechteckRechts, so you would want to following contactTestBitMasks:
figur.physicsBody!.cagegoryBitMask = bodyType.figur
figur.physicsBody!.contactTestBitMask = bodyType.rechteckRechts | bodyType.rechteckLinks
Edit: correcting the definition of figurs physics body:
We change the ? to ! so that we get a crash should the physics body not exist. We also change the isDynamic property to true as physics bodies that are not dynamic cannot register contacts (I think). It’s ok for the other 2 to not be dynamic. We assign the collisionTestBitMask to the value of all the categories we want to be notified of collision with logically ‘OR’ed together:
figur.physicsBody!.isDynamic = true
figur.physicsBody!.categoryBitMask = bodyType.figur.rawValue
figur.physicsBody!.contactTestBitMask = bodyType.rechteckRechts.rawValue | bodyType.rechteckLinks.rawValue

sprite kit collision not working

This is a game I have been working on. In summary there is a moving block named enemy and I want it to collide with an invisible static block called invisibleGround2. I have it printing hit when they supposedly collide but they are not colliding. Ive read every swift collision documentation by apply and others out there and I dont know whats wrong. Any help would be much appreciated!
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var orb = SKSpriteNode(imageNamed: "orb")
var scrollingG:scrollingGround?
var invisibleGround = SKSpriteNode(imageNamed: "invisible")
var invisibleGround2 = SKSpriteNode(imageNamed: "invisible")
let enemies = [SKSpriteNode(imageNamed: "blueE.png"), SKSpriteNode(imageNamed: "redE.png")]
let enemyCategory : UInt32 = 1
let jumperCategory : UInt32 = 1
let rotateDuration = 2
let enemyMoveSpeed = 5
let groundScrollingSpeed = 5
func createOrb(){
let orbConst = frame.size.width/2
let xConstraint = SKConstraint.positionX(SKRange(constantValue: orbConst))
orb.position = CGPoint(x: frame.size.width/2, y: 480)
orb.physicsBody = SKPhysicsBody(texture: orb.texture!, size: orb.texture!.size())
orb.constraints = [xConstraint]
self.addChild(orb)
}
func createScrollingGround () {
scrollingG = scrollingGround.scrollingNodeWithImage(imageName: "ground", containerWidth: self.size.width)
scrollingG?.scrollingSpeed = CGFloat(groundScrollingSpeed)
scrollingG?.anchorPoint = .zero
self.addChild(scrollingG!)
}
func createGround(){
invisibleGround2.size.width = 1
invisibleGround2.size.height = 1
invisibleGround2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width:1, height: 100))
invisibleGround2.physicsBody?.isDynamic = false
invisibleGround2.position = CGPoint(x: 530, y: 191)
invisibleGround2.physicsBody?.categoryBitMask = jumperCategory
invisibleGround2.physicsBody?.collisionBitMask = enemyCategory
invisibleGround2.physicsBody?.contactTestBitMask = enemyCategory
invisibleGround2.name = "jumper"
invisibleGround.position = CGPoint(x: 0, y: 190)
invisibleGround.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width * 3, height: 10))
invisibleGround.physicsBody?.isDynamic = false
self.addChild(invisibleGround2)
self.addChild(invisibleGround)
}
func getRandomEnemy(fromArray array:[SKSpriteNode])->SKSpriteNode{
return array[Int(arc4random_uniform(UInt32(array.count)))]
}
func spawnEnemy() {
if let enemy = getRandomEnemy(fromArray: enemies).copy() as? SKSpriteNode {
enemy.position = CGPoint(x: frame.size.width + frame.size.width/3, y: 440)
enemy.physicsBody = SKPhysicsBody(texture: enemy.texture!, size: enemy.texture!.size())
if enemy.size.width < 95 {
enemy.physicsBody?.categoryBitMask = enemyCategory
enemy.physicsBody?.collisionBitMask = jumperCategory
enemy.physicsBody?.contactTestBitMask = jumperCategory
}
enemy.name = "enemy"
self.addChild(enemy)
let moveLeft = SKAction.moveBy(x: -1500, y: 0, duration: TimeInterval(enemyMoveSpeed))
enemy.run(moveLeft)
}
}
func addEnemies () {
self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.spawnEnemy()
}, SKAction.wait(forDuration: 4)])))
}
func jump() {
orb.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 335))
}
func rotate() {
let rotate = SKAction.rotate(byAngle: CGFloat(M_PI * -2.55), duration: TimeInterval(rotateDuration))
let repeatAction = SKAction.repeatForever(rotate)
orb.run(repeatAction)
}
override func didMove(to view: SKView) {
createScrollingGround()
createOrb()
createGround()
rotate()
addEnemies()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
jump()
}
override func update(_ currentTime: TimeInterval) {
if self.scrollingG != nil {
scrollingG?.update(currentTime: currentTime)
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.categoryBitMask
let bodyB = contact.bodyB.categoryBitMask
if bodyA == jumperCategory && bodyB == enemyCategory {
print("hit")
} else if bodyA == enemyCategory && bodyB == jumperCategory {
print("hit 2")
}
}
}
}
}
Not an swer, but some things to check:
Have you set the scene’s physicsworld delegate property set to
self?
Move didBegin outside of update
Is the 'enemy' sprite's width < 95?
Add a print("Contact detected") as the first line of your relocated didBegin so you at least know that some contact has been detected.
See my answer here https://stackoverflow.com/a/43605825/1430420 for a simple SK collision demo which might help - I think it needs updates to Swift 4 which I'll try and do.

Why my contact recogniser doesn't work

I made contact recogniser using physics body, category bit mask and SKPhysics contact. Unfortunately it do not work. Could You help me somehow to find mistake?
This code do not print statement Touched.
Is it because one body is SKSpriteNode and second SKShapeNode? Could we recognise contact between SpriteNode and ShapeNode?
I made all improvements like #giorashc said with SKPhysics body, but in not worked.
import Foundation
import SpriteKit
import GameplayKit
import UIKit
var TouchLevel3BadgeChecker = 0
enum BodyType:UInt32{
case TheCircle = 1
case TheEightBack = 2
}
class TouchLevel3SceneClass: SKScene, SKPhysicsContactDelegate{
var Eight = SKSpriteNode(imageNamed: "8.png")
var EightBack = SKSpriteNode(imageNamed: "8Back.png")
var counter : Int = 0;
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
Eight.position = CGPoint(x: 0, y: -50)
Eight.name = "Eight"
addChild(Eight)
EightBack.position = CGPoint(x: 0, y: -50)
EightBack.name = "EightBack"
EightBack.zPosition = 2
EightBack.physicsBody? = SKPhysicsBody(texture: EightBack.texture!,
size: EightBack.texture!.size())
EightBack.physicsBody?.usesPreciseCollisionDetection = true
EightBack.physicsBody?.isDynamic = true
EightBack.physicsBody?.affectedByGravity = false
EightBack.physicsBody?.categoryBitMask = BodyType.TheEightBack.rawValue
addChild(EightBack)
Circle.position = CGPoint(x: 0, y: -25)
Circle.zPosition = 2;
Circle.fillColor = SKColor.black;
Circle.name = "Circle"
Circle.physicsBody? = SKPhysicsBody(circleOfRadius: 30)
Circle.physicsBody?.usesPreciseCollisionDetection = true
Circle.physicsBody?.isDynamic = true
Circle.physicsBody?.affectedByGravity = false
Circle.physicsBody?.categoryBitMask = BodyType.TheCircle.rawValue
Circle.physicsBody?.collisionBitMask = BodyType.TheEightBack.rawValue
Circle.physicsBody?.contactTestBitMask = BodyType.TheEightBack.rawValue
addChild(Circle)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches{
let location = t.location(in: self);
if atPoint(location).name == "Circle"{
Circle.position.x = location.x
Circle.position.y = location.y
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyB.categoryBitMask == BodyType.TheEightBack.rawValue{
print("Touched")
} else if contact.bodyB.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyA.categoryBitMask == BodyType.TheEightBack.rawValue{
print("Touched")
}
}
}
You are not creating the physicsBody property. Since its optional and nil setting its properties won't do anything.
create the physicsBody property with:
EightBack.physicsBody = SKPhysicsBody(...);
Circle.physicsBody = SKPhysicsBody(...);
check the example in this link: https://developer.apple.com/documentation/spritekit/skphysicsbody

Collision Detection In Sprite Kit using Swift

The code below is from a project I am currently working on, I have been trying to teach myself Swift language and Sprite Kit for the past few days and this is my first attempt at a game, it is a Flappy Bird type game. I ran into a problem today when I was trying to write the code for the collision detection. When the bird touches one of the pipes the game is supposed to pause. However when I run the code and the bird touches the pipe, nothing happens, the bird just bounces off of it. I have read many tutorials and watched many videos on this subject to try and resolve my problem and haven't had any luck. I have written all of the collision detection code that I learned off of the last video I watched in the code below. Could anyone please tell me what I am doing wrong. Any advice would be greatly appreciated, thank you.
//
// GameScene.swift
// Bird Flappy Game
//
// Created by Brandon Ballard on 1/4/15.
// Copyright (c) 2015 Brandon Ballard. All rights reserved.
//
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
let pipeGap = 150.0
enum ColliderType:UInt32 {
case BIRD = 1
case PIPE = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.cyanColor()
//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
self.physicsWorld.contactDelegate = self
func didBeginContact(contact: SKPhysicsContactDelegate) {
scene?.view?.paused = true
bird.setScale(12.0)
}
//Bird
var birdTexture = SKTexture(imageNamed:"Bird")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird = SKSpriteNode(texture: birdTexture)
bird.setScale(0.6)
bird.position = CGPoint(x: 375, y: self.frame.size.height * 0.6)
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = true
bird.physicsBody?.affectedByGravity = true
bird.physicsBody!.categoryBitMask = ColliderType.BIRD.rawValue
bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
bird.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
self.addChild(bird)
//Ground
var groundTexture = SKTexture(imageNamed: "Ground")
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)
self.addChild(sprite)
var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height + 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
ground.physicsBody?.dynamic = false
self.addChild(ground)
//Pipes
//Create the Pipes
pipeUpTexture = SKTexture(imageNamed: "PipeUp")
pipeDownTexture = SKTexture(imageNamed: "PipeDown")
//Movement of Pipes
let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removePipes = SKAction.removeFromParent()
pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])
//Spawn Pipes
let spawn = SKAction.runBlock({() in self.spawnPipes()})
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawnThenDelay = SKAction.sequence([spawn,delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
}
func spawnPipes() {
let pipePair = SKNode()
pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
pipePair.zPosition = -10
let height = UInt32(self.frame.size.height / 4)
let y = arc4random() % height + height
var pipeDown = SKSpriteNode(texture: pipeDownTexture)
pipeDown.setScale(2.0)////////
pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )
pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
pipeDown.physicsBody?.dynamic = false
pipeDown.physicsBody!.affectedByGravity = false
pipeDown.physicsBody!.categoryBitMask = ColliderType.PIPE.rawValue
pipeDown.physicsBody!.contactTestBitMask = ColliderType.BIRD.rawValue
pipeDown.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
pipePair.addChild(pipeDown)
var pipeUp = SKSpriteNode(texture: pipeUpTexture)
pipeUp.setScale(2.0)
pipeUp.position = CGPointMake(0.0, CGFloat(y))
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
pipeUp.physicsBody?.dynamic = false
pipeUp.physicsBody!.affectedByGravity = false
pipeUp.physicsBody!.categoryBitMask = ColliderType.PIPE.rawValue
pipeUp.physicsBody!.contactTestBitMask = ColliderType.BIRD.rawValue
pipeUp.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
pipePair.addChild(pipeUp)
pipePair.runAction(pipesMoveAndRemove)
self.addChild(pipePair)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
bird.physicsBody?.applyImpulse(CGVectorMake(0,25))
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
From what I can see, you only need to DETECT collisions, not actually simulate them. For this, you need to set only the contactTestBitMask of the physicsBodies. You can set the collisionBitMask as 0.
bird.physicsBody!.collisionBitMask = 0
pipe.physicsBody!.collisionBitMask = 0
Also, as hamobi has already said, the didBeginContact method needs to be outside the didMoveToView method with the override keyword. (This question has the exact same problem as yours)
class GameScene: SKScene, SKPhysicsContactDelegate {
// ...
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
// set as delegate:
self.physicsWorld.contactDelegate = self
// ..
}
// should be called now
func didBeginContact(contact: SKPhysicsContact){
scene?.view?.paused = true
bird.setScale(12.0)
}
}
You put your didBeginContact INSIDE of didMoveToView. It's not callable from there. Put it in the body of your class