2015-07-26 16 views
5

Bir SKSpriteNode sınıfı uzantısı oluşturdum. Onlar bu sınıfının bir örneğini yapmak Ardından iOS/Swift - Enit Değeri ayarlanamıyor Init()

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 
    } 
} 

vurulmak zaman farklı davranışa sahip tuğla nesneleri yapıyorum:

let brickPrototype = Brick(size: CGSizeMake(55, 25), type: Brick.type.Green) 

Benim sorun olduğunu, convenience init()setType() arama rağmen Genel brickType değişkeninin değeri, varsayılan değer olan Brick.type.None'dur. Renk sorunsuz olarak değiştirilir, bu nedenle argümanın doğru bir şekilde geçtiği görülmektedir. Ben Brick.type.Yellow varsayılan brickType değişkeni ayarlamak ve gotHit() fonksiyonunu çalıştırmak, setType() işlevi etkin bir Brick.type.Green için tuğla türünü değişecek ve tekrar çağırdıktan sonra düğüm self.removeFromParent() arayarak görünümünden silinir Eğer

. Bu yüzden, hata olmamasına rağmen, convenience init() işlevini çağırdığımda sorun olduğundan eminim.

+0

Nesneyi nasıl oluşturduğunuzu ve türünü nasıl kontrol edeceğinizi gösterir misiniz? Kodunuzu bir oyun alanına yapıştırdım ve doğru çalıştım – Paulw11

+0

Oyun alanında hala benim için çalışıyor. – Paulw11

+0

Basit bir "enum bir dize işlevine dönüştür" ekledim - https://gist.github.com/paulw11/2010f0474429a679a0b9 ve uygun değeri döndürüyor – Paulw11

cevap

2

İlk kez başlatıcıda ayarladıysanız, varsayılan değere sahip olmanıza gerek yoktur. Sorununuzu çözüp çözmediğini görmek için bunu test etmedim, ancak kodu biraz temizledim.

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 
+0

nolu cevabın analizini/adresini analiz etmek daha iyidir - kesinlikle bir şey öğrendim! – Heriotza