Unable to create array of SKActions

那年仲夏 提交于 2019-12-12 12:12:15

问题


I'm experimenting with SpriteKit in Swift but somehow seem unable to create an array of actions to use in a sequence. I've split it up to try and pin-point the problem, but no luck so far.

func animateBackground(){
    let moveLeft = SKAction.moveByX(100, y: 0, duration: 3)
    moveLeft.timingMode = SKActionTimingMode.EaseInEaseOut
    let moveRight = SKAction.reversedAction(moveLeft)
    let actions = [moveLeft, moveRight] // <--- here there be dragons/trouble
    let sequence = SKAction.sequence(actions)
    let repeat = SKAction.repeatActionForever(sequence)
}

When trying to create the actions-array I get the error "Cannot convert the expression's type 'Array' to type 'ArrayLiteralConvertible' " So, I thought I might need to be more explicit and attempted to change it to

var actions: SKAction[] = [moveLeft, moveRight]

This seemed to bring down the house, and not in a good way, resulting in the SourceKit terminated bug...


回答1:


You're adding a function to the array for moveRight, not the SKAction itself. Try using this instead:

let moveRight = SKAction.reversedAction(moveLeft)()



回答2:


When you create moveRight you're actually generating a function. You can call the function with "()" to get the actual SKAction. I added explicit types to the two SKAction's so it's clear that they can be put in an SKAction[]:

let moveLeft:SKAction = SKAction.moveByX(100, y: 0, duration: 3)
moveLeft.timingMode = SKActionTimingMode.EaseInEaseOut
let moveRight:SKAction = moveLeft.reversedAction()
let actions = [moveLeft, moveRight]
let sequence = SKAction.sequence(actions)
let repeat = SKAction.repeatActionForever(sequence)


来源:https://stackoverflow.com/questions/24094018/unable-to-create-array-of-skactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!