2015-10-18 44 views
1

我正在使用SpriteKit + Swift製作一個簡單的Pong遊戲。即使手指已經觸摸顯示屏,我也試圖移動這兩個撥片。我有一個建議將屏幕分成兩個View Controller。這會有幫助嗎?如果是這樣,這將如何完成? Link to previous question.將屏幕分割爲兩個視圖控制器SpritKit + Swift

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.first as UITouch? 
    let touchLocation = touch?.locationInNode(self) 
    let body: SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation!) 

    if body?.node!.name == PaddleCategoryName { 
     print("Paddle Touched!") 
     fingerIsOnPaddle = true 
    } 

    if body?.node!.name == PaddleCategoryName2 { 
     print("Paddle2 Touched!") 
     fingerIsOnPaddle2 = true 
    } 


} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if fingerIsOnPaddle { 
     let touch = touches.first as UITouch? 
     let touchLocation = touch?.locationInNode(self) 
     let previousTouchLocation = touch?.previousLocationInNode(self) 

     let paddle = self.childNodeWithName(PaddleCategoryName) as! SKSpriteNode 

     var newYPosition = paddle.position.y + (touchLocation!.y - previousTouchLocation!.y) 

     newYPosition = max(paddle.size.height/2, newYPosition) 
     newYPosition = min(self.size.height - paddle.size.height/2, newYPosition) 

     paddle.position = CGPointMake(paddle.position.x, newYPosition) 

    } 

    if fingerIsOnPaddle2 { 
     let touch = touches.first as UITouch? 
     let touchLocation = touch?.locationInNode(self) 
     let previousTouchLocation = touch?.previousLocationInNode(self) 

     let paddle2 = self.childNodeWithName(PaddleCategoryName2) as! SKSpriteNode 

     var newYPosition = paddle2.position.y + (touchLocation!.y - previousTouchLocation!.y) 

     newYPosition = max(paddle2.size.height/2, newYPosition) 
     newYPosition = min(self.size.height - paddle2.size.height/2, newYPosition) 

     paddle2.position = CGPointMake(paddle2.position.x, newYPosition) 
    } 
} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    fingerIsOnPaddle = false 
    fingerIsOnPaddle2 = false 
} 
+1

我不確定那真的是你想要做的。您可能會更好地獲取觸摸位置,因此您可以使用該位置來查看它是否位於iDevice或其他位置。 'touchesMoved'也是一樣,只要看看觸摸位置在哪裏,然後在那邊移動槳。 – Gliderman

+0

我確實嘗試過,但仍然無法使用。 –

+0

你遇到了什麼錯誤?你能夠展示一些代碼嗎? – Gliderman

回答

0

與您所提供的代碼的主要問題是,你是使用第一觸摸,當你有兩個人走動槳這是不好的。我其實不會使用touchesBegantouchesEnded。我假定遊戲場沿水平方向將有x,所以512(默認場景寬度除以2)將是中間線,其中槳位於x = 0x = 1024

以下代碼位於touchesMoved中,並處理移動的每個觸摸。如果觸摸位置在屏幕的一側,則會獲得該側的槳並移動它。您也可以將此代碼放在touchesBegan方法中,以便用戶可以觸摸他們想要的槳的位置。你可以回到你的槳檢測方法(除了不使用任何觸摸之外,我沒有看到任何問題),但我建議使用paddle.containsPoint來測試觸摸是否在裏面。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: UITouch in touches { 
     let location = touch.locationInNode(self) 
     if location.x < 512 { 
      let paddle = self.childNodeWithName(PaddleCategoryName) as! SKSpriteNode 
      paddle.position.y = location.y 
     } else { 
      let paddle2 = self.childNodeWithName(PaddleCategoryName2) as! SKSpriteNode 
      paddle2.position.y = location.y 
     } 
    } 
} 

我目前正在下載Xcode 7以獲取Swift 2,並且我將使用適當的語法編輯它。但就目前而言,只需進行一些更改(如方法調用),它就可以工作。

+0

非常感謝!有用! –

相關問題