swiftif-statementcollision-detectionuicolorcolor-detection

Comparing two sprites colors based on random colorization?


This code colorizes my ball.

var colorize1 = SKAction.colorizeWithColor(.redColor(), colorBlendFactor: 1.0, duration: 0.3)

        var colorize2 = SKAction.colorizeWithColor(.greenColor(), colorBlendFactor: 1.0, duration: 0.3)

        var colorize3 = SKAction.colorizeWithColor(.blueColor(), colorBlendFactor: 1.0, duration: 0.3)

        var actions = [colorize1, colorize2, colorize3]

        var randomIndex = Int(arc4random_uniform(3))

        var action = actions[randomIndex]

        let seconds = 0.14
        let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

        dispatch_after(dispatchTime, dispatch_get_main_queue(), {


            self.Ball.runAction(action)


        })

    }

This code colorizes my walls.

var colorBucket = [UIColor]()

func randomColor() -> UIColor {

    if colorBucket.isEmpty {
        fillBucket()
    }

    let randomIndex = Int(arc4random_uniform(UInt32(colorBucket.count)))
    let randomColor = colorBucket[randomIndex]
    colorBucket.removeAtIndex(randomIndex)
    return randomColor

}

func fillBucket() {
    colorBucket = [UIColor.redColor(), UIColor.greenColor(), UIColor.blueColor()]
}

I need to write an if statement that detects whether or not the ball and wall are the same color when I collide with an spritekitnode() located in between my walls.

I've tried these:

if Wall1.color == UIColor.redColor() && Wall2.color == UIColor.redColor() && Ball.color != UIColor.redColor {

died = true

}

My main issue is writing the if statement that determines if the ball and walls are the same color. I have the collision part down. Thank you so much for your help.

func didBeginContact(contact: SKPhysicsContact) {


    let firstBody = contact.bodyA
    let secondBody = contact.bodyB




    let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball
    var wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall
    let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score
    let colorWasContacted = firstBody.categoryBitMask == PhysicsCat.Color || secondBody.categoryBitMask == PhysicsCat.Ball

    if ballWasContacted {
        if scoreWasContacted {
            score += 1
            scoreLbl.text = "\(score)"
            let scoreNode = firstBody.categoryBitMask == PhysicsCat.Score ? firstBody.node : secondBody.node
            scoreNode!.removeFromParent()
        } else if wallWasContacted {
            enumerateChildNodesWithName("wallPair", usingBlock: ({
                (node, error) in
                node.speed = 0
                self.removeAllActions()

                                }))


            if died == false {
                died = true
                createBTN()
                fallDie()
            }
        }
    }

    else if colorWasContacted {


    }

}

The colorWasContacted isn't really being used right now, but everything else is.

Code to change color of wall.

action = SKAction.colorizeWithColor(randomColor(), colorBlendFactor: 1.0, duration: 0.3)

Wall1.runAction(action)

This is what happened when I added the breakpoints.

Breakpoints


Solution

  • Contacts only happen with two elements at a time, so you could do something like this in your didBeginContact (edit, based on your actual code):

    ...
    else if wallWasContacted {
        let sprite1 = firstBody.node as! SKSpriteNode
        let sprite2 = secondBody.node as! SKSpriteNode
        if sprite1.color.isEqual(sprite2.color) {
            // Whatever your heart desires...
        } else {
           // Something else you'd like to have done...
        }
    
        enumerateChildNodesWithName("wallPair", usingBlock: ({
            (node, error) in
            node.speed = 0
            self.removeAllActions()
        }))
    
        if died == false {
            died = true
            createBTN()
            fallDie()
        }
    }
    

    As this might help with this isolated issue, I belive (from looking through your other threads) that you need to rethink the whole logic when it comes to discovering passing between walls...