Didbegincontact Not Being Called Swift

didBeginContact not called

From the docs,

An object that implements the SKPhysicsContactDelegate protocol can respond when two physics bodies with overlapping
contactTestBitMask values are in contact with each other in a physics
world. To receive contact messages, you set the contactDelegate
property of a SKPhysicsWorld object. The delegate is called when a
contact starts or ends.

Consequently, you'll need to adopt the SKPhysicsContactDelegate protocol and set the physicsWorld's contactDelegate property to the SKScene by

class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
}
}

Also, you should remove the ? from the following statements

player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size)

note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size)

didBeginContact not being called Swift

As mentioned above it seems you have misspelled your node names. You are looking for "enemy" in the collision method but named your enemies "enemyBall". Thats why we should always create constants to avoid this e.g.

let enemyName = "Enemy"

and than use it like so

enemy.name = enemyName

You can also try writing your collision method like this, which should make it slightly nicer to read and you only need 1 if statement per collision. Also this way you do not need node names to compare bodies.

func didBegin(_ contact: SKPhysicsContact) {

let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

// Main ball with enemy or enemy with main ball
if (firstBody.categoryBitMask == bitMasks.mainBall) && (secondBody.categoryBitMask == bitMasks.enemy) {
// do something
}
}

I would also try to follow the swift guidlines, classes, enums and structs should start with capital letters.

Hope this helps

iOS Swift didBeginContact not being called

It turned out to be a simple problem. In my original code I was setting parameters for the SKPhysicsBody detection frame like so:

let carBody = SKPhysicsBody(
rectangleOfSize: car.frame.size, center: car.position)

Similarly I was doing the same for the second node that I was testing physics collisions for.

Simply removing the 'centre:' parameters like so:

let carBody = SKPhysicsBody(rectangleOfSize: car.frame.size)

for the two sprite nodes solved the problem and the nodes now crash into each other and push themselves aside as expected.

didBeginContact function not being called

Your definition for didBeginContact() is buried in the touchesEnded() function. If you straighten out your bracing, it might help:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
// for touches loop (skipped)
}
} // Add this closing brace

func didBeginContact(contact: SKPhysicsContact) {
// contact body (skipped)
}

And you'll also need to delete the extra brace after update():

// ...skipped

func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
// } // get rid of this one
}

Hope that helps.

Swift func didBeginContact Not Called

You have an error in this line :

bird.physicsBody?.categoryBitMask = openingGroup | enemeyObjectsGroup

It should be like:

bird.physicsBody?.categoryBitMask = birdGroup

Also try to use didBeginContact like this:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}

//Handle contacts here
}

How you can handle contacts has been commented here a lot of times, so I will skip about that one:

https://stackoverflow.com/a/20604762/3402095

https://stackoverflow.com/a/26723392/3402095

Hint:

Note that contact will not be detected between two static bodies (node.physicsBody.dynamic = false). I am not sure in which contacts are you interested (probably between bird and a crossing/pipe), but you should be aware of this too even if your current setup is fine (bird is dynamic, crossing and pipes are static).

SpriteKit's didBeginContact can't be called

Find out the reason. Need use

s1.physicsBody!.dynamic = true
s2.physicsBody!.dynamic = true

SpriteKit didBeginContact not being called

You haven't added your physics body for coins

SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
coins.name = @"coins";
coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);

coins.physicsBody = [SKPhysicsBody bodyWith...//need code here

coins.physicsBody.categoryBitMask = coinCategory;
coins.physicsBody.dynamic = NO;
coins.physicsBody.collisionBitMask = 0;
SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
[coins runAction:repeatRotate];
[self.world addChild:coins];


Related Topics



Leave a reply



Submit