2017-02-06 5 views
0

私は完全なnoobのように聞こえると思いますが、私は最初のiOSゲームで作業しています。ユーザーが画像を押さえたまま放置した状態でスワップバックしている間に、「シュートプレス」画像にスワップしてアニメートしたいと考えています。 ここで私の感触は始まり、動いて終わりました。心に留めておくと、違反していないコードは除外されました。私は最後の2日間この問題に立ち往生してきました。スプライトノードボタンがアニメーションを押している間にアニメーションを与えるのを助ける必要があります

UPDATE:私は私のボタンノード

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in:self) 
     let node = self.atPoint(location) 

     if(node.name == "Shoot") { 
      shootButton.texture = SKTexture(imageNamed: "shootPushed") 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in:self) 
     let node = self.atPoint(location) 
     if (node.name == "Shoot") { 
      shootBeams() 
      shootButton.texture = SKTexture(imageNamed: "shootUnpushed") 
     } 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in:self) 
     let node = self.atPoint(location) 
     if node.name == ("shoot") { 
      shootButton.texture = SKTexture(imageNamed: "shootPushed") 
     } 
    } 
}  

func addButtons() { 
     var buttonT1 = SKTexture(imageNamed: "shootUnpushed") 
     var buttonT2 = SKTexture(imageNamed: "shootPushed") 

     var shootButton = SKSpriteNode(texture: buttonT1)       
     shootButton.name = "Shoot" 
     shootButton.position = CGPoint(x: self.frame.maxX - 130, y: leftButton.position.y) 
     shootButton.zPosition = 7 
     shootButton.size = CGSize(width: shootButton.size.width*0.2, height: shootButton.size.height*0.2) 
     self.addChild(shootButton) 
} 
+0

はあなたがbuttonNodeためのセットアップ・コードを示してもらえますか? –

+0

ここにありがとう – zolev

答えて

0

shootButtonあなたはaddButtonsで作成するローカル変数があるの作成に使用する関数を用意しました。

したがって、touchesMoved内でshootButton.texture = SKTexture(imageNamed: "shootPushed")を実行すると、これは確かに同じノードを参照していません。

これは明らかにこれをコンパイルすると、クラスに既にプロパティーshootButtonがあることを意味します。 addButtons機能をvar shootButton = SKSpriteNode(texture: buttonT1)に変更すると、問題が解決する可能性が高くなります。

それは単にshootButton -propertyではなく、新しいローカル変数の作成を初期化する必要があります

func addButtons() { 
     shootButton = SKSpriteNode(texture: buttonT1)  
     shootButton.name = "Shoot" 
     shootButton.position = CGPoint(x: self.frame.maxX - 130, y: leftButton.position.y) 
     shootButton.zPosition = 7 
     shootButton.size = CGSize(width: shootButton.size.width*0.2, height: shootButton.size.height*0.2) 
     self.addChild(shootButton) 
} 
+0

私は自分の撮影ボタンを適切に初期化するようにコードを変更しましたが、押されたときに私の "shootUnpressed"画像に残っていました。 – zolev

+0

さて、あなたの 'touchesMoved'は、おそらくアセットが実際に見える限り、名前の一致が再び"シュート "ではなく"シュート "ではなく" touchesBegan "と" touchesEnded "正しい。 –

+0

「シュート」と「撮影」を混在させることができますか?文字列の比較は、エラーが発生する可能性があります。何も得られません。 –

関連する問題