我创建了SKSpriteNode类的扩展。我正在制作砖块对象,当它们被击中时会有不同的行为
import SpriteKit
class Brick: SKSpriteNode {
enum type {
case None, Green, Yellow, Orange, Red
}
static let colorMap = [
Brick.type.Green : UIColor.greenColor(),
Brick.type.Yellow : UIColor.yellowColor(),
Brick.type.Orange : UIColor.orangeColor(),
Brick.type.Red : UIColor.redColor()
]
var brickType = Brick.type.None
convenience init (size: CGSize, type: Brick.type) {
self.init(color: UIColor.whiteColor(), size: size)
// Here I set the initial type and color
// The color is assigned just fine, but the brickType
// variable is still Brick.type.None
self.setType(type)
}
func gotHit () -> Int {
switch (self.brickType) {
case Brick.type.Yellow:
setType(Brick.type.Green);
break;
case Brick.type.Orange:
setType(Brick.type.Yellow);
break;
case Brick.type.Red:
setType(Brick.type.Orange);
break;
case Brick.type.Green: // Green
self.removeFromParent()
return 1
default:
break
}
return 0
}
func setType (typeToSet: Brick.type) {
self.brickType = typeToSet // only works when called from gotHit()
self.color = Brick.colorMap[typeToSet]! // this works everytime
}
}
然后,我创建一个此类的实例:
let brickPrototype = Brick(size: CGSizeMake(55, 25), type: Brick.type.Green)
我的问题是,尽管在
setType()
中调用了convenience init ()
,但公共(public)brickType
变量的值仍然是默认值Brick.type.None
。颜色更改没有问题,因此该参数似乎正确传递了。如果我将默认的
brickType
变量设置为Brick.type.Yellow
并执行gotHit()
函数,则setType()
函数将有效地将砖的类型更改为Brick.type.Green
,并在再次调用它之后,通过调用self.removeFromParent()
将节点从 View 中删除。因此,我可以确定问题出在从convenience init()
调用函数时,即使没有错误。 最佳答案
如果您是在初始化程序中首次设置默认值,则无需设置默认值。还没有测试它是否可以解决您的问题,但是我确实整理了一下代码。
class Brick: SKSpriteNode {
enum BrickColorType: UInt {
case Red
case Orange
case Yellow
case Green //This order matters for nextColor
func color() -> UIColor {
switch self {
case Green:
return .greenColor() //Swift does not need break statements in cases.
case Yellow:
return .yellowColor()
case Orange:
return .orangeColor()
case Red:
return .redColor()
}
}
func nextColor() -> BrickColorType? {
return BrickColorType(rawValue: self.rawValue.successor()) //If self = green, this will return nil.
}
}
var brickType: BrickColorType {
didSet {
self.color = brickType.color()
}
}
init (size: CGSize, type: BrickColorType) {
brickType = type
super.init(texture: nil, color: .whiteColor(), size: size) //Because the earlier one was a convenience init and Xcode was complaining
self.color = brickType.color() //Because didSet is not called in initializer
}
required init?(coder aDecoder: NSCoder) { //This is just boilerplate required to inherit from any NSObject
brickType = .Red //Or whatever else. If you really want to add a None case to the enum, might I suggest instead making brickType optional?
super.init(coder: aDecoder)
}
func gotHit () -> Int { //Consider making this a Bool
if let next = self.brickType.nextColor() {
//There is a valid next color
brickType = next
return 0
}
//There is no valid next color
self.removeFromParent()
return 1
}
} //Weird formatting because of StackOverflow