2016-04-12 8 views
2

私はSpritekitを使用してボードゲームを作成しています。当初、すべての部分(SKSpriteNode)は隠されています。彼らは私がそれらに触れた後に表示されるはずですか? しかし、それらは非表示に設定されているため、touchBegan関数をオーバーライドするとアクセスできません。私はどうしたらいいですか?隠されたSKSpriteNodeをSpritekitでタッチして検出する方法は?

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let location = touches.first!.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(location) 
    if(touchedNode.name == "pieces"){ 
     if(touchedNode.hidden == true){ 
      touchedNode.hidden = false 
     } 
    } 
} 
+0

も始めました。ユーザーは、隠された何かを押すことを彼は知っていると思われますか? – pingul

+0

隠しtry alphaを使用する代わりに? – hamobi

+0

ノードのalphaプロパティを0.0 –

答えて

2

あなたが試みているのは、iOS8で作業していたものです。しかし、これはiOS9で修正されています。 iOS8でコードを実行しようとすると、それが動作することがわかります。これは、隠されたノードをタップすることができないので、iOS8で私を悩ましたことの1つです。それでも、アルファ0.0(iOS8でも動作します)のノードをタップできますが、これはと固定されており、iOS9でもと固定されています。したがって、アルファを0.0に設定しても機能しません。

はポイントが 親の座標系内にあるかどうかを示すブール値を返します:

ソリューションは

あなたはSKNodeのcontainsPointメソッドを使用することができます。

親があなたのスプライトになるでしょう。 herecontainsPoint方法がどのように動作するかを読んでください。基本的にcontainsPointは、ノードが表示されているかどうか、または親があるかどうかは気にしません。特定のポイント(location)が親の座標系(touchedNode)の内部にあるかどうかをチェックするだけです。だからここにあなたがこの方法を使ってそれを行うことができる方法である。

ここ
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     let location = touches.first!.locationInNode(self) 

     self.enumerateChildNodesWithName("pieces") { node, stop in 

      if node.containsPoint(location) && node.hidden == true { 

       node.hidden = false 
       stop.memory = true 
      } 
     } 
    } 

、あなたは(私は自分が親であると仮定)の作品の全てを列挙し、特定のノードは、タッチ位置を含むプラスそれが隠されている場合されていますそれを見えるようにします(さらに不要な検索を止めます)。

ヒント:

if let touch = touches.first { 

    let location = touch.locationInNode(self) 

    //do your stuff here 
} 

をそれは安全ですので、オプションの結合を使用することは良い習慣です:

また、ここでオプションのバインディングを使用することを検討してください。

+0

に設定していただき、ありがとうございました。 – user3499912

+0

@ user3499912ようこそ。 ) – Whirlwind

+0

同じ場所の別の隠れノードが最初にキャッチしていたため、ノードが「touchesBegan」を起動していなかったiOS8でアプリをデバッグするのに時間を費やしていましたそれがAppleがやった良い解決策だと教えてください。 – rghome

0

私はこれまで動作しているようだ別のアプローチがあります。

//あなたはターンを取らなければならない場合は、黒のディスクを持つforループwhitediskを繰り返すことができます。私は、選択された正方形の識別するために、.nameのを使用しています

func drawBoard() { 

     let numRows = 8 
     let numCols = 8 
     let x = scene?.size.width 
     xOffset = ((x)! - ((x)!/9) * CGFloat(numCols)) 
     squareSize = CGSizeMake(x!/9, x!/9) 
     diskSize = CGSizeMake(x!/11, x!/11) 

     // Draw the squares 
     for row in 0...numRows-1 { 
      for col in 0...numCols-1 { 
       square = SKSpriteNode(texture: SKTexture(imageNamed: "square.png"), size: squareSize!) 
       //row and column both start with zero, may want to start with 1 later 
       square!.name = "square \(col)-\(row)" 
       square!.userInteractionEnabled = false 
       square!.position = CGPointMake(CGFloat(col) * squareSize!.width + xOffset, CGFloat(row) * squareSize!.height + xOffset) 
       self.addChild(square!) 
      } 
     } 
     // Draw the White disks "hidden" 
     for row in 0...numRows-1 { 
      for col in 0...numCols-1 { 
       let gamePiece = SKSpriteNode(texture: SKTexture(imageNamed: "whitedisk.png"), size: diskSize!) 
       gamePiece.name = "whitedisk \(col)-\(row)" 
       gamePiece.userInteractionEnabled = false 
       //print("Disk Name: \(gamePiece.name)") 
       gamePiece.position = CGPointMake(CGFloat(col) * squareSize!.width + xOffset, CGFloat(row) * squareSize!.height + xOffset) 
       gamePiece.hidden = true 
       self.addChild(gamePiece) 
      } 
     } 
} 

//ディスク変数は、そのターン、それは あるこの関数は、正方形が選択されたかを決定した後、ゲームの駒を表示するに応じて、whitediskかblackdiskのいずれかである

func placeDisk(name: String, disk: String) { 
    var myName = name 
    let theDisk = disk 
    myName = myName.stringByReplacingOccurrencesOfString("square", withString: theDisk) 
    let node = self.childNodeWithName(myName) 
    //print("Display: \(myName) - Node: \(String(node))") 
    node?.hidden = false 
} 

//私はtouchesEND機能をoverodeいますが、タッチを使用することができますがあなたのアイデアを再考する必要があるように思え

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch in touches { 
      let positionInScene = touch.locationInNode(self) 
      let name = self.nodeAtPoint(positionInScene).name 
      if name != nil { 
       print("Touched: \(name)") 
       placeDisk(name!, disk: "whitedisk") 
      } 
     } 
    } 
関連する問題