Kate Yashenko Kate Yashenko - 8 days ago 6
Swift Question

How to make statement work only for the selected level in Sprite Kit

So Here I have this code

if collision == PhysicsCategory.Cat | PhysicsCategory.Bed {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}


And basically this statement works for every single level that I have .
I want to make it like this for only one level

if collision == PhysicsCategory.Cat | PhysicsCategory.Bed| PhysicsCategory.Bottle {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}


But If I will use it it will mean that the collision with bottle will be needed in every single level , however I want to use it only in one level

I am using swift

thank you!

Answer

Possible without a special variable and more succinctly:

var desiredCollision = PhysicsCategory.Cat | PhysicsCategory.Bed
if currentLevel == 7 { // or whatever level you want
   desiredCollision |= PhysicsCategory.Bottle
}

if collision = desiredCollision { 
// ...
Comments