2017-07-02 67 views
0

所以我最近做了一個快速遊戲與精靈套件,現在我卡住了。 我有一個角色選擇屏幕,如果你持有角色,我想顯示角色的描述,但如果你只是觸摸角色,你可以選擇並使用角色。我已經有了播放/顯示描述的代碼。我只需要知道何時調用相應的函數,以及如何區分節點是否被觸摸。迅速skscene觸摸並舉行其他行動,而不僅僅是觸摸

在此先感謝

+0

你可以實現手勢識別器,或者你可以自己做。兩種方法都有其自身的缺點。識別器的問題是它們被添加到視圖中,而不是添加到節點。手動執行的問題是......它需要你做一些編碼:)但是你可以嘗試使用SKAction序列,這會在延遲後彈出一個描述。這個動作應該在touchesBegan中開始,並以touchesEnded結束。 – Whirlwind

+0

準確地說,這裏的延遲代表了印刷機應該在描述出現之前的時間。 – Whirlwind

+0

是的,謝謝這就是我如何顯示說明,但我如何做到這一點,如果你只是點擊它觸發一個不同的功能比如果你長時間按它 –

回答

0

所以,在這裏是如何使用SKActions做到這一點......另外,還有使用SKAction另一種方式,但我真的不能告訴你一切準備:)不管怎麼說,這裏是這你想要做什麼代碼:

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

    let kLongPressDelayActionKey = "longPressDelay" 
    let kLongPressStartedActionKey = "longPressStarted" 
    let kStoppingLongPressActionKey = "stoppingLongPressActionKey" 

    let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150)) 
    let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150)) 
    let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150)) 
    override func didMove(to view: SKView) { 

     addChild(desc) 
     addChild(button) 
     addChild(other) 

     button.position.y = -160 
     button.name = "button" 
     desc.alpha = 0.0 
     desc.name = "description" 
     other.name = "other" 
     other.alpha = 0.0 
     other.position.y = -320 

    } 

    private func singleTap(onNode:SKNode){ 

     other.alpha = other.alpha == 0.0 ? 1.0 : 0.0 
    } 

    private func startLongPress(withDuration duration:TimeInterval){ 

     //How long does it take before long press is fired 
     //If user moves his finger of the screen within this delay, the single tap will be fired 
     let delay = SKAction.wait(forDuration: duration) 

     let completion = SKAction.run({ 
     [unowned self] in 

      self.desc.removeAction(forKey: self.kLongPressDelayActionKey) 
      self.desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey) 
     }) 

     self.desc.run(SKAction.sequence([delay,completion]), withKey: kLongPressDelayActionKey) 

    } 

    private func stopLongPress(){ 

     //Fire single tap and stop long press 
     if desc.action(forKey: kLongPressDelayActionKey) != nil{ 

      desc.removeAction(forKey: kLongPressDelayActionKey) 
      self.singleTap(onNode: self.other) 
     //or just stop the long press 
     }else{ 

      desc.removeAction(forKey: kLongPressStartedActionKey) 

      //Start fade out action if it isn't already started 
      if desc.action(forKey: kStoppingLongPressActionKey) == nil { 
       desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey) 
      } 
     } 
    } 

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

     //To stop the long press if we slide a finger of the button 
     if let touch = touches.first { 

      let location = touch.location(in: self) 

      if !button.contains(location) { 

       stopLongPress() 

      } 

     } 
    } 

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

     if let touch = touches.first { 

      let location = touch.location(in: self) 

      if button.contains(location) { 
       print("Button tapped") 

       startLongPress(withDuration: 1) 
      } 
     } 
    } 

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

     stopLongPress() 
    } 
} 

您可以運行的代碼,你會看到,如果你的紫色按鍵敲擊,一個黃色的框將彈出。如果再次點擊它,它會隱藏。此外,如果您將手指放在紫色框上1秒鐘,白色盒子就會褪色。鬆開手指時,它會淡出。另外,我已經實現了touchesMoved,所以如果你在藍色框出來時滑動你的紫色按鈕的手指,它也會淡出。

所以在這個例子中,我融合了長按和單擊。單擊是被認爲是不長按的一切。例如,如果用戶手指按住0.01秒或0.5秒或0.99秒,則它將被視爲單擊並且黃色框將彈出。如果你的手指保持> = 1秒,長按動作將被觸發。

另一種方法是使用手勢識別......,我可能會做一個例子,後來:)

0

這裏是如何使用手勢識別做到這一點:

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

    var longPressGesture:UILongPressGestureRecognizer! 
    var singleTapGesture:UITapGestureRecognizer! 

    let kLongPressStartedActionKey = "longPressStarted" 
    let kStoppingLongPressActionKey = "stoppingLongPressActionKey" 

    let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150)) 
    let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150)) 
    let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150)) 

    override func didMove(to view: SKView) { 

     longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(GameScene.longPress(_:))) 
     singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(GameScene.singleTap(_:))) 
     self.view?.addGestureRecognizer(longPressGesture) 
     self.view?.addGestureRecognizer(singleTapGesture) 
     addChild(desc) 
     addChild(button) 
     addChild(other) 

     button.position.y = -160 
     button.name = "button" 
     desc.alpha = 0.0 
     desc.name = "description" 
     other.name = "other" 
     other.alpha = 0.0 
     other.position.y = -320 

    } 

    private func stopLongPress(){ 



     desc.removeAction(forKey: self.kLongPressStartedActionKey) 

     //Start fade out action if it isn't already started 
     if desc.action(forKey: kStoppingLongPressActionKey) == nil { 
      desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey) 
     } 

    } 

    func singleTap(_ sender:UITapGestureRecognizer){ 


     if sender.state == .ended { 

      let location = convertPoint(fromView: sender.location(in: self.view)) 

      if self.button.contains(location) { 
        self.other.alpha = self.other.alpha == 0.0 ? 1.0 : 0.0 
      } 
     } 
    } 

    func longPress(_ sender: UILongPressGestureRecognizer) { 
     let longPressLocation = convertPoint(fromView: sender.location(in: self.view)) 

     if sender.state == .began { 

      if button.contains(longPressLocation) { 
       desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey) 
      } 

     }else if sender.state == .ended { 
      self.stopLongPress() 
     }else if sender.state == .changed { 


      let location = convertPoint(fromView: sender.location(in: self.view)) 


      if !button.contains(location) { 

       stopLongPress() 

      } 

     } 
    } 
}