2015-07-19 52 views
2

我想合併的SKShapeNode和SKLabeNode僅做一個節點合併SKShapeNode和SKLabelNode

這裏是我的「BLOQUE」類誰繪製一個矩形,並添加sklabelnode孩子就可以了:

class Bloque : SKShapeNode 
{ 
    var color : String! 
    var numero : Int! 
    var type : Int! 

    var labelNumeroBloque : SKLabelNode! 


    init(type : Int, numero : Int, tailleBloque : CGSize) 
    { 
     super.init() 

     self.numero = numero 
     self.type = type 


     switch (type) 
     { 
      case 0: color = "#4aaddb" 
      default: color = "#ccc" 
     } 


     var rect = CGRect(origin: CGPoint(x: 0.5, y: 0.5), size: CGSize(width: tailleBloque.width, height: tailleBloque.height)) 

     self.path = CGPathCreateWithRoundedRect(rect, 2.0, 2.0, nil) 
     self.fillColor = UIColor(rgba: color) 
     self.name = "\(numero)" 
     self.lineWidth = 0.0 
     self.zPosition = 200 


     labelNumeroBloque = SKLabelNode(text: String(numero)) 
     labelNumeroBloque.position = CGPointMake(tailleBloque.width/2, tailleBloque.height/2) 
     labelNumeroBloque.verticalAlignmentMode = .Center 
     labelNumeroBloque.horizontalAlignmentMode = .Center 
     labelNumeroBloque.fontName = "ArialMT" 
     labelNumeroBloque.fontSize = 20 
     labelNumeroBloque.name = "\(numero)" 

     self.addChild(labelNumeroBloque) 
    } 


    required init?(coder aDecoder: NSCoder) 
    { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

有了這段代碼,當我點擊彩色空間它的工作,但如果用戶點擊數字它不起作用。 它看起來像的SKShapeNode和SKlabelNode不是一個完整的節點

Bloque image

這裏是的touchesBegan功能:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 
{ 
    for touch in (touches as! Set<UITouch>) 
    { 
     let location = touch.locationInNode(self) 
     let cliqueNode = nodeAtPoint(location) 

     if let bloque = cliqueNode as? Bloque 
     { // verifie que le bloque est du type Bloque 
      nb++ 
      bloque.removeFromParent() 
     } 
     else 
     { // mauvais bloque cliqué  
      println("Debug : mauvais bloque") 
     } 
    } 
} 

我想知道我怎麼能既SKNode合併做一個合理的,所以當用戶點擊彩色區域或數字它的工作。 有人可以幫我嗎? 對不起,我的英語不好:/

回答

0

你可能需要子類SKLabelNode,覆蓋func calculateAccumulatedFrame() -> CGRect並返回零大小CGRect。正在返回標籤,因爲func nodeAtPoint(_ p: CGPoint) -> SKNode返回與點相交的最深的後代。它使用calculateAccumulatedFrame()檢查十字路口,所以通過返回零大小的矩形不會相交,因此返回SKShapeNode

代碼可能是這樣的:

class SpecialLabelNode: SKLabelNode { 
    override func calculateAccumulatedFrame() -> CGRect { 
     return CGRectZero 
    } 
} 

class Bloque : SKShapeNode { 
    ... 
    var labelNumeroBloque : SpecialLabelNode! 
    ... 
} 
+0

謝謝,它的工作:D – Xakaa

1

既然你做出BLOQUE SKShapeNode和SKShapeNode的子類是SKNode的子類,也許你可以設置userInteractionEnabled BLOQUE實例屬性爲true。然後你可以直接在類Bloque裏面寫touchesBegan touchesEnd函數。這樣你就不必計算觸摸是否在區域內。這些函數只會在Bloque實例的區域內被觸發。