只在按下时让 Sprite 移动?
Making a Sprite move only when pressing?
我在屏幕上有一个圆圈,当我按下屏幕右侧时它会向右移动,当我按下屏幕左侧时它会向左移动。现在我需要做到这一点,当我开始触摸时,我的圆圈向右移动,但如果我松开触摸,那么圆圈将停止,直到我再次按下屏幕。
这是我的 touchesBegan 函数。
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {
let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft)
}
else {
let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight)
}
}
这是我目前的情况。
基本上是这样的:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {
let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft, withKey:"moveLeft")
}
else {
let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight withKey:"moveRight")
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
Ball.removeActionForKey("moveLeft")
Ball.removeActionForKey("moveRight")
}
我建议您在每次需要时都对密钥进行更智能的处理,而不是将它们硬编码为字符串,但这是要点。
我在屏幕上有一个圆圈,当我按下屏幕右侧时它会向右移动,当我按下屏幕左侧时它会向左移动。现在我需要做到这一点,当我开始触摸时,我的圆圈向右移动,但如果我松开触摸,那么圆圈将停止,直到我再次按下屏幕。
这是我的 touchesBegan 函数。
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {
let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft)
}
else {
let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight)
}
}
这是我目前的情况。
基本上是这样的:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {
let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft, withKey:"moveLeft")
}
else {
let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight withKey:"moveRight")
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
Ball.removeActionForKey("moveLeft")
Ball.removeActionForKey("moveRight")
}
我建议您在每次需要时都对密钥进行更智能的处理,而不是将它们硬编码为字符串,但这是要点。