当在 Swift Spritekit 中触摸并按住屏幕时,我如何向上移动我的节点?
How would I move my node up when there is a touch and hold on the screen in Swift Spritekit?
当用户触摸屏幕并按住它时,我希望 SKSpriteNode
飞起来,当用户停止触摸后,它再次落到地上。现在我正在使用点击来向上移动节点并在 touchesEnded 中将重力设置为 false 以使其落到地面。我将如何使它起作用。谢谢!
var touchingScreen = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
touchingScreen = true
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
touchingScreen = false
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
touchingScreen = false
}
override func update(currentTime: CFTimeInterval) {
if touchingScreen {
// Adjust the CGVector to suit your needs.
heroNode.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 10))
}
}
首先,您需要跟踪用户何时触摸并按住屏幕。你可以这样做,在你的 SKScene
:
var touchingScreen = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
touchingScreen = true
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
touchingScreen = false
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
touchingScreen = false
}
其次,您现在需要让对象在用户按下屏幕时向上移动,再次在您的 SKScene
:
override func update(currentTime: CFTimeInterval) {
if touchingScreen {
// Adjust the CGVector to suit your needs.
sprite.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 50))
}
}
或者,如果您希望对象以恒定速度向上移动,请替换:
sprite.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 50))
和
sprite.physicsBody!.velocity = CGVector(dx: yourDx, dy: yourDy)
希望对您有所帮助!
当用户触摸屏幕并按住它时,我希望 SKSpriteNode
飞起来,当用户停止触摸后,它再次落到地上。现在我正在使用点击来向上移动节点并在 touchesEnded 中将重力设置为 false 以使其落到地面。我将如何使它起作用。谢谢!
var touchingScreen = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
touchingScreen = true
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
touchingScreen = false
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
touchingScreen = false
}
override func update(currentTime: CFTimeInterval) {
if touchingScreen {
// Adjust the CGVector to suit your needs.
heroNode.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 10))
}
}
首先,您需要跟踪用户何时触摸并按住屏幕。你可以这样做,在你的 SKScene
:
var touchingScreen = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
touchingScreen = true
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
touchingScreen = false
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
touchingScreen = false
}
其次,您现在需要让对象在用户按下屏幕时向上移动,再次在您的 SKScene
:
override func update(currentTime: CFTimeInterval) {
if touchingScreen {
// Adjust the CGVector to suit your needs.
sprite.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 50))
}
}
或者,如果您希望对象以恒定速度向上移动,请替换:
sprite.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 50))
和
sprite.physicsBody!.velocity = CGVector(dx: yourDx, dy: yourDy)
希望对您有所帮助!