枚举可以包含类列表吗?
我有一个超类Activity:SKScene {},它具有子私有类Activity01:Activity {},子私有类Activity02:Activity {}和子私有类Activity03:Activity {}。 每个子私人课程都包含一个定时活动。当计时器在用户解决活动之前用完时,会弹出一个GameOver场景(类GameOver:Activity {})(带有文本“运气不好,请重试”)。
我正在尝试在GameOver类中创建一个枚举,以便通过触摸触发该操作,以使用户返回其上一次活动,但我无法解决该活动。轻按它总是返回到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?
答案 0 :(得分:1)
下面的enum声明非常好。但是这里没有枚举类型作为类。它只是一个没有任何类型的枚举,在Swift中是允许的。
enum classes {
case Activity01
case Activity02
case Activity03
}
仅供参考,来自Apple的“ The Swift Programming Language”中的Enum:
Swift枚举案例默认情况下未设置整数值, 不像C和Objective-C等语言。相反, 枚举案例本身就是价值。
总是获得Activity01
的原因是,您在代码中将firstClass
分配为Activity01
等等。
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
比较,而不是与{{1 }}和firstClass
等。应该是这样的。
classes.Activity01
这样,即使活动02/03也可以正确显示上一个活动类别。