【发布时间】:2016-06-17 05:43:13
【问题描述】:
这段代码为我的球着色。
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()]
}
当我与位于我的墙之间的 spritekitnode() 发生碰撞时,我需要编写一个 if 语句来检测球和墙的颜色是否相同。
我试过这些:
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(动作)
这就是我添加断点时发生的情况。
【问题讨论】:
标签: swift if-statement collision-detection uicolor color-detection