我有一个x位置的数组,并希望将精灵从它的当前位置移动到右边的下一个xPosition(每2秒移动精灵,持续时间为一秒)只有当位置是如果位置是空的,我想在相反的方向(左)移动精灵。我希望这能永远重复,直到精灵被摧毁。我不太清楚如何解决这个问题。以下是我能够做到的事情:
for i in 1..<xPositions.count {
let delayAction = SKAction.waitForDuration(2.0)
if freePositions.contains(xPositions[indexOfCurrentSpritePosition! + i]) {
let spriteSpriteToRight = SKAction.moveToX(CGFloat(xPositions[indexOfCurrentSpritePosition! + i]), duration: 1.0)
let sequence = SKAction.sequence([moveSpriteToRight, delayAction])
sprite.runAction(sequence)
}
else if !freePositions.contains(xPositions[indexOfCurrentSpritePosition! + i]) {
let moveSpriteToLeft = SKAction.moveToX(CGFloat(xPositions[indexOfCurrentSpritePosition! - i]), duration: 1.0)
let sequence = SKAction.sequence([moveSpriteToLeft, delayAction])
sprite.runAction(sequence)
}
}