我有一个我正在研究的演示游戏并在场景中实现了暂停播放功能。这两个功能都很好。暂停将停止所有操作,并且播放将恢复除一个操作之外的操作。这是我在触摸开始时实现暂停/播放功能的方式:
- (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];
的调用不起作用,所以在按下播放按钮后我无法做到再移动角色了。其他动画工作得很好,但角色不再通过触摸移动。有人可以帮助理解我做错了吗?
答案 0 :(得分:1)
这是我想出的答案。我在这里提到它,以便其他人可以从中受益。我在上面的代码中正在做的是触发暂停按钮并触发播放按钮,但是当另一个激活时,我从未将暂停和播放按钮设置为否。代码应如下:
- (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;
时它会停止时间和播放器在游戏暂停时不会移动。是的,在精灵工具包中学到了一步。:)