我正在使用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
}
我不確定那真的是你想要做的。您可能會更好地獲取觸摸位置,因此您可以使用該位置來查看它是否位於iDevice或其他位置。 'touchesMoved'也是一樣,只要看看觸摸位置在哪裏,然後在那邊移動槳。 – Gliderman
我確實嘗試過,但仍然無法使用。 –
你遇到了什麼錯誤?你能夠展示一些代碼嗎? – Gliderman