【发布时间】:2015-10-21 09:32:00
【问题描述】:
我正在使用 SpriteKit + Swift 制作一个简单的 Pong 游戏。即使手指已经在触摸显示屏,我也在尝试移动两个拨片。我得到了将屏幕分成两个视图控制器的建议。这会有帮助吗?如果是这样,这将如何完成? 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
}
【问题讨论】:
-
我不确定这是否真的是您想要做的。您可能会更好地获取触摸的位置,因此您可以使用该位置来查看它是在 iDevice 的一侧还是另一侧。
touchesMoved也是如此,只需查看触摸的位置,然后移动该侧的拨片即可。 -
我试过了,但还是不行。
-
你遇到了什么错误?你能展示一些代码吗?
-
基本上,我制作了两个倾斜的LeftSide和RightSide节点。如果当时按下了 LeftSide,它会发现我是否按下了左侧的 Paddle1,反之亦然。当我运行更改时,我仍然遇到同样的问题。
-
我在问题中添加了代码。
标签: ios swift sprite-kit