2017-07-25 27 views
-1

我有一個遊戲,我的角色從一塊土地跳到土地,問題是隻要用戶點擊屏幕,他會繼續跳起來。如何只讓你的角色跳一次?

Knight.physicsBody?.velocity = CGVector(dx: 0, dy: 0) 
Knight.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 275)) 

這是我的touchesBegan下,讓他在第一跳的地方,我怎麼做就那麼一次,他所接觸與SKSprite節點只允許他跳一次? 只要屏幕被點擊,角色就可以跳轉,但是我希望他只能在地面上跳躍,然後用戶點擊屏幕

+1

可能的重複[Swift:如何讓我的角色在我的遊戲中只有在他碰到地面時才能跳躍?](https://stackoverflow.com/questions/45290346/swift-how-to-make-我的角色在我的遊戲中只能跳躍一次他擊中) –

+0

請添加此信息(字符可以跳躍,只要屏幕輕點,而不是一次)到您的較早問題,因爲它們密切相關。 –

+0

@SteveIves我更新了問題 – Luyer

回答

0

您是否嘗試過使用節點位置檢查觸摸位置? 從toughBegan獲取觸摸位置:

let point = touch.location(in: self) 

如果在這一點上一個節點等於你的對象,進行跳

let nodes = self.nodes(at: point) 
+0

這對手頭的問題沒有幫助。 – Knight0fDragon

3

我們將使用一個計數器。所以每次你打到一個平臺時,加上,每次你離開,減去。當你的計數器爲0時,你不在陸地上。

在某處爲身體類型創建一個枚舉......我把它放在我的GameScene類的正上方。

enum BodyType:UInt32 { 
    case player = 1 
    case platform = 2 
} 

設置你的騎士類這樣...

class Knight: SKSpriteNode { 
// this variable will be toggled by didBegin Contact ... when 0 you are not land. Assumes that player starts on land. // 
var onLandCounter = 1 

    func setUpKnight() { 
      /// call this in didMoveToView when setting up your nodes 
      /// Should have other stuff in here too .. but this is the important bit // 
     self.physicsBody?.categoryBitMask = BodyType.player.rawValue 

     self.physicsBody?.collisionBitMask = BodyType.platform.rawValue 

     self.physicsBody?.contactTestBitMask = BodyType.platform.rawValue 


    } 

    func jump() { 
     if (onLandCounter >= 1) { 

      self.physicsBody?.velocity = CGVector(dx: 0, dy: 0) 
      self.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 275)) 
     } 
     } 
} 
中的touchesBegan

,你反而會做

theKnight.jump() 

注意,假定您創建了騎士類的一個實例,並呼籲它theKnight

訣竅然後是你需要一個平臺類,你會自定義類你的地面體例如

enter image description here

class Platform: SKSpriteNode { 
     func setUpPlatform() { 

      self.physicsBody?.categoryBitMask = BodyType.platform.rawValue 
      self.physicsBody?.collisionBitMask = BodyType.player.rawValue | BodyType.platform.rawValue 
      self.physicsBody?.contactTestBitMask = BodyType.player.rawValue 

     } 
    } 

然後使用didBeginContact使計數器來改變你的theKnight實例,並允許跳,因爲騎士已經觸及地面再次發生。我們添加並且現在計數器至少爲1,所以我們可以跳轉。

例如

@objc(didBeginContact:) func didBegin(_ contact: SKPhysicsContact) { 

      let firstBody: SKPhysicsBody 
      let secondBody: SKPhysicsBody 

      if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
       firstBody = contact.bodyA 
       secondBody = contact.bodyB 
      } else { 
       firstBody = contact.bodyB 
       secondBody = contact.bodyA 
      } 

      //MARK: KNIGHT with PLATFORM 

      if (firstBody.categoryBitMask == BodyType.player.rawValue && secondBody.categoryBitMask == BodyType.platformSurface.rawValue) { 


       if let theKnight:Knight = firstBody.node as? Knight, 
       let thePlatform:Platform = secondBody.node as? Platform 
       { 
        theKnight.onLandCounter += 1 
       } 

      } 
    } 

記住在你的GameScene正確

class GameScene: SKScene, SKPhysicsContactDelegate { 
var theKnight: Knight = Knight() 


    override func didMove(to view: SKView) { 
     self.enumerateChildNodes(withName: "//*") { 
      node, stop in 

     if let thePlatform:Platform = node as? Platform { 

       thePlatform.setUpPlatform() 

     } 
    } 

    // MARK: SET UP PLAYER 

    // Assumes you call your player in the Scene "Player1" 
    //AND! you set it's custom class to Knight 

     if (self.childNode(withName: "Player1") != nil) { 

      theKnight = self.childNode(withName: "Player1") as! Knight 


      theKnight.setUpKnight()  
    } 
    } 
} 

最後一組的東西了!在didEndContact

@objc(didEndContact:) func didEnd(_ contact: SKPhysicsContact) { 

      let firstBody: SKPhysicsBody 
      let secondBody: SKPhysicsBody 

      if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
       firstBody = contact.bodyA 
       secondBody = contact.bodyB 
      } else { 
       firstBody = contact.bodyB 
       secondBody = contact.bodyA 
      } 

      //MARK: KNIGHT with PLATFORM 

      if (firstBody.categoryBitMask == BodyType.player.rawValue && secondBody.categoryBitMask == BodyType.platformSurface.rawValue) { 


       if let theKnight:Knight = firstBody.node as? Knight, 
       let thePlatform:Platform = secondBody.node as? Platform 
       { 
        theKnight.onLand -= 1 
       } 

      } 
    } 

不要忘了自定義類你的騎士!

enter image description here

現在你有一個設置...在跳轉功能纔會啓用時,您的播放器,使一個「勤」節點接觸上。

調整此解決方案,然而它適合您自己的代碼。讓我知道,如果我忘了支撐或什麼。

重點是....你只是使用計數器來防止使用跳轉功能。

+0

如果他掉下平臺,你的騎士可以跳下去 – Knight0fDragon

+0

這個人在複製線程中給你答案。我只是更詳細地介紹了代碼的例子。我的回答與以下概念是平行的:「你可以用一個布爾值追蹤與陸地的碰撞,並在跳躍發生後將其設爲假--4Kman」。 –

+0

我更新了我的解決方案,完全像那樣。你採取我的基本想法,幷包括一個didEndContact部分。 –

相關問題