Comparing two sprites colors based on random colorization?

孤街浪徒 提交于 2019-12-12 03:37:47

问题


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


回答1:


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...




回答2:


As you are mixing questions and re-asking them (worded slightly differently) I'll attempt dealing with your colorization problems here as well, as this is most likely where you are failing. I say most likely because the code you supply does not show us that you colorize the walls at all.

It shows us that:

  1. You have logic in place to change color on the ball.
  2. You have logic to pick out a random color from a colorBucket.
  3. You have logic to refill the bucket when all the colors have been used.

It does not show us that you actually change the color-information for the walls. Could you please supply this part of the code (if it exists).



来源:https://stackoverflow.com/questions/37873958/comparing-two-sprites-colors-based-on-random-colorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!