T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6383

Unable to create array of SKActions

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...

Upvotes: 4

Views: 706

Answers (2)

Dash
Dash

Reputation: 17408

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)

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130183

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

let moveRight = SKAction.reversedAction(moveLeft)()

Upvotes: 4

Related Questions