【发布时间】:2015-11-09 03:17:48
【问题描述】:
我想在每次点击屏幕时增加一个CGFloat。
浮点数有一个设定的最大值和最小值。
我试图通过这个建议:StackOverflow
但是,这只会在进行触摸或达到最大值后增加 CGFloat。我想在在触摸时增加 CGFloat,这意味着你触摸的时间越长,Jump/CGFloat 就越高。
问题可能在于冲动,应用后无法更改。也就是说,当“玩家”获得 20 的脉冲后,触摸屏幕的时间更长,浮点数可能会增加,但脉冲不会增加。
如果您查看我当前的代码,则在触摸屏幕时将脉冲设置为最大,但如果释放,则应删除该操作。然而,它不起作用,冲动并没有停止。
我知道你可以在按压后将身体的速度设置为一个值,如果按压结束,速度会回到 0,所以它会停止“跳跃”,但这看起来不太顺畅就像冲动一样。
有人有解决办法吗?
struct Constants {
static let minimumJumpForce:CGFloat = 20.0
static let maximumJumpForce:CGFloat = 60.0
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var force: CGFloat = 20.0
func longPressed(longPress: UIGestureRecognizer) {
if (longPress.state == UIGestureRecognizerState.Began) {
println("Began")
self.pressed = true
let HigherJump = SKAction.runBlock({Player.physicsBody?.applyImpulse(CGVectorMake(0, Constants.maximumJumpForce))})
self.runAction(HigherJump , withKey:"HighJump")
}else if (longPress.state == UIGestureRecognizerState.Ended) {
println("Ended")
self.pressed = false
self.removeActionForKey("HighJump")
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
【问题讨论】:
标签: ios swift sprite-kit physics