Question

i have a demo game I'm working on and have implemented the pause play function in the scene. both functions work fine. pausing will stop all actions and play will resume the actions except one action. here is how i implemented the pause/play function in touches began:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:_bgLayer];
    [self moveZombieToward:touchLocation];

    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if (node == _pauseButton) {
        [_pauseButton setTexture:[SKTexture textureWithImageNamed:@"pausebutton"]];
        _pauseButtonPressed = YES;
        self.scene.view.paused = YES;
    }

    if (node == _playButton) {
        [_playButton setTexture:[SKTexture textureWithImageNamed:@"playButton"]];
        _playButtonPressed = YES;
        self.scene.view.paused = NO;
    }
}

i also update the time in update method so the current time is accounted for and pause action work properly.

- (void)update:(NSTimeInterval)currentTime
{
    if (_lastUpdateTime) {
        _dt = currentTime - _lastUpdateTime;
    } else {
        _dt = 0;
    }
    _lastUpdateTime = currentTime;

    if (_pauseButtonPressed) {
        _lastUpdateTime = 0;
        return;
    }
}

and here is zombie action method:

- (void)moveZombieToward:(CGPoint)location
{
    [self startZombieAnimation];
    _lastTouchLocation = location;
    CGPoint offset = CGPointSubtract(location, _zombie.position);

    CGPoint direction = CGPointNormalize(offset);
    _velocity = CGPointMultiplyScalar(direction, ZOMBIE_MOVE_POINTS_PER_SEC);


}

the only issue i have with this methods is (as i mentioned earlier) the call to move the zombie [self moveZombieToward:touchLocation]; does not work after pressing the play button, so after pressing the play button i can't move the character any more. other animations work just fine but the character won't move by touch any more. can some one help out to understand what I'm doing wrong?

Was it helpful?

Solution

here is the answer i figured out. i mention it here so others can benefit from it. what i am doing in the above code is firing the pause button and also firing the play button but i never set the pause and play button to no when the other one is active. the code should be as follows:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:_bgLayer];
    [self moveZombieToward:touchLocation];

    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if (node == _pauseButton) {
        [_pauseButton setTexture:[SKTexture textureWithImageNamed:@"pausebutton"]];
        _pauseButtonPressed = YES;
        _playButtonPressed = NO;
        self.scene.paused = YES;
    }

    if (node == _playButton) {
        [_playButton setTexture:[SKTexture textureWithImageNamed:@"playButton"]];
        _playButtonPressed = YES;
        _pauseButtonPressed = NO;
        self.scene.paused = NO;
    }
}

also an additional tip, i had to change the self.scene.view.paused = YES; to self.scene.paused = YES; since i only needed to pause the scene so when i set the _lastUpdateTime = 0; it would stop the time and the player won't move while the game is paused. yay, one more step learnt in sprite kit.:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top