枚举可以包含类列表吗?
我有一个超类Activity:SKScene{},它有一个子私有类Activity01:Activity{},一个子私有类Activity02:Activity{},还有一个子私有类Activity03:Activity{}。
每个子私有类都包含一个定时活动。当计时器在用户解决活动之前用完时,将弹出一个GameOver场景(类GameOver:activity{})(文本为“Bad luck try again”)。
我试图在GameOver类中创建一个枚举,以便在touch中触发,开始将用户返回到他们的最后一个活动,但我无法解决这个问题。当点击时,它总是返回到Activity01。
以下是我的尝试:
…} // Activity03 sub-private class closure
class GameOver: Activity {
enum classes {
case Activity01
case Activity02
case Activity03
}
var firstClass = classes.Activity01
var secondClass = classes.Activity02
var thirdClass = classes.Activity03
let restartGameText = SKLabelNode(fontNamed: "AvenirNext-HeavyItalic")
init(size: CGSize, won:Bool) {
super.init(size: size)
startGameText.removeFromParent()
restartGameText.text = "Bad luck try again"
restartGameText.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center
restartGameText.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center
restartGameText.fontSize = 60
restartGameText.fontColor = SKColor(red: 230/255, green: 60/255, blue: 70/255, alpha: 1)
restartGameText.position = CGPoint(x: scene!.size.width/2.0, y: scene!.size.height/2.0)
restartGameText.zPosition = 5
addChild(restartGameText)
let pulseAction = SKAction.sequence([SKAction.fadeAlpha(to: 1, duration: 1), SKAction.fadeAlpha(to: 0.7, duration: 1)])
restartGameText.run(SKAction.repeatForever(pulseAction))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if firstClass == classes.Activity01 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity01 = Activity01(size: size)
view?.presentScene(activity01, transition: transition)
} else if secondClass == classes.Activity02 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity02 = Activity02(size: size)
view?.presentScene(activity02, transition: transition)
} else if thirdClass == classes.Activity03 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity03 = Activity03(size: size)
view?.presentScene(activity03, transition: transition)
}
}
} //GameOver class closure
} // Activity Superclass closure
如果是最后一个活动,是否有方法返回Activity02或Activity03?
最佳答案
下面的枚举声明非常好。但这里没有枚举类型作为类。它只是一个不带任何类型的枚举,这在Swift中是允许的。
enum classes {
case Activity01
case Activity02
case Activity03
}
仅供您参考,关于来自苹果的“Swift编程语言”中的Enum:
默认情况下,Swift枚举案例没有设置整数值,
与C和Objective-C等语言不同…..相反,不同的是
枚举事例本身就是值。
总是得到
Activity01
的原因是,您在代码中将firstClass
指定为Activity01
ans等等。var firstClass = classes.Activity01
var secondClass = classes.Activity02
var thirdClass = classes.Activity03
因此,当
firstClass
与classes.Activity01
方法中的touchesBegan
比较时,它总是匹配的。这就是原因,你总是得到“Activity01
”。您应该有实例成员来跟踪
lastClass
类型的enum classes
,并且应该将其与classes.Activity01 / 02 / 03
方法中的touchesBegan
进行比较,而不是与firstClass
和classes.Activity01
等进行比较。应该是这样的。override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if lastClass == classes.Activity01 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity01 = Activity01(size: size)
view?.presentScene(activity01, transition: transition)
} else if lastClass == classes.Activity02 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity02 = Activity02(size: size)
view?.presentScene(activity02, transition: transition)
} else if lastClass == classes.Activity03 {
let transition = SKTransition.fade(withDuration: 2.0)
let activity03 = Activity03(size: size)
view?.presentScene(activity03, transition: transition)
}
}
这样,即使是活动02/03,最后一个活动类也可以正确显示。