Swift SpriteKit Physics Collision Issue - swift

I am making a simple physics based game. Everything is working normally with exception to collision detection. It feels like the didBeginContact method is being ignored.
I have tried several ways of configuring the "PhysicsCategory" struct (even using enum) and several formations of the bodyA/bodyB contact statements.
I am all out of ideas. I can get the 2 objects to collide but they just bounce off each other. There are no errors and nothing logged to the console. I hope that I have made a trivial mistake that I am overlooking.
Below is all the pertinent code. In case it matters... setupPhysics() is being called in didMoveToView
PhysicsCategory Struct
struct PhysicsCategory {
static let None: UInt32 = 0
static let Fish: UInt32 = 0b1
static let Bird: UInt32 = 0b10
static let BottomEdge: UInt32 = 0b100}
Physics Setup Method
//MARK: - Physics Methods
func setupPhysics() {
/* Physics World */
physicsWorld.gravity = CGVectorMake(0, -9.8)
physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
/* Bottom Collision Rect */
let bEdge = CGRect(x: CGPointZero.x, y: CGPointZero.y, width: size.width, height: size.height * 0.005)
let bottomEdge = SKShapeNode(rect: bEdge)
bottomEdge.physicsBody = SKPhysicsBody(edgeLoopFromRect: bEdge)
bottomEdge.physicsBody!.categoryBitMask = PhysicsCategory.BottomEdge
bottomEdge.physicsBody!.collisionBitMask = PhysicsCategory.Fish
bottomEdge.physicsBody!.dynamic = false
gameLayer.addChild(bottomEdge)
/* Fish */
fish.physicsBody = SKPhysicsBody(circleOfRadius: blowfish.size.height / 2.1)
fish.physicsBody!.allowsRotation = false
fish.physicsBody!.categoryBitMask = PhysicsCategory.Fish
fish.physicsBody!.collisionBitMask = PhysicsCategory.Bird | PhysicsCategory.BottomEdge
/* Left Random Bird */
randomLeftBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomLeftBird.size)
randomLeftBird.physicsBody!.affectedByGravity = false
randomLeftBird.physicsBody!.allowsRotation = false
randomLeftBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
randomLeftBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish
/* Random Right Bird */
randomRightBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomRightBird.size)
randomRightBird.physicsBody!.affectedByGravity = false
randomRightBird.physicsBody!.allowsRotation = false
randomRightBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
randomRightBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish
}
didBeginContact Setup
func didBeginContact(contact: SKPhysicsContact!) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Fish | PhysicsCategory.Bird {
println("COLLISON WITH BIRD!")
updateLives()
} else if collision == PhysicsCategory.Fish | PhysicsCategory.BottomEdge {
println("EPIC FAIL!")
}
}

