I am making a simple Pong game using SpriteKit + Swift. I am trying to move the two paddles even if a finger is already touching the display. I got a suggestion to split the screen into two View Controller. Would this help? If so, how would this be done? Link to previous question.
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
}
The primary problem with the code you have provided is that you are only using the first touch, which is not good when you have two people moving the paddles around. I would in fact not use touchesBegan
or touchesEnded
. I'm going to assume that the game field will have x
along the horizontal, so 512 (the default scene width divided by 2) will be the middle line, with the paddles positioned at x = 0
and x = 1024
.
The code below is in touchesMoved
, and handles each touch that moved. If the touch location is on one side of the screen, it gets the paddle for that side and moves it. You may wish to put this code in the touchesBegan
method as well, so that the user can touch where they want the paddle to go. You could go back to your paddle detection method (I didn't see anything wrong with it aside from not using every touch), but I would recommend using paddle.containsPoint
to test if the touch is inside.
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
}
}
}
I am currently downloading Xcode 7 to get Swift 2, and I will edit this with proper syntax. But for now, with a few changes (like to the method call), it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.