I'm building a game using iOS SpriteKit. I have a regular UIViewController
"Chooser" that segues into a second VC that loads a SKScene. The SKScene
loads SKSprites
and moves them via SKAction
across the screen. When I run the first round it works great.
After completing a game we return to the Chooser
to setup the next round. I reload the same VC with same SKScene
. This time the Sprites are created but the SKAction
does not run. I added a bunch of logs statements to track it down.
Here's the code with the SKAction
:
NSInteger startZone = [TPMathUtilities randomOffscreenStartZone];
CGPoint startPointONE = [TPMathUtilities OFFScreenPointFinder:startZone];
NSInteger finishZone = [TPMathUtilities finishZoneFinder:startZone];
CGPoint finishPointONE = [TPMathUtilities OFFScreenPointFinder:finishZone];
NSLog(@"startPointONE = %f %f finishPointONE = %f %f", startPointONE.x,startPointONE.y,finishPointONE.x,finishPointONE.y);
newTap.position = startPointONE;
CGFloat distanceToOffScreenPt = [TPMathUtilities distanceBetweenTwoPoints:startPointONE toPoint:finishPointONE];
CGFloat movementDuration = distanceToOffScreenPt/[[TPGameData data] tapBaseGeneratorVelocity];
NSLog(@"movementDuration = %f", movementDuration);
newTap runAction:[SKAction sequence:@[[SKAction runBlock:^{NSLog(@"starting TAP ACTIONS ");}],
[SKAction moveTo:finishPointONE duration:movementDuration],
[SKAction removeFromParent]]]];
Here's the log for the first round, you can see the "starting TAP ACTIONS"
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.511158
startPointONE = -50.000000 740.000000 finishPointONE = 913.000000 -50.000000
movementDuration = 4.529381
UPDATE after Sprite Added
starting TAP ACTIONS
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.512250
startPointONE = 1064.000000 158.000000 finishPointONE = 521.000000 808.000000
movementDuration = 3.079871
UPDATE after Sprite Added
starting TAP ACTIONS
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.561383
startPointONE = 1064.000000 451.000000 finishPointONE = -50.000000 755.000000
movementDuration = 4.199035
UPDATE after Sprite Added
starting TAP ACTIONS
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.507709
startPointONE = 921.000000 808.000000 finishPointONE = -50.000000 110.000000
movementDuration = 4.348527
UPDATE after Sprite Added
starting TAP ACTIONS
Here's the second round with out the SKAction:
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.813655
startPointONE = 1064.000000 440.000000 finishPointONE = -50.000000 7.000000
movementDuration = 4.346155
UPDATE after Sprite Added
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.813989
startPointONE = 437.000000 -50.000000 finishPointONE = 652.000000 808.000000
movementDuration = 3.216464
UPDATE after Sprite Added
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.806389
startPointONE = 992.000000 -50.000000 finishPointONE = 856.000000 808.000000
movementDuration = 3.158952
UPDATE after Sprite Added
UPDATE variables: localGenMult= 0.800000 tapBaseGen= 0.800000 gameInterval= 0.811594
startPointONE = -50.000000 146.000000 finishPointONE = 1064.000000 746.000000
movementDuration = 4.601108
UPDATE after Sprite Added
My code for creating the SKScene
remains the same, and the code above does not change. Any guess why the action is not activating on the Sprite the second round?
@DFrog headed me in the right direction. The problem was in the unwind segue. My app has an initial VC where players choose a game 'level.' It segues to a second VC containing the SKScene. It then 'unwinds' back to the chooserVC at the end of the game.
I deleted all segues/connections between the VC's and made a new unwind segue following the advice here: Unwind segue in a nutshell
It now works great. Thanks all.