I don't see any use of contactTestBitMask in your code. That one controls whether you get contact delegate messages — collisionBitMask just controls whether they collide (bounce off).
These are separate so that you can get contact delegate messages even for categories that don't bounce off each other, but it means you also can have collisions that don't send messages. (That can be a good thing if you don't want game logic for every kind of collision.) Any that you do want contact messages for you need to explicitly request.

Related

Inconsistent contact detection in Swift 3 using SpriteKit

I'm having an issue with contact detection in Swift 3 using SpriteKit. The contact detection is working...sometimes. It seems purely random as to when it fires and when it doesn't.
I have a yellow "bullet" that moves up on the screen to hit a red sprite named targetSprite. The desired behavior is to have the bullet removed when it hits the target, but sometimes it just passes through underneath.
I've found many questions about contact detection not working at all, but I haven't found any dealing with inconsistent detection.
What can I do to fix this?
Here's the code:
import SpriteKit
import GameplayKit
enum PhysicsCategory:UInt32 {
case bullet = 1
case sprite1 = 2
case targetSprite = 4
// each new value should double the previous
}
class GameScene: SKScene, SKPhysicsContactDelegate {
// Create sprites
let sprite1 = SKSpriteNode(color: SKColor.blue, size: CGSize(width:100,height:100))
let targetSprite = SKSpriteNode(color: SKColor.red, size: CGSize(width:100,height:100))
let bullet = SKSpriteNode(color: SKColor.yellow, size: CGSize(width: 20, height: 20))
// show the bullet?
var isShowingBullet = true
// Timers
//var timer:Timer? = nil
var fireBulletTimer:Timer? = nil
// set up bullet removal:
var bulletShouldBeRemoved = false
let bulletMask = PhysicsCategory.bullet.rawValue
override func didMove(to view: SKView) {
// Physics
targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.centerRect.size)
targetSprite.physicsBody?.affectedByGravity = false
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.centerRect.size)
bullet.physicsBody?.affectedByGravity = false
// Contact Detection:
targetSprite.physicsBody?.categoryBitMask = PhysicsCategory.targetSprite.rawValue
targetSprite.physicsBody?.contactTestBitMask =
//PhysicsCategory.sprite1.rawValue |
PhysicsCategory.bullet.rawValue
targetSprite.physicsBody?.collisionBitMask = 0 // no collision detection
// bullet physics
bullet.physicsBody?.categoryBitMask = PhysicsCategory.bullet.rawValue
bullet.physicsBody?.contactTestBitMask =
PhysicsCategory.targetSprite.rawValue
bullet.physicsBody?.collisionBitMask = 0 // no collision detection
// execute once:
fireBulletTimer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.fireBullet),
userInfo: nil,
repeats: false)
// Add sprites to the scene:
self.addChild(sprite1)
self.addChild(bullet)
self.addChild(targetSprite)
// Positioning
targetSprite.position = CGPoint(x:0, y:300)
// Note: bullet and sprite1 are at 0,0 by default
// Delegate
self.physicsWorld.contactDelegate = self
}
func didBegin(_ contact: SKPhysicsContact) {
print("didBegin(contact:))")
//let firstBody:SKPhysicsBody
// let otherBody:SKPhysicsBody
// Use 'bitwise and' to see if both bits are 1:
if contact.bodyA.categoryBitMask & bulletMask > 0 {
//firstBody = contact.bodyA
//otherBody = contact.bodyB
print("if contact.bodyA....")
bulletShouldBeRemoved = true
}
else {
//firstBody = contact.bodyB
//otherBody = contact.bodyA
print("else - if not contacted?")
}
/*
// Find the type of contact:
switch otherBody.categoryBitMask {
case PhysicsCategory.targetSprite.rawValue: print(" targetSprite hit")
case PhysicsCategory.sprite1.rawValue: print(" sprite1 hit")
case PhysicsCategory.bullet.rawValue: print(" bullet hit")
default: print(" Contact with no game logic")
}
*/
} // end didBegin()
func didEnd(_ contact: SKPhysicsContact) {
print("didEnd()")
}
func fireBullet() {
let fireBulletAction = SKAction.move(to: CGPoint(x:0,y:500), duration: 1)
bullet.run(fireBulletAction)
}
func showBullet() {
// Toggle to display or not, every 1 second:
if isShowingBullet == true {
// remove (hide) it:
bullet.removeFromParent()
// set up the toggle for the next call:
isShowingBullet = false
// debug:
print("if")
}
else {
// show it again:
self.addChild(bullet)
// set up the toggle for the next call:
isShowingBullet = true
// debug:
print("else")
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if bulletShouldBeRemoved {
bullet.removeFromParent()
}
}
}
Sorry for the inconsistent indentation, I can't seem to find an easy way to do this...
EDIT:
I have found that using 'frame' instead of 'centerRect' makes the collision area the size of the sprite. For example:
targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.centerRect.size)
should be:
targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.frame.size)
First advice - Do not use NSTimer (aka Timer) in SpriteKit. It is not paired with a game loop and can cause different issues in a different situations. Read more here ( answer posted by LearnCocos2D)
So, do this:
let wait = SKAction.wait(forDuration: 1)
run(wait, completion: {
[unowned self] in
self.fireBullet()
})
What I have noticed is that if I run your code in Simulator, I get the behaviour you have described. didBegin(contact:) is being fired randomly. Still, this is not happening on a device for me, and device testing is what matters.
Now, when I have removed Timer and did the same thing with SKAction(s) everything worked, means contact were detected every time.
Have you tried adding
.physicsBody?.isDynamic = true
.physicsBody?.usesPreciseCollisionDetrction =true
SpriteKit physics engine will calculate collision correctly if you do following:
1) set "usesPreciseCollisionDetection" property to true for bullet's physics body. This will change collision detection algorithm for this body. You can found more information about this property here, chapter "Working with Collisions and Contacts".
2) move your bullet using applyImpulse or applyForce methods. Collision detection will not woking correctly if you move body by changing it's position manually. You can find more information here, chapter "Making Physics Bodies Move".

