如何根据触摸屏幕的时间使精灵跳跃higher/smaller?
How to make sprite jump higher/smaller depending on the amount of time touching screen?
您好,我很难找到适用于我的 sprite 的正确代码,以便仅在用胶带粘住时允许小跳,而在手指在屏幕上停留时间较长时允许跳得更高。 (请在下面找到当前代码)
override func touchesBegan(touches: Set<NSObject>, withEvent event:UIEvent) {
/* Called when a touch begins */
if (gameOver == 0){
//Player Begin Jumping.
player.physicsBody?.applyImpulse(CGVectorMake(0, 200))
player.runAction(SKAction .playSoundFileNamed("sounds/Jump.caf", waitForCompletion: true))
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (gameOver == 0){
//Player End Jump.
player.physicsBody?.applyImpulse(CGVectorMake(0, -120))
您可以使用 update
方法。在您的 touchesBegan 方法中,您设置了一个布尔值或类似的东西来显示您仍在屏幕上按下的 update
方法。例如:
//touchesBegan
touching = true
//update-method
if touching {
player.physicsBody?.applyImpulse(CGVectorMake(0, 1))
}
//touchesEnded
touching = false
您必须更改 applyImpulse
以使其符合您的需要。
这其实很简单,这意味着只要玩家松开屏幕他就会掉下来,如果他按住屏幕他会达到 200 否则他会掉下来
touches began {
sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: 200)
}
touches eneded {
sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: -57)
}
您好,我很难找到适用于我的 sprite 的正确代码,以便仅在用胶带粘住时允许小跳,而在手指在屏幕上停留时间较长时允许跳得更高。 (请在下面找到当前代码)
override func touchesBegan(touches: Set<NSObject>, withEvent event:UIEvent) {
/* Called when a touch begins */
if (gameOver == 0){
//Player Begin Jumping.
player.physicsBody?.applyImpulse(CGVectorMake(0, 200))
player.runAction(SKAction .playSoundFileNamed("sounds/Jump.caf", waitForCompletion: true))
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (gameOver == 0){
//Player End Jump.
player.physicsBody?.applyImpulse(CGVectorMake(0, -120))
您可以使用 update
方法。在您的 touchesBegan 方法中,您设置了一个布尔值或类似的东西来显示您仍在屏幕上按下的 update
方法。例如:
//touchesBegan
touching = true
//update-method
if touching {
player.physicsBody?.applyImpulse(CGVectorMake(0, 1))
}
//touchesEnded
touching = false
您必须更改 applyImpulse
以使其符合您的需要。
这其实很简单,这意味着只要玩家松开屏幕他就会掉下来,如果他按住屏幕他会达到 200 否则他会掉下来
touches began {
sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: 200)
}
touches eneded {
sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: -57)
}