gpt4 book ai didi

ios - 在 SpriteKit 节点上应用脉冲

转载 作者:可可西里 更新时间:2023-11-01 01:05:28 24 4
gpt4 key购买 nike

我正在开发一个 Sprite Kit 游戏,我有一个名为 hero 的节点,它会躲避接近的恶棍。我对 applyImpulse 的跳跃 Action 有疑问,在这种情况下,英雄可以反复跳跃和飞行,而不是躲避坏人。我使用了一个 bool 变量来更改定时器值的状态,使其在 3 秒内仅跳转一次,但这是行不通的。这是我的代码

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if isGameOver == true {
self.restart()
}

if isstarted {
self.runAction(SKAction.playSoundFileNamed("jump.wav", waitForCompletion: false))
for touch: AnyObject in touches{
if jumpon == false { //hero has not jumped
let location = touch.locationInNode(self)
heroAction()
jumpon = true //hero jumped
}
}

} else {
isstarted = true
hero.stop()
hero.armMove()
hero.rightLegMove()
hero.leftLegMove()
treeMove()
villanMove()
let clickToStartLable = childNodeWithName("clickTostartLable")
clickToStartLable?.removeFromParent()
addGrass()
// star.generateStarWithSpawnTime()
}
}

func changeJump(){
jumpon = false // hero has landed
}

每秒都会调用函数更新,然后必须调用 changeJump

 override func update(currentTime: CFTimeInterval) {

if isstarted == true {
let pointsLabel = childNodeWithName("pointsLabel") as MLpoints
pointsLabel.increment()
}

if jumpon == true { // checked everyframe that hero jumped
jumpingTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "changeJump", userInfo: nil, repeats: true) // changing jump status
}
}

我应该如何更新我的代码,让英雄在三秒内只跳一次。提前致谢。

最佳答案

一些想法...

  1. 我建议您在 Sprite Kit 游戏中使用一两个 SKAction 而不是 NSTimerSKAction在您暂停/恢复场景和/或查看时适本地暂停/恢复
  2. update 方法每秒被调用约 60 次,而不是每秒调用一次
  3. 不需要在update中检查跳转状态
  4. 或者,您可以在开始另一个跳跃序列之前检查英雄是否在地面上,而不是使用计时器。如果英雄在地上(并准备跳跃)但计时器仍在倒计时,游戏可能会让用户感到沮丧

还有一些代码...

if (!jumping) {
jumping = true
let wait = SKAction.waitForDuration(3)
let leap = SKAction.runBlock({
// Play sound and apply impulse to hero
self.jump()
})
let action = SKAction.group([leap, wait])
runAction(action) {
// This runs only after the action has completed
self.jumping = false
}
}

关于ios - 在 SpriteKit 节点上应用脉冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810138/

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