一触移动2点,但不动
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)
}
}
}
感谢大家的回答:)
这可能是一个愚蠢的回答,但您是否同时用 2 根手指触摸它?
尝试 touches.count > 0 来测试它是否只是触摸。
如果您先触摸 1 个手指,然后再触摸另一个手指,您的触摸计数将保持为 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;
}
请帮忙! 当屏幕被触摸到其他位置并在下一次触摸到原始位置时,我想让我的球移动。 我只想更改 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)
}
}
}
感谢大家的回答:)
这可能是一个愚蠢的回答,但您是否同时用 2 根手指触摸它?
尝试 touches.count > 0 来测试它是否只是触摸。
如果您先触摸 1 个手指,然后再触摸另一个手指,您的触摸计数将保持为 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;
}