0
我遇到了我设置的SKAction序列的问题。Swift中Sprite Kit SKAction的问题
该序列的目标是获取9个精灵节点的数组,并一次显示一个节点。然后,该序列将显示当前节点右侧或左侧的节点,具体取决于按下哪个按钮(右按钮或左按钮)。
我在这里遇到一些有趣的事情。它看起来像现在一样工作,但是如果我多次快速按下按钮左侧或右侧按钮,似乎无法跟上并且进程失败。我没有收到错误,精灵节点只是不显示或不正确显示。
我附上我的代码如下。因为它在慢慢循环时正在工作,所以我认为这是一个时间问题,也许我需要添加一个完成块。我试过这样做,但没有成功。
我的问题是,是否有任何明显突出显示错误在这里,你认为完成块可能可以解决这个问题?
func pressedButtonRight(){
if currentDisplayedWorld < 8 {
var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode
var nextWorld:SKSpriteNode = worldArray[currentDisplayedWorld + 1] as SKSpriteNode
nextWorld.position = CGPointMake(self.frame.size.width, 50)
self.addChild(nextWorld)
let move = SKAction.moveByX(-self.frame.size.width, y: 0,
duration: 1.0, delay: 0,
usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
//currentWorld.runAction(move)
nextWorld.runAction(move)
currentWorld.removeFromParent()
currentDisplayedWorld++
}else if currentDisplayedWorld == 8 {
}
}
func pressedButtonLeft(){
if currentDisplayedWorld > 0 {
var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode
var previousWorld:SKSpriteNode = worldArray[currentDisplayedWorld - 1] as SKSpriteNode
previousWorld.position = CGPointMake(-self.frame.size.width, 50)
self.addChild(previousWorld)
let moveBack = SKAction.moveByX(self.frame.size.width, y: 0,
duration: 1.0, delay: 0,
usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
//currentWorld.runAction(moveBack)
previousWorld.runAction(moveBack)
currentWorld.removeFromParent()
currentDisplayedWorld--
}else if currentDisplayedWorld == 0 {
}
}
有一个叫做moving的类属性布尔值。如果移动是错误的,则只移动节点。在将节点集移动到true之前。使用完成处理程序在节点上运行移动操作,该处理程序将再次移动为false。 – Okapi 2014-12-05 07:14:12
因此,代码实际上并不会停止任何已经运行的操作。如果您快速按下按钮,则可能会同时执行多个移动操作。该行为是未定义的。使用键运行动作,以便运行动作可以自动停止已经运行的其他具有相同键的动作。 – LearnCocos2D 2014-12-05 09:24:47
好吧,这是有道理的。我今天会试试这个,并告诉你它是否成功。谢谢。 – 2014-12-05 14:13:40