vendredi 17 juin 2016

Do an SKAction if the colors of two sprites aren't the same?


I would be so thankful if someone could help me out. I've been stuck on this issue for two days.

I have six rectangles. Every two rectangles is one pair. These rectangles are walls in my game.

I'm going to throw a whole bunch of code at you because I don't know what you will need to help me out.

    let Wall1 = SKSpriteNode(imageNamed: "Walls")
    let Wall2 = SKSpriteNode(imageNamed: "Walls")
    let Wall3 = SKSpriteNode(imageNamed: "Walls")
    let Wall4 = SKSpriteNode(imageNamed: "Walls")
    let Wall5 = SKSpriteNode(imageNamed: "Walls")
    let Wall6 = SKSpriteNode(imageNamed: "Walls")

These walls are all white originally. But I randomly colorize them using these functions.

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()]
}

And here is my ball:

        Ball = SKSpriteNode(imageNamed: "Ball")
    Ball.size = CGSize(width: 40, height: 40)
    Ball.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    Ball.physicsBody = SKPhysicsBody(circleOfRadius: Ball.frame.width / 2)
    Ball.physicsBody?.categoryBitMask = PhysicsCat.Ball
    Ball.physicsBody?.collisionBitMask = PhysicsCat.Wall
    Ball.physicsBody?.contactTestBitMask =  PhysicsCat.Wall | PhysicsCat.Score
    Ball.physicsBody?.affectedByGravity = false
    Ball.physicsBody?.dynamic = true
    Ball.zPosition = 1
    Ball.physicsBody!.allowsRotation = false
    Ball.zRotation = CGFloat(M_PI)
    self.addChild(Ball)



     if score != 0 {

        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)


        })

    }

So, my ball will either become red, blue or green each time I pass a wall. Wall1 and Wall2 are the same color, and can be red, green, or blue. Wall3 and Wall4 can't be the same as 1 and 2. Wall5 and Wall6 can't be the same 1 and 2 or 3 or 4.

My ball is going to go in between a gap between Wall1 and Wall2.

My ball might be red, and Wall1 and Wall2 might be green.

If this is the case, I want to lose.

But I want to lose when my ball is even with the bottom of the Wall1 and Wall2.

Here is what it looks: To give you an idea of what I'm talking about.

When that green ball in the picture hits that line, I want the game to end because the color of Wall1 and Wall2 are not the same, the walls are red in that picture. How can I do this? I really need all the help I can get, and if you can figure it out I would be so happy because this is my first app and this is my last function that I want to add into the game. Thanks for your help in advance.


Aucun commentaire:

Enregistrer un commentaire