2017-11-10 6 views
1

ユーザーがタップしている間はボタンの画像を変更したいが、画面から指を離した場合は元の画像に戻す(スプライトキットを使用している)ユーザーがボタンをタップしている限り、ボタンの画像を変更したいですか?

マイコード:?

var SettingButton = SKSpriteNode(imageNamed: "SettingButton1.0") 

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

     if atPoint(locationUser) == SettingButton{ 
      let SettingButton = SKSpriteNode(imageNamed: "SettingButton2.0") //change the image 
     } 
    } 
} 

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

     if atPoint(locationUser) == SettingButton{ 
      //change image back to original 
     } 
    } 
} 
+0

の質感を交換してみてくださいあなたのコードはコンパイルされますか?ランタイムエラーが発生しますか?あなたのコードは、あなたが期待することをしません。より具体的にしてください。 –

+0

touchesBegan関数の上のボタンから画像を変更する方法がわかりません@DavidPásztor – Jay23

答えて

2

は、あなたの質問は何であるSpriteNode

var buttonTextureUp = SKTexture(imageNamed: "SettingButton1.0") 
var buttonTextureDown = SKTexture(imageNamed: "SettingButton2.0") 

var settingButton = SKSpriteNode(texture: buttonTextureUp) 

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

     if atPoint(locationUser) == settingButton { 
      settingButton.texture = buttonTextureDown //change the image 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches{ 
     let locationUser = touch.location(in: self) 

     if atPoint(locationUser) == settingButton{ 
      settingButton.texture = buttonTextureUp //change image back to original 
     } 
    } 
} 
+1

tochchesCancelledが呼び出された場合、テクスチャをbuttonTextureに変更することを忘れないでください。 – Knight0fDragon

+0

優秀な点 –

+0

素晴らしいです。ありがとう!@Ron Myschuk – Jay23

関連する問題