我正在尝试在switch语句之外定义一个常量,以便我可以在switch语句执行完毕后使用它并在switch语句中分配它:
let action: SKAction! switch (whatever) { case 0: sprite.position = CGPointMake(0, self.scene.size.height * lengthDiceroll) action = SKAction.moveTo(CGPointMake(self.scene.size.width, self.scene.size.height * (1 - lengthDiceroll)), duration: 1) // error here // other actions default: println("meh") // add the sprite to the scene let sequence = SKAction.sequence([action, action2]) sprite.runAction(SKAction.repeatActionForever(sequence)) self.addChild(sprite)
但是我收到了错误Cannot assign to 'let' value 'action'
.如何分配操作以便我可以在交换机外部使用它?
如果我尝试:
action! = SKAction.moveTo(CGPointMake(self.scene.size.width, self.scene.size.height * (1.0 - lengthDiceroll)), duration: 1)
我收到了错误"Could not find an overload for '*' that accepts the supplied arguments"
.
声明的变量let
必须立即赋值.您不能简单地使用let和no值来定义变量,就像您尝试在第一行上做的那样.
你说你希望action成为一个常量,但是你在运行时修改它的值,这不是常量.因此,您需要一个变量,即使它的值只改变一次.