0

我有一个正在开发的演示游戏,并在场景中实现了暂停播放功能。这两个功能都可以正常工作。暂停将停止所有动作,播放将恢复除一个动作之外的动作。以下是我在触摸开始时实现暂停/播放功能的方式:

    - (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;
    }
}

我还更新了更新方法中的时间,以便考虑当前时间并暂停操作正常工作。

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

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

这是僵尸动作方法:

- (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);


}

我对这种方法的唯一问题是(正如我之前提到的)在按下播放按钮后移动僵尸的调用[self moveZombieToward:touchLocation];不起作用,所以在按下播放按钮后我不能再移动角色了。其他动画效果很好,但角色不再通过触摸移动。有人可以帮助了解我做错了什么吗?

4

1 回答 1

1

这是我想出的答案。我在这里提到它,以便其他人可以从中受益。我在上面的代码中所做的是触发暂停按钮并触发播放按钮,但是当另一个按钮处于活动状态时,我从未将暂停和播放按钮设置为 no。代码应如下所示:

- (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;
    }
}

还有一个额外的提示,我必须将其更改为self.scene.view.paused = YES;self.scene.paused = YES;因为我只需要暂停场景,所以当我设置它时,_lastUpdateTime = 0;它会停止时间,并且玩家在游戏暂停时不会移动。是的,在精灵套件中又学到了一步。:)

于 2014-04-06T14:09:18.960 回答