玩家触发事件后如何添加延迟?

时间:2019-12-16 22:05:37

标签: swift sprite-kit game-development

如何在玩家触发跳跃之后添加延迟,以使跳跃不会被触发得太频繁(或被垃圾邮件)?

import scrapy
from scrapy_splash import SplashRequest
class ScrapySpider(scrapy.Spider):
    name = "spide1"
    start_urls = [
                'link1',
                'link2', 
                etc

4 个答案:

答案 0 :(得分:0)

一种简单的方法是使用布尔值标志,当播放器触发跳跃时该标志会改变,并在短暂的延迟后自动重置。

var canJump: Bool = True

func jumpRight() {
    guard canJump == True else { return }
    self.canJump = False
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: 100, dy: 500))
    DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
        self.canJump = True
    }
}

答案 1 :(得分:0)

有几种方法可以执行此操作,无论您在何处设置了跳转按钮,都需要添加选中目标(无论是向右跳转还是向左跳转),您都需要添加检查以监测按下跳转命令的时间以及跳转命令完成时。

我将从在您的函数上添加完成处理程序开始。

func jumpRight(arg: Bool, completion: (Bool) -> ()) {
    completion(arg) 
}

这样做可以确定函数何时完成。 下面是您调用跳转按钮命令的位置。无论您在何处定义了操作,都应调用此方法。

jumpRight(arg: true, completion: { (success) -> Void in
    print("Second line of code executed")
    if success { // this will be equal to whatever value is set in this method call
          print("true")
    } else {
         print("false")
    }
})

如果某人发送了垃圾邮件,则会从完成处理程序中打印“ false”。在该位置,您可以防止用户再次跳转,直到该功能定义的操作完成为止。

答案 2 :(得分:-1)

我的解决方案是尝试这样的事情

var lastUpdateTime: TimeInterval = 0
var dt: TimeInterval = 0
let jumpHysterisisTimeLimit: TimeInterval = 0.2
var jumpHysterisisTimer: TimeInterval = 0
var jumpAGoGo: Bool = false

func updateGameLoopTimer(currentTime: TimeInterval) {
  if lastUpdateTime > 0 {
    dt = currentTime - lastUpdateTime
  } else {
    dt = 0
  }
  lastUpdateTime = currentTime
}

override func update(_ currentTime: TimeInterval) {
    updateGameLoopTimer(currentTime: currentTime)
    isJumpAGoGo()
}

func isJumpAGoGo() {
  if jumpAGoGo {
    jumpHysterisisTimer += dt
    if jumpHysterisisTimer >= jumpHysterisisTimeLimit {
      jumpHysterisisTimer = 0
      jumpAGoGo = false
    }
  }
}

func jumpRight() {
  if !jumpAGoGo {
    jumpAGoGo = true
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: 100, dy: 500))
  }  
}

func jumpLeft() {
  if !jumpAGoGo {
    jumpAGoGo = true
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: -100, dy: 500)) 
  }
}

然后可以使用 jumpHysterisisTimeLimit 值来适应您的需求。

答案 3 :(得分:-1)

1)我将确定地面是什么,只有在地面上才跳。

func jumpRight() {
    if onGround == false {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: 100, dy: 500))  
}

func jumpLeft() {
    if onGround == false {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: -100, dy: 500))    
}

onGround可以检查一个物理物体是否存在于玩家的脚下,或者可以是您身上的特定y区域。

我希望检查身体。

rayStart是您的“脚”所在的地方

func isStandingOnGround(_ rayStart:CGPoint) -> Bool {
    let rayEnd = CGPoint(x: rayStart.x, y: rayStart.y + 1) // + 1 is distance to ground,  change it if a wider gap is needed.
    let body = scene.physicsWorld.body(alongRayStart: rayStart, end: rayEnd)
    return (body?.categoryBitMask ?? 0) & Category.Ground > 0
}

2)如果您需要添加“充值”,(对于那些不了解游戏机制的人来说,这是跳转之间出现“延迟”的地方),我会使用SKActions

func jumpRight() {
    if onGround == false || player.action(forKey:"jump") {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: 100, dy: 500))  
    player.run(SKAction.wait(forDuration:1), withKey:"jump")
}

func jumpLeft() {
    if onGround == false || player.action(forKey:"jump") {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: -100, dy: 500))    
    player.run(SKAction.wait(forDuration:1), withKey:"jump")
}

3)为了安全起见,还应包括y的速度为零(或某个不表示跳跃的阈值)

func jumpRight() {
    if onGround == false || player.action(forKey:"jump") || player.physicsBody?.velocity.dy != 0 {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: 100, dy: 500))  
    player.run(SKAction.wait(forDuration:1), withKey:"jump")
}

func jumpLeft() {
    if onGround == false || player.action(forKey:"jump") || player.physicsBody?.velocity.dy != 0 {return}
    player.physicsBody?.velocity = CGVector(dx: 0,dy: 0)
    player.physicsBody?.applyImpulse(CGVector (dx: -100, dy: 500))    
    player.run(SKAction.wait(forDuration:1), withKey:"jump")
}

由于特定的人在使用Sprite Kit时遇到困难,因此我将针对具体问题发布确切答案,因为他们无法推断出该问题。我不建议仅在跳跃时执行此操作,因为它允许两次跳跃或在跌倒时跳跃。

if !player.action(forKey:"jump”){
    player.run(SKAction.wait(forDuration:1), withKey:"jump")
    // insert your jump function calls here
}

您可以将这种技术用于任何种类或充电。

您甚至可以更进一步,用waitcustomActionWithDuration \ sequence代替group来创建动画,以补充充电时间。