Failed to create SKActions array

I am experimenting with SpriteKit in Swift, but for some reason seems unable to create an array of actions to use in a sequence. I split it to try to identify the problem, but so far no luck.

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 I try to create an actions array, I get the error message โ€œIt is not possible to convert the type of the expressionโ€œ Array โ€to the typeโ€œ ArrayLiteralConvertible. โ€So, I thought that I might need a more explicit one and try to change it to

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

This seemed to knock down the house, not in a good way, causing SourceKit to complete the error ...

+4
source share
2 answers

You add the function to the array for moveRight, and not to SKAction itself. Try using this instead:

 let moveRight = SKAction.reversedAction(moveLeft)() 
+4
source

When you create moveRight, you are actually generating a function. You can call the function with "()" to get the actual SKAction. I added explicit types for two SKAction, so itโ€™s clear that they can be placed in 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) 
+1
source

Source: https://habr.com/ru/post/976185/


All Articles