Reputation: 2228
I have a GKAgent2D governing the movement of an SKNode enemy that moves towards a destination.
When I pause the SKScene that the agent is moving in, the enemy node seems to continue to move without showing such movement. All of the visible movements stop (AKA the enemy stops moving). If I wait 5 seconds then unpause the scene, it's as if the enemy never stopped moving and it pops to where it would have been had I not paused the scene, and it continues to move from that point as normal.
I looked into the sample demo game that Apple had created for users to use called DemoBots and they implement a "fix" similar to what I am using by simply leaving the update:(NSTimeInterval *)currentTime
method with a return. Here is what my code currently looks like:
- (void)update:(NSTimeInterval)currentTime {
[super update:currentTime];
if (_lastUpdateTime == 0) _lastUpdateTime = currentTime;
float delta = currentTime - _lastUpdateTime;
_lastUpdateTime = currentTime;
// Stop updating if the `worldNode` is paused.
if (worldNode.paused) { return };
for (GKComponentSystem *componentSystem in componentSystems) {
[componentSystem updateWithDeltaTime:deltaTime];
}
}
But I am unable to dive deeper and find out what else they are doing to ensure that the GKGoal
objectives that are currently in effect are stopped right when the pause happens. I even logged the agentDidUpdate method and it stops firing when I pause the scene, so I am really not sure how it continues to move.
If someone knows the answer please let me know. Thank you!
UPDATE: I even tried pausing the individual nodes that have the goals set to move:
- (void)didPauseScene {
worldNode.paused = YES;
/*
<< Animations >>
*/
for (OEnemy *enemy /*my subclass*/ in enemyArray) {
enemy.paused = YES;
}
}
But this still did not stop the GKGoals
from continuing as they would without pausing the scene..
UPDATE 2: The ONLY solution that stops the agents cold is literally removing the agent systems purpose:
if (worldNode.paused) {
self.agentSystem = nil;
return;
}
This is a very sad solution as I would hope there is a more elegant / appropriate way of stopping the goals without removing everything completely from the scene. The other issue however is even with this, resetting the goals upon unpausing creates the same issue of the jumping to the position they would be in if no pausing had occurred.
Upvotes: 1
Views: 171
Reputation: 3
For those where the GKGoal goalToReachTargetSpeed:
did not work.
Removing my MovementComponent from the entity worked for me:
let movementComponent = entity.component(ofType: MovementComponent.self)!
entity.removeComponent(ofType: MovementComponent.self)
componentSystem.removeComponent(moveComponent)
When you resume your game, make sure you create a new instance of your movement component and add it to the entity again:
let movementComponent = MovementComponent()
entity.addComponent(movementComponent)
componentSystem.addComponent(moveComponent)
When using a GKComponentSystem to update your movement component, also make sure that you remove and add the movement component accordingly.
Upvotes: 0
Reputation: 91
Agents will continue along their last known valid velocity if another goal isn't altering it. If you'd like the agents to stop you could try using GKGoal's goalToReachTargetSpeed:.
Upvotes: 1