作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作这个游戏,你应该与另一个人玩躲避球。问题是当我触摸 paddlei 时,我无法让 paddlei 移动。当我触摸桨时,它会移动,但它也会移动 paddlei。我可以做什么或改变什么?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
let touchi = touches.first
let touchLocationi = touchi!.locationInNode(self)
if let body = physicsWorld.bodyAtPoint(touchLocation) {
if body.node!.name == PaddleCategoryName {
print("Began touch on paddle")
isFingerOnPaddle = true
}
}
if let otherBody = physicsWorld.bodyAtPoint(touchLocationi) {
if otherBody.node!.name == PaddleyName {
print("EL otro toco")
isFingerOnPaddlei = true
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
isFingerOnPaddle = false
isFingerOnPaddlei = false
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isFingerOnPaddle {
// 2
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
let previousLocation = touch!.previousLocationInNode(self)
// 3
let paddle = childNodeWithName(PaddleCategoryName) as! SKSpriteNode
// 4
var paddleY = paddle.position.y + (touchLocation.y - previousLocation.y)
var paddleX = paddle.position.x + (touchLocation.x - previousLocation.x)
// 5
paddleY = max(paddleY, paddle.size.width/2)
paddleY = min(paddleY, size.height - paddle.size.height/2)
paddleX = max(paddleX, paddle.size.height/2)
paddleX = min(paddleX, size.height - paddle.size.width/2)
// 6
paddle.position = CGPoint(x: paddleX, y: paddleY)
if isFingerOnPaddlei {
let paddlei = childNodeWithName(PaddleyName) as! SKSpriteNode
// 4
var paddleS = paddlei.position.y + (touchLocation.y - previousLocation.y)
var paddleU = paddlei.position.x + (touchLocation.x - previousLocation.x)
// 5
paddleS = max(paddleS, paddlei.size.width / 2)
paddleS = min(paddleS, size.height - paddlei.size.height / 2)
paddleU = max(paddleU, paddlei.size.height / 2)
paddleU = min(paddleU, size.height - paddlei.size.width / 2)
// 6
paddlei.position = CGPoint(x: paddleU, y: paddleS)
}
}
}
}
最佳答案
if isFingerOnPaddle
位于 if 语句中,声明 if isFingerOnPaddle
。第二个 if 语句永远不会被调用,因为它是嵌套的,并且两者永远不应该同时为 true。
关于swift - 我怎样才能让两个球员在触球时都移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313225/
我是一名优秀的程序员,十分优秀!