我是编程新手,并且一直在使用swift和spritekit的在线课程试图从头开始创建我的第一个工作游戏。
目前我在尝试创建一系列独立运行的函数时遇到问题,请等待,以便一次只运行一个函数,并无限循环。
第一个功能:
func shootTwentyArrows() {
var oneArrow = SKAction.runBlock{
self.shootArrow()
}
var waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
var fireAnArrow = SKAction.sequence([oneArrow, waitBetweenArrows])
self.runAction(SKAction.repeatAction(fireAnArrow, count: 20))
}
第二个功能:
func shootSpiral1() {
var leftArrow = SKAction.runBlock{
self.arrowFromLeft()
}
var rightArrow = SKAction.runBlock{
self.arrowFromRight()
}
var waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
var sequence = SKAction.sequence([leftArrow, waitBetweenArrows, rightArrow, waitBetweenArrows])
var spiral1 = SKAction.repeatAction(sequence, count: 5)
self.runAction(spiral1)
澄清一下,我试图运行类似的东西: shootTwentyArrows() 什么时候完成,shootSpiral1(),当它完成时重复。
提前感谢您的回复。
答案 0 :(得分:1)
我想最正确的方法是重构代码:
func shootTwentyArrows() -> SKAction {
let oneArrow = SKAction.runBlock{
self.shootArrow()
}
let waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
let fireAnArrow = SKAction.sequence([oneArrow, waitBetweenArrows])
return SKAction.repeatAction(fireAnArrow, count: 20)
}
func shootSpiral1() -> SKAction {
let leftArrow = SKAction.runBlock{
self.arrowFromLeft()
}
let rightArrow = SKAction.runBlock{
self.arrowFromRight()
}
let waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
let sequence = SKAction.sequence([leftArrow, waitBetweenArrows, rightArrow, waitBetweenArrows])
let spiral1 = SKAction.repeatAction(sequence, count: 5)
return spiral1
}
然后在代码中的某处你可以做类似的事情:
let spiralAction = shootSpiral1()
let oneArrowAction = shootTwentyArrows()
let sequence = SKAction.sequence([spiralAction, oneArrowAction])
let infiniteSequence = SKAction.repeatActionForever(sequence)
self.runAction(infiniteSequence)
我故意将功能名称保留为相同,因此您可以理解。
P.S。将变量声明为let
是一种常见做法,只有在以后需要修改时才将其声明为var
。
答案 1 :(得分:0)
执行此操作的最佳方法是使用一个调用两个函数的SKAction序列。您可以在任何想要初始化箭头拍摄操作的地方调用此SKAction,例如viewDidLoad
。这将是调用操作的代码......
var actionShootingArrows = SKAction.sequence([shootSpiral1(), shootTwentyArrows()])
self.runAction(SKAction.repeatActionForever(actionShootingArrows))
希望这有帮助!