gpt4 book ai didi

ios - SKAction.waitForDuration 在 SpriteKit 中不起作用

转载 作者:行者123 更新时间:2023-11-28 13:16:13 25 4
gpt4 key购买 nike

我一直在为这个看似简单的功能而烦恼,我正试图将其构建到我正在使用 SpriteKit 和 Swift 开发的游戏中。我已经搜索过,但不明白为什么这不起作用。

我想做的:当玩家接触到敌人或障碍物时,我想加载并播放爆炸动画 createExplosion()(动画在 0.5 秒内完成),然后进入 gameOverAction()。问题是只显示了动画的第一帧,然后它进行到 gameOverAction()。我试图制作一个 SKAction.sequence,但 Xcode 不喜欢我在序列内调用函数,所以我找到了 SKAction.waitForDuration。问题是,它对代码的执行没有影响。这应该如何正确完成?

func collisionWithPlayer(playerObject: SKSpriteNode) {
runAction(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
createExplosion(playerObject)
playerObject.removeFromParent()
runAction(SKAction.waitForDuration(NSTimeInterval(0.5)))
gameOverAction()
}

这里是 gameOverAction() 的代码:

func gameOverAction()   {

backgroundMusicPlayer.stop()

globalHighScore = NSUserDefaults.standardUserDefaults().objectForKey("GameHighScore") as? Int

localHighScore = self.monstersDestroyed

if globalHighScore == nil {
globalHighScore = localHighScore
}

globalHighScore = self.recordHighScore(localHighScore, highScore: globalHighScore!)
println(globalHighScore!)

let reveal = SKTransition.fadeWithDuration(1.0)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
}

func createExplosion(object: SKSpriteNode) -> SKSpriteNode {

var explosionArray = Array<SKTexture>()
var explosionSprite = SKSpriteNode()

explosionArray.append(SKTexture(imageNamed: "explosionSprite01.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite02.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite03.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite04.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite05.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite06.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite07.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite08.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite09.png"))

let animateAction = SKAction.animateWithTextures(explosionArray, timePerFrame: 0.05)
explosionSprite = SKSpriteNode(texture:explosionArray[0])
explosionSprite.position = CGPoint(x: object.position.x + 10.0, y: object.position.y)
addChild(explosionSprite)
explosionSprite.runAction(SKAction.sequence([animateAction, SKAction.removeFromParent()]))
return(explosionSprite)
}

最佳答案

waitForDuration SKAction 可用于在 Action 序列中引入延迟,但它不会阻止调用 runAction 的线程 - 所以您的 waitForDuration Action 将被设置为在您的节点上运行,但 gameOverAction() 将立即被调用。

您可以使用 runAction:completion 的完成 block 在操作完成后执行 gameOverAction() -

func collisionWithPlayer(playerObject: SKSpriteNode) {
var actions = Array<SKAction>();
actions.append(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
actions.append(SKAction.waitForDuration(NSTimeInterval(0.5)))
actions.append(SKAction.removeFromParent())
createExplosion(playerObject)
let sequence = SKAction.sequence(actions);
playerObject.runAction(sequence,completion: { () -> Void in
self.gameOverAction()
})
}

根据 createExplosion 的作用,也可以将其合并到序列中并消除对 waitForDuration 的需要

关于ios - SKAction.waitForDuration 在 SpriteKit 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911737/

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