gpt4 book ai didi

ios - 长时间触摸时增加跳跃高度,直到最大高度

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:21 25 4
gpt4 key购买 nike

我使用以下方法让我的角色跳跃:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

if onGround && !gameOver {
self.character.physicsBody?.applyImpulse(CGVectorMake(0, 75))
self.onGround = false
}

}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

}

这很好用,但我想让角色根据触摸的长度跳到一定高度,直到达到最大值。我已经尝试过一些带有帧持续时间的东西,但这没有用。

如何让角色根据触摸长度跳到最大高度?

最佳答案

您可以在抬起手指时禁用 body 在 y 轴上的速度,如果您想限制最大跳跃高度,请使用可选变量来存储跳跃前的初始 y 位置,并检查当前 y 之间的增量定位到初始 y 位置:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if onGround && !gameOver {
if self.initialJumpY == nil {
self.initialJumpY = self.character.position.y
}

if self.character.position.y - self.initialJumpY < <Your cap value> {
self.character.physicsBody?.applyImpulse(CGVectorMake(0, 75))
} else {
self.character.physicsBody?.velocity.dy = 0.0
}

self.onGround = false
}

完成后重置:

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
self.character.physicsBody?.velocity.dy = 0.0
self.initialJumpY = nil
}

在定义触摸方法的节点类中声明 initialJumpY:

class MyNode: SKNode {

var initialJumpY: Float?

}

关于ios - 长时间触摸时增加跳跃高度,直到最大高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30223997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com