4

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

Community
  • 1
  • 1
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32

2 Answers2

4

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

let moveRight = SKAction.reversedAction(moveLeft)()
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • 2
    You are absolutely correct. This function *looks* misleading, I would say `moveLeft.reversedAction()` is more clear. – Erik Jun 07 '14 at 05:41
  • Thanks! It look like it's gonna take some time before I get up to scratch in Swift. :-p – T. Benjamin Larsen Jun 07 '14 at 05:44
  • @nickfalk Same here actually, this wasn't obvious to me either. I just printed the action variables and found that one wasn't what I expected, which led me to this. Glad it helped though. – Mick MacCallum Jun 07 '14 at 05:46
1

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)
Dash
  • 17,188
  • 6
  • 48
  • 49
  • Thanks, I feel this case gives a good incentive to use explicit types, at least until I get used to accessing the API's through swift. After reading the SKAction documentation I'm still struggling to wrap my head around the logic. Should I even have been allowed to do `SKAction.reversedAction(action)`? This method is an instance method but I call it on the class itself(?) – T. Benjamin Larsen Jun 07 '14 at 19:38
  • 1
    SKAction.reversedAction(moveLeft) is the same thing as moveLeft.reversedAction. The first syntax is asking the class for the reversedAction method for the moveLeft instance. It returns the method and then needs to be called with an extra "()". – Dash Jun 07 '14 at 19:49
  • I updated my answer to have the more clear syntax, but the logic is the same. – Dash Jun 07 '14 at 19:49