Reputation: 41
I have a sprite animation of 30 frames, 6 of these frames are keyframes and the rest are transitions from keyframe to keyframe.
I am trying to figure out how to do the following:
When button is pressed the sprite starts animating. Animation will run at least once and then it will stop the animation on a randomly chosen keyframe.
When button is pressed again, animation will start from the last chosen keyframe and will run 1 complete cycle and then stops again on a randomly chosen keyframe.
I've found a similar question here but that was asked in 2014. Did Swift and Spritekit change enough in 3 years to make it possible?
Edit: @Alessandro Ornano I've tried this:
var roll1Textures = ["Roll1Motion0", "Roll1Motion1", "Roll1Motion2", "Roll1Motion3", "Roll1Motion4", "Roll1Motion5", "Roll1Motion6", "Roll1Motion7", "Roll1Motion8", "Roll1Motion9", "Roll1Motion10", "Roll1Motion11", "Roll1Motion12", "Roll1Motion13", "Roll1Motion14", "Roll1Motion15", "Roll1Motion16", "Roll1Motion17", "Roll1Motion18", "Roll1Motion19", "Roll1Motion20", "Roll1Motion21", "Roll1Motion22", "Roll1Motion23", "Roll1Motion24", "Roll1Motion25", "Roll1Motion26", "Roll1Motion27", "Roll1Motion28", "Roll1Motion29"]
let roll1Animation = SKAction.animate(with: Array(roll1Textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
But I get an error message: Cannot use instance member 'roll1Textures' within property initialiser; property initialisers run before 'self' is available.
How did I implement your code in a wrong way?
Upvotes: 1
Views: 573
Reputation: 35392
I don't know if it could be useful to your game but now in Swift you can also do in one line:
let animation = SKAction.animate(with: Array(textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
In other words you can create an array slice composed by your specific range then re-create the array of textures with Array
Upvotes: 1