How to check if a SpriteNode touches another SpriteNote?

So what I'm trying to do is a player which is able to jump when he's on the ground but when he's in the air he is not. And to be honest I have no idea how to accomplish this because I'm new to stuff like that.
I would have an instance variable in the player class like var isOnGround: Bool = true. I would then set up a physics body for the player and the ground using SKPhysicsBody. Then check for collisions in the didBegin(...) and didEnd(...) (contact methods) for the player and the ground. Set the isOnGround variable to true in didBegin(...) and set it to false in the didEnd(...) methods.
Here's an example of the different bit masks properties:
let playerCollisionNumber: UInt32 = 0x1 << 0
let groundCollisionNumber: UInt32 = 0x1 << 1
//node's own collision number
player.physicsBody.categoryBitMask = playerCollisionNumber
//makes the player's physics body interact/collide with the ground's physics body
player.physicsBody.collisionBitMask = groundCollisionNumber
//setting this allows you to be able to interject your own code when the player's physics body and the ground's physics body interact (in the didBegin and didEnd methods)
player.physicsBody.contactTestBitMask = groundCollisionNumber
Then do the same for the ground:
groundNode.physicsBody.categoryBitMask = groundCollisionNumber
groundNode.physicsBody.collisionBitMask = playerCollisionNumber
groundNode.physicsBody.categoryBitMask = playerCollisionNumber
You'll set the isOnGround variable in the didBegin() and didEnd() methods like this:
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
//in didBegin()
if contactMask == (playerCollisionNumber | groundCollisionNumber) {
player.isOnGround = true
}
//in didEnd()
if contactMask == (playerCollisionNumber | groundCollisionNumber) {
player.isOnGround = false
}

Change texture of individual nodes in didBeginContact

