[英]One touch moving in 2 points, but it is not moving
请帮忙! 我想让我的球在将屏幕触摸到其他位置时移动,并在下次触摸到原始位置时移动。 我只想更改X的位置,Y将会相同。 但是,当我触摸屏幕时,它什么也没做。 这是我编写的代码。
override func didMoveToView(view: SKView) {
addball()
}
func addball() {
ball = SKSpriteNode(imageNamed: "Ball")
self.ball.position = CGPointMake(frame.size.width/4, frame.size.height/3)
self.addChild(ball)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
if touches.count % 2 == 0 {
let right = SKAction.moveToX(frame.size.width/4*3, duration: 0.1)
self.ball.runAction(right)
} else {
let left = SKAction.moveToX(frame.size.width/4, duration: 0.1)
self.ball.runAction(left)
}
}
}
感谢所有答案:)
这可能是一个愚蠢的答案,但是您是否同时用两根手指触摸它?
尝试执行touches.count> 0来测试它是否仅仅是触摸。
如果您触摸了一根手指,那么另一只手指的触摸数将保持为1,因为只有1次触摸开始事件发生了。
第一次触摸是移动状态的一部分,第二次触摸是您的开始状态
编辑:
var toggle = false;
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
if toggle {
let right = SKAction.moveToX(frame.size.width/4*3, duration: 0.1)
self.ball.runAction(right)
} else {
let left = SKAction.moveToX(frame.size.width/4, duration: 0.1)
self.ball.runAction(left)
}
}
toggle = !toggle;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.