Search code examples
swiftsprite-kitcollisionskspritenodecolor-detection

How to detect if a sprite is a different color than another at a certain point?


I'm going to try to get straight to the point. I'd be surprised if anyone knew how to help me on this.

I'll try my best to describe what I need.

I have two rectangles that fall down the screen at a certain rate and then once those two fall off, two new ones begin falling. There is a gap between the two rectangles that you are trying to fit a ball into. The ball will either be red, green, or blue. The two rectangles will be the same color as each other, but can also be red, green, or blue. I need to somehow detect if, when my ball enters the gap, if the color of the ball and the color of the two rectangles are the same. IS THIS POSSIBLE?

Here is the code that randomly picks the colors of the ball and rectangles.

 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 can't figure this out.

I was thinking that I would need a spritekitnode attached to each rectangle and when the ball collided would check if the two colors are the same. Is there another way? Any help would be appreciated.

This is what it looks like.


Solution

  • Of course it is possible!

    Given your Rectangle class

    class Rectangle: SKSpriteNode {
        init(color: SKColor) {
            let texture = SKTexture(imageNamed: "rectangle")
            super.init(texture: texture, color: color, size: texture.size())
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    and you Ball class

    class Ball: SKSpriteNode {
        init(color: SKColor) {
            let texture = SKTexture(imageNamed: "ball")
            super.init(texture: texture, color: color, size: texture.size())
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    You can compare two instances just writing

    let ball = Ball(color:.redColor())
    let rectangle = Rectangle(color:.redColor())
    
    print(ball.color == rectangle.color) // true
    

    Update

    It looks like you are not using subclassing of SKSpriteNode.

    Infact you are using SKSpriteNode directly like this

    let wall1 = SKSpriteNode(imageNamed: "Walls")
    let ball = SKSpriteNode(imageNamed: "Ball")
    

    In this case just remember to add a color to these sprites when you create them

    wall1.color = .redColor()
    ball.color =  .redColor()
    

    Now you can compare them by color

    if wall1.color == ball.color {
        print("same color!")
    }