I'm currently learning swift and i'm having some difficulty. In my game a random background color changes every time to a random color between Red Green and Blue.
let colorgen = arc4random_uniform(3)+1
if colorgen == 1{
Col = UIColor.redColor()
}
else if colorgen == 2{
Col = UIColor.greenColor()
}
else if colorgen == 3{
Col = UIColor.blueColor()
}
It works pretty well but I know its not the best way to do things. The game is pretty straight forward, a object can pass through either 3 walls. One green, one red, one blue. However the object can only pass through one wall at a time and that walls color MUST match the background color.
Here is what I tried to do:
override func update(currentTime: CFTimeInterval) {
if segment.color == self.backgroundColor{
segment.physicsBody?.collisionBitMask = 0
}
}
The code above is in the gamescene swift class
The effect is supposed to knock the walls out of place (only when the object hits the wrong color wall, making it fly away with its enabled collision bit mask) indicating a game over.
I only want the walls to have the ability to be knocked out physically when hit if the background color and wall being hit does NOT match (Basically wrong wall getting hit/touched by object). Thus that is why im using collision bit mask so that the current matching correct wall can still register the contact but the object goes right through it (setting it to 0). Exactly how I want it to be.
However collisionBitMask = 0 seems to have no effect on ALL the walls at all times.
My segment code is in another class called MovingGround and was made like this:
for i in 0 ..< NumberSeg {
//NumberSeg = 3 So that means 3 walls on screen
if i == 1{
segmentColor = colorone
//ColorX is just UIColors of Red Green and Blue
}
else if i == 2{
segmentColor = colortwo
}
else{
segmentColor = colorthree
}
segment = SKSpriteNode(color: segmentColor, size: CGSizeMake(self.size.width / CGFloat(NumberSeg), self.size.height))
segment.anchorPoint = CGPointMake(0.5, 0.5)
segment.position = CGPointMake(CGFloat(i)*segment.size.width+50, 0.5)
addChild(segment)
segment.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(self.size.width/3,
15))
segment.physicsBody?.categoryBitMask = heroCategory
segment.physicsBody?.contactTestBitMask = wallCategory
segment.physicsBody?.affectedByGravity = false
segment.physicsBody?.velocity = CGVectorMake(0, 0)
segment.physicsBody?.usesPreciseCollisionDetection = true
It's weird, when I try:
if segment.color == UIColor.blueColor(){
segment.physicsBody?.collisionBitMask = 0
}
or
if self.backgroundColor == UIColor.blueColor(){
segment.physicsBody?.collisionBitMask = 0
}
To test if they work individually, they do!
I just don't understand why if I try to put them together with either a nested if statement or a single if with the && operator it won't work!
I should also mention I made the segment variable global (At least I think that's what its supposed to do) by putting it outside of Constants (Which is another separate file) swift class by:
var segment: SKSpriteNode!
I'm 14 & pretty new to Swift with not much programming knowledge except for a little bit of Python. So if anyone could help me figure this out that would be great!