我有一个触摸开始的方法,但我不想让精灵在触摸两次时再次跳跃?
I have a touches began method but I don't want the sprite to jump again if touched twice?
我有这个 touches begin 方法,它会导致精灵跳跃。
如果我继续点击精灵,它会不断上升。我想停止这个,但我还需要能够在大约 0.8 秒后跳过另一个物体。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
}
当您第一次检测到触摸时,设置一个标志,这意味着触摸被禁止跳跃并启动计时器。当计时器触发时,您可以重置标志。在您的 touches begin 方法中,在允许新的跳跃之前检查标志。
请注意,如果游戏有暂停,那么您需要考虑计时器如何与其交互。
我建议你使用一个 bool 来知道玩家是否应该跳跃,并使用一个 NSTimer 来设置这个 bool,像这样:
var shouldJump = true;
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if (shouldJump){
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
shouldJump = false;
var timer = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self, selector: Selector("updateState"), userInfo: nil, repeats: false)
}
}
func updateState() {
shouldJump = true;
}
使用变量丢弃触摸:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if touchEnabled {
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
touchEnabled = false
}
}
}
并在你的update
函数中设置为真,你可以检查你的精灵是否在休息:
if hurdie.physicsBody?.resting {
touchEnabled = true
}
我有这个 touches begin 方法,它会导致精灵跳跃。 如果我继续点击精灵,它会不断上升。我想停止这个,但我还需要能够在大约 0.8 秒后跳过另一个物体。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
}
当您第一次检测到触摸时,设置一个标志,这意味着触摸被禁止跳跃并启动计时器。当计时器触发时,您可以重置标志。在您的 touches begin 方法中,在允许新的跳跃之前检查标志。
请注意,如果游戏有暂停,那么您需要考虑计时器如何与其交互。
我建议你使用一个 bool 来知道玩家是否应该跳跃,并使用一个 NSTimer 来设置这个 bool,像这样:
var shouldJump = true;
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if (shouldJump){
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
shouldJump = false;
var timer = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self, selector: Selector("updateState"), userInfo: nil, repeats: false)
}
}
func updateState() {
shouldJump = true;
}
使用变量丢弃触摸:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if touchEnabled {
if moving.speed > 0 {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
hurdie.physicsBody?.velocity = CGVectorMake(0, 0)
hurdie.physicsBody?.applyImpulse(CGVectorMake(0, 30.0))
touchEnabled = false
}
}
}
并在你的update
函数中设置为真,你可以检查你的精灵是否在休息:
if hurdie.physicsBody?.resting {
touchEnabled = true
}