检查随机操作的结果?
Checking the result of a random action?
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)
})
}
因此,此代码将我的球着色为红色、绿色或蓝色。当我的游戏是 运行 时,有没有办法检查它选择了哪种颜色?它不只是选择一个然后就完成了,我的球的颜色每隔几秒钟就会改变一次。我如何构造一个 if 语句,例如:"If the random color picked by my action is red then print("hey") 或类似的语句?
编辑 我是 swift 和 xCode 的新手,所以我可能需要更深入的帮助。
您只需打开 randomIndex
:
switch randomIndex {
case 0:
print("colorize1")
case 1:
print("colorize2")
case 2:
print("colorize3")
}
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)
})
}
因此,此代码将我的球着色为红色、绿色或蓝色。当我的游戏是 运行 时,有没有办法检查它选择了哪种颜色?它不只是选择一个然后就完成了,我的球的颜色每隔几秒钟就会改变一次。我如何构造一个 if 语句,例如:"If the random color picked by my action is red then print("hey") 或类似的语句?
编辑 我是 swift 和 xCode 的新手,所以我可能需要更深入的帮助。
您只需打开 randomIndex
:
switch randomIndex {
case 0:
print("colorize1")
case 1:
print("colorize2")
case 2:
print("colorize3")
}