func didBegin(_ contact: SKPhysicsContact) {
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
}
if (firstBody.categoryBitMask & Constants().playerCategoryBitMask != 0)
{
if(secondBody.categoryBitMask & Constants().borderCategoryBitMask == 4)
{ touchingWall = true
print("Touching the wall ");
}
}
}
didBegin is working great!
However didEnd not sure how to do it?
func didEnd(_ contact: SKPhysicsContact) {
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
}
if (firstBody.categoryBitMask & Constants().borderCategoryBitMask != 0 )
{
if(secondBody.categoryBitMask & Constants().playerCategoryBitMask != 0 )
{
touchingWall = false
print("Not Touching the wall ");
}
}
}
I also have
let playerCategoryBitMask:UInt32 = 1
let borderCategoryBitMask:UInt32 = 4
This is because you´re using a method called the bitwise AND operator (&)
.
The bitwise AND operator (&) combines the bits of two numbers. It returns a new number whose bits are set to 1 only if the bits were equal to 1 in both input numbers:
let eightBits1: UInt8 = 0b00000001
let eightBits2: UInt8 = 0b00000001
let lastBit = eightBits1 & eightBits2 // equals 0b00000001
Combining the bits only the last bit 1 would return 1 all the rest would return zero.
A simpler explanation:
I declare two variables:
let x = 1
let y = 1
Here both x and y has the value 1, when you use bitwise AND operator the result will be also 1 and when checking if the result it is not equal to zero it would be true (any result that it is not equal to zero would return true).
let eightBits1: UInt8 = 0b00000001 // 1
let eightBits2: UInt8 = 0b00000001 // 1
let lastBit = eightBits1 & eightBits2 // equals 0b00000001 // 2
The result would always be the same as x
(which is equal to y
) in this case 1
.
if (x & y) != 0 {
print("Same")
} else {
print("Not same")
}
In this case:
let x = 1
let y = 2
let eightBits1: UInt8 = 0b00000001 // 1
let eightBits2: UInt8 = 0b00000010 // 2
let noBits = eightBits1 & eightBits2 // equals 0 -> 0b00000000
You get false
and not same will be printed out since the result of the bitwise operator it IS equal to zero
Basically, if you use the Bitwise AND operator
using two identical numbers the result will always be the same number.
To your issue:
In your didBegin
, you´re comparing:
if (firstBody.categoryBitMask & playerCategoryBitMask) != 0
Here your firstBody.categoryBitMask
is 1 and playerCategoryBitMask
is also 1 hence true
and you enter the if-statement.
In your didEnd
you´re comparing:
if (firstBody.categoryBitMask & Constants().borderCategoryBitMask) != 0
Here your firstBody.categoryBitMask
is 1 and borderCategoryBitMask
is 4 hence the result is zero
and you don´t enter the if-statement because 0 it is equal to zero.
Now that you know this, you can modify your code and make it work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With