我有一个正在开发的演示游戏,并在场景中实现了暂停播放功能。这两个功能都可以正常工作。暂停将停止所有动作,播放将恢复除一个动作之外的动作。以下是我在触摸开始时实现暂停/播放功能的方式:
- (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];
不起作用,所以在按下播放按钮后我不能再移动角色了。其他动画效果很好,但角色不再通过触摸移动。有人可以帮助了解我做错了什么吗?