iOS/Swift - 无法在 Init() 上设置枚举值

标签 ios swift enums init

我创建了 SKSpriteNode 类的扩展。我正在制作砖 block 对象,当它们被击中时会有不同的行为

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)

我的问题是,尽管在 convenience init () 中调用了 setType(),公共(public) brickType 变量的值仍然是默认的,Brick.type.None。颜色更改没有问题,因此参数似乎已正确传递。

如果我将默认的 brickType 变量设置为 Brick.type.Yellow,并执行 gotHit() 函数, setType() 函数将有效地将积木的类型更改为 Brick.type.Green,再次调用后,通过调用 self 从 View 中删除节点。 removeFromParent()。因此,我确信问题出在我从 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

关于iOS/Swift - 无法在 Init() 上设置枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642865/

相关文章:

swift - 将 AppleScript 字符串列表转换为 Swift 数组

struct - swift 如何在构造结构时使用枚举作为参数?

iPhone cocos2d "Expected specifier-qualifier-list before ..."等

ios - 无法在 Xcode 中以编程方式创建 map (Big Nerd Ranch IOS 编程 : chapter 6 p97)

iOS 模拟器在 50% 比例下呈现问题

ios - 如何从可选函数 didMoveToPage 中获取索引?

ios - Swift:在 touchesBegan 中使用 switch 语句

ios - 按核心数据中的日期组件获取条目

c# - 为枚举提供本地化的最佳方式是什么?

c# - 通过枚举值+依赖注入(inject)创建实例