I've created a simple game where I have a match hover over candles (the odd description lends itself to my question) and the player scores a point when the match comes in contact with the wick. However, if it comes into contact with the anything else (like the 'wax' part of the candle), the game is over. The player controls the match by tapping on the screen.
My candle, being the wick and the coloured part, is created as follows (I have removed irrelevant parts, like the series of random textures):
func makeCandles() {
//Node properties and randomisation
let candle = SKNode()
let randomCandle = Int(arc4random_uniform(UInt32(candleTexture.count)))
let randomTexture = candleTexture[randomCandle] as SKTexture
let random = arc4random_uniform(17)
candle.position = CGPoint(x: self.frame.size.width, y: CGFloat(random * 12) - 120)
//Candle
let chosenCandle = SKSpriteNode(texture: randomTexture)
chosenCandle.position = CGPoint(x: 0, y: self.frame.size.height / 2)
chosenCandle.physicsBody = SKPhysicsBody(rectangleOfSize: chosenCandle.size)
chosenCandle.physicsBody?.dynamic = false
chosenCandle.physicsBody?.categoryBitMask = self.candleCategory
chosenCandle.physicsBody?.contactTestBitMask = self.matchCategory
chosenCandle.physicsBody?.collisionBitMask = 0
chosenCandle.physicsBody?.restitution = 0
candle.addChild(chosenCandle)
//Wick
let wickArea = SKSpriteNode(texture: wickTexture)
wickArea.name = "wickNode"
wickArea.position = CGPoint(x: 0, y: self.frame.size.height / 1.3)
wickArea.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: wickArea.size.width / 4, height: wickArea.size.height))
wickArea.physicsBody?.dynamic = false
wickArea.physicsBody?.categoryBitMask = self.wickCategory
wickArea.physicsBody?.contactTestBitMask = self.matchCategory
wickArea.physicsBody?.collisionBitMask = 0
wickArea.zPosition = 11
wickArea.physicsBody?.restitution = 0
candle.addChild(wickArea)
//Add the node and zPosition
self.partsMoving.addChild(candle)
chosenCandle.zPosition = 12
}
The candles are then created in a runBlock:
let createCandles = SKAction.runBlock({() in self.makeCandles()})
let briefPause = SKAction.waitForDuration(averageDelay, withRange: randomDelay)
let createAndPause = SKAction.sequence([createCandles, briefPause])
let createAndPauseForever = SKAction.repeatActionForever(createAndPause)
self.runAction(createAndPauseForever)
This is my function that changes the texture which is called in didBeginContact:
func updateFlame() {
if let newNode: SKNode = self.childNodeWithName("//wickNode") {
let updateTexture = SKAction.setTexture(flameTexture, resize: true)
newNode.runAction(updateTexture)
}
}
This is my didBeginContact function:
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == wickCategory || contact.bodyB.categoryBitMask == wickCategory {
score += 1
scoreLabel.text = "\(score)"
updateFlame()
} else {
runGameOverScene()
}
My problem is that it only changes the first node to a flame, and doesn't change any others. Even if it is the second or third wick on which contact is detected, only the first created wick is changed (the first one that comes across the screen). I know that contact is being detected on each node and that that works fine, because the score updates every time the match comes into contact with a wick.
What am I doing wrong that is stopping the texture of each node that individually comes into contact with the match from changing? Everything else is working just fine, but this part has had me beat for a week and everything I've tried doesn't work. This is the closest I've gotten.
After much trial and error, I have finally figured out how to make each node change texture when contact occurs! This is my code for that part:
func didBeginContact(contact: SKPhysicsContact) {
let collision : UInt32 = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask)
if collision == (matchCategory | candleCategory | cakeCategory) {
runGameOverScene()
}
if (contact.bodyA.categoryBitMask == wickCategory) {
let newWick = contact.bodyA.node
let updateTexture = SKAction.setTexture(flameTexture, resize: true)
newWick!.runAction(updateTexture)
} else if (contact.bodyB.categoryBitMask == wickCategory) {
let newWick = contact.bodyB.node
let updateTexture = SKAction.setTexture(flameTexture, resize: true)
newWick!.runAction(updateTexture)
}
}
I followed the logic of this question (even though I wanted to set the texture, not remove it) and it worked perfectly: removeFromParent() Doesn't Work in SpriteKit.

Swift SpriteKit basic Contact / Collision

The problem: When the coin spawns, it's physicsBody appears right underneath the spriteNode. Also, when the player comes in contact with the coin's physicsBody, the player bounces off of the physicsBody and the game ends.
What the output should be: The coin's physisBody should be aligned properly with the coin spriteNode. When the player comes in contact with the coin, the coin should disappear and +1 should be added to the proper label.
The current code:
struct ColliderType {
static let playerCategory: UInt32 = 0x1 << 0
static let boundary: UInt32 = 0x1 << 1
​
​static let coinCategory: UInt32 = 0x1 << 2
​
​static let bodyA: UInt32 = 0x1 << 4
​
​static let bodyB: UInt32 = 0x1 << 8
}
override func didMoveToView(view: SKView) {
var coinInt = 0
​
​
​
​self.physicsWorld.gravity = CGVectorMake(0.0, -7.0)
physicsWorld.contactDelegate = self
player = SKSpriteNode(imageNamed: "player")
player.zPosition = 1
player.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 5.12)
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
self.addChild(player)
generateCoins()
​
​
coin = SKSpriteNode( imageNamed: "coin")
coin.physicsBody? = SKPhysicsBody(circleOfRadius: coin.size.height / 10)
coin.physicsBody?.dynamic = false
coin.physicsBody?.allowsRotation = false
coin.zPosition = 1
​self.addChild(coin)
​
player.physicsBody?.categoryBitMask = ColliderType.playerCategory
​player.physicsBody?.contactTestBitMask = ColliderType.boundary
player.physicsBody?.collisionBitMask = ColliderType.coinCategory | ColliderType.boundary
coin.physicsBody?.categoryBitMask = ColliderType.coinCategory
coin.physicsBody?.contactTestBitMask = ColliderType.playerCategory
coin.physicsBody?.collisionBitMask = ColliderType.playerCategory
func didPlayerCollideWithCoin(player: SKSpriteNode, coin: SKSpriteNode) {
self.coin.removeFromParent()
self.coin += 1
coinLabel.text = "\(coinInt)"
}
​
​
​
​func generateCoins() {
if(self.actionForKey("spawning") != nil){return}
let coinTimer = SKAction.waitForDuration(7, withRange: 2)
let spawnCoin = SKAction.runBlock {
self.coin = SKSpriteNode( imageNamed: "coin")
self.coin.physicsBody = SKPhysicsBody(circleOfRadius: self.coin.size.height / 10)
self.coin.name = "coin"
self.coin.physicsBody?.dynamic = false
self.coin.physicsBody?.allowsRotation = false
var coinPosition = Array<CGPoint>()
coinPosition.append((CGPoint(x:340, y:103)))
coinPosition.append((CGPoint(x:340, y:148)))
coinPosition.append((CGPoint(x:340, y:218)))
coinPosition.append((CGPoint(x: 340, y:343)))
let spawnLocation = coinPosition[Int(arc4random_uniform(UInt32(coinPosition.count)))]
let action = SKAction.repeatActionForever(SKAction.moveToX(+self.xScale, duration: 4.4))
self.coin.runAction(action)
self.coin.position = spawnLocation
self.addChild(self.coin)
print(spawnLocation)
}
let sequence = SKAction.sequence([coinTimer, spawnCoin])
self.runAction(SKAction.repeatActionForever(sequence), withKey: "spawning")
}
​​
func didBeginContact(contact:SKPhysicsContact) {
let bodyA: SKPhysicsBody = contact.bodyA
let bodyB: SKPhysicsBody = contact.bodyB
if ((bodyA.categoryBitMask == ColliderType.playerCategory) && (bodyB.categoryBitMask == ColliderType.coinCategory)){
didPlayerCollideWithCoin(bodyA.node as! SKSpriteNode, coin: bodyB.node as! SKSpriteNode)
}
}
I would recommend that you read these documentation first!
contactTestBitMask - A mask that defines which categories of bodies cause intersection notifications with a current physics body.
When two bodies share the same space, each body’s category mask is
tested against the other body’s contact mask by performing a logical
AND operation. If either comparison results in a nonzero value, an
SKPhysicsContact object is created and passed to the physics world’s
delegate. For best performance, only set bits in the contacts mask for
interactions you are interested in.
collisionBitmask - A mask that defines which categories of physics bodies can collide with this physics body.
When two physics bodies contact each other, a collision may occur.
This body’s collision mask is compared to the other body’s category
mask by performing a logical AND operation. If the result is a nonzero
value, this body is affected by the collision. Each body independently
chooses whether it wants to be affected by the other body. For
example, you might use this to avoid collision calculations that would
make negligible changes to a body’s velocity.
Remove this code to solve the colliding issue
coin.physicsBody?.collisionBitMask = ColliderType.playerCategory
Try to see if this solves your SpriteNode alignment issue
var coinTexture = SKTexture(imageNamed: "coin")
coin = SKSpriteNode(texture:coinTexture)
Also, I would recommend changing how you're defining your Category Bitmasks. Since you're already doing bit shifting with <<, you don't need to shift by powers of two. That's what the bit shifting does for you. Try changing your code to this:
static let playerCategory: UInt32 = 0x1 << 0
static let boundary: UInt32 = 0x1 << 1 ​
​static let coinCategory: UInt32 = 0x1 << 2 ​
​static let bodyA: UInt32 = 0x1 << 3 ​
​static let bodyB: UInt32 = 0x1 << 4
This won't help with your problem, but this is good to know.
I actually found a workaround that works much better for my game, where I sublcass SKNode to represent objects like missiles that have different damage, terrain cells, etc. I set the collision bit masks to all my objects to be the same (i.e., they all need to interact with each other. Then to loop through all collisions with player and also get a pointer to the proper object that it's colliding with, I do the following
for (int i=0; i<[self.physicsBody.allContactedBodies count]; i++) {
SKPhysicsBody* contactedBody = self.physicsBody.allContactedBodies[i];
if (contactedBody != NULL && [contactedBody.node isKindOfClass:[TerrainCell class]]) {
NSLog(#"BUMPED INTO TERRAIN");
TerrainCell* collisionObject = (TerrainCell*)contactedBody.node;
// do more stuff specific to TerrainCell
}
}

Swift/SpriteKit Collision Detection Between Many Objects

So I have two objects that should lose health points at an collision.
func addPlayer(xPos: CGFloat, yPos: CGFloat){
playerNode = SKSpriteNode(imageNamed: "player")
playerNode.physicsBody = SKPhysicsBody(circleOfRadius: width/2)
playerNode.physicsBody!.affectedByGravity = false
playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode.physicsBody!.contactTestBitMask = PhysicsCategory.Wall | PhysicsCategory.Zombie
playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Wall | PhysicsCategory.Zombie
playerNode.name = "Player"
player = Player(node: playerNode, healthPoints: 100, attack: 10)
playerNode.position.x = xPos
playerNode.position.y = yPos
playerNode.size = CGSize(width: width, height: width)
addChild(playerNode)
}
func addZombie(xPos: CGFloat, yPos: CGFloat){
zombieNode = SKSpriteNode(imageNamed: "zombie")
zombieNode.physicsBody = SKPhysicsBody(circleOfRadius: width/2)
zombieNode.physicsBody!.affectedByGravity = false
zombieNode.physicsBody!.categoryBitMask = PhysicsCategory.Zombie
zombieNode.physicsBody!.contactTestBitMask = PhysicsCategory.Zombie | PhysicsCategory.Player | PhysicsCategory.Wall
zombieNode.physicsBody!.collisionBitMask = PhysicsCategory.Zombie | PhysicsCategory.Player | PhysicsCategory.Wall
zombieNode.name = "Zombie"
zombie = Zombie(node: zombieNode, healthPoints: 50, attack: 5)
Zombies.append(zombie!)
zombieNode.position.x = xPos
zombieNode.position.y = yPos
zombieNode.size = CGSize(width: width, height: width)
addChild(zombieNode)
}
When a collision appears this function get activated:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA.node as! SKSpriteNode
let secondBody = contact.bodyB.node as! SKSpriteNode
if(firstBody.name == "Player" && secondBody.name == "Zombie"){
changeHealthPointsForZombieWithNode(secondBody, points: player!.attack)
} else if(firstBody.name == "Zombie" && secondBody.name == "Player"){
changeHealthPointsForPlayer(secondBody, points: zombie!.attack)
print(player!.healthPoints)
}
}
func changeHealthPointsForZombieWithNode(node: SKSpriteNode, points: Int) {
for zombie in Zombies {
if zombie.node == node {
zombie.healthPoints -= points
print(zombie.healthPoints)
if(zombie.healthPoints <= 0){
zombieNode.removeFromParent()
}
return
}
}
}
func changeHealthPointsForPlayer(node: SKSpriteNode, points: Int) {
player!.healthPoints -= points
if(player!.healthPoints <= 0){
playerNode.removeFromParent()
gameOver = true
}
}
I want to subtract the health points of the zombie depending on the attack of the player and other way around. When the player hits the zombie the zombie should lose life points. When the zombie hits the player the player should lose life points. Every player/zombie got health points and an attack value. The Problem is that some zombies are killable and lose health and other (normally 1-3) are not able to lose health. These zombies who aren't able to lose health are the only one able to kill the player. Zombies that lose health can't deal damage(why?)? So there only able to do one thing(attack or lose health) although they should be able to do two things(attack and lose health).
Keep in mind that the two physics bodies described in the contact parameter are not passed in a guaranteed order. Inside the delegate function there is not such a thing as "A is the one colliding with B, not the other way around". There is only "A and B are colliding". That said, you have two options at least, depending on your game mechanics:
Make player AND zombie deal damage to each other every time they collide.
Make for player and zombie a subclass of SKSpriteNode with a property isAttacking, a boolean to determine when the entity should deal damage when detecting a contact with it. For the player this boolean might get "activated" every button/tap press. For the zombie, every once in a while, if close enough to the player.
Additionally, the way you implemented you will be getting doubled contact notifications. SpriteKit will detect that the player is in contact with the zombie, by the player contact mask and zombie category mask, and that the zombie is in contact with the player, by the zombie contact mask and the player category mask. You usually don't want this to happen. You should make only one of them detect it. I would suggest the player. This way there is no need to set the zombie contact mask. As long as you set zombie in the player's contact mask and the zombie's category mask, you will already have the detection (and once).