此代码使我的球着色。
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)
})
}
这段代码使我的墙壁着色。
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()]
}
我需要编写一条if语句,以检测与墙壁之间的spritekitnode()碰撞时球和墙壁的颜色是否相同。
我已经尝试过这些:
if Wall1.color == UIColor.redColor() && Wall2.color == UIColor.redColor() && Ball.color != UIColor.redColor {
died = true
}
我的主要问题是编写if语句,该语句确定球和墙的颜色是否相同。我的碰撞部分下降了。
非常感谢你的帮助。
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 {
}
}
现在尚未真正使用colorWasContacted,但其他所有功能都已使用。
更改墙壁颜色的代码。
action = SKAction.colorizeWithColor(randomColor(), colorBlendFactor: 1.0, duration: 0.3)
Wall1.runAction(动作)
这就是我添加断点时发生的情况。
Breakpoints
最佳答案
一次只能使用两个元素进行联系,因此您可以在didBeginContact
中执行以下操作(根据实际代码进行编辑):
...
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()
}
}
因为这可能有助于解决这个孤立的问题,所以我相信(通过查看其他线程),您需要重新考虑发现墙之间的通过的整个逻辑...
关于swift - 比较基于随机着色的两种 Sprite 颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37873958/