我不确定我的问题措辞是否正确,但基本上我正在开发一款有开始按钮的游戏,当用户点击它时会触发 10 秒计时器,然后用户有 10 秒时间收集一群猫。一切正常,我的问题是代码在 update
: 方法中是这样设置的:
//reset counter if starting:
if (startGamePlay) {
startTime = currentTime;
startGamePlay = NO;
}
countDownInt = 10.0 - (int)(currentTime - startTime);
if (countDownInt>0) {
countDown.text = [NSString stringWithFormat:@"TIME REMAINING: %i", countDownInt];
}else if (countDownInt <= 0) {
[self runAction:[SKAction sequence:@[
[SKAction runBlock:^{
countDown.text = @"";
}],
[SKAction waitForDuration:0.5],
[SKAction runBlock:^{
//transition
}],
]]];
}
在我想插入到游戏结束场景的过渡的地方,上面写着//transition。问题是这与游戏开始时的代码相同,因为时间计时器仅在用户单击按钮时初始化,此时 startGamePlay BOOL 设置为 YES,并且计时器被激活。
如何插入:
gameOverScene *gameOver = [[gameOverScene alloc] initWithSize:self.size];
[self.view presentScene:gameOver transition:[SKTransition moveInWithDirection:SKTransitionDirectionUp duration:0.5]];
我必须做出哪些改变才能使其发挥作用?另一个 bool 可能?我应该把 bool 放在哪里?
如果需要,这是 tap 方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"tapCircle"]) {
score = score + 1;
}
if ([node.name isEqualToString:@"startButton"] && _startButton.zPosition == 10.0) {
startGamePlay = YES;
_startButton.zPosition = -2.0;
}
}
}
最佳答案
你的这个问题很难理解,所以我不确定我是否答对了...
您发布的第一个片段似乎是由某个计时器调用的某种方法,并且无论游戏状态如何都会不断调用?如果是这样,这不是最好的设计,但仍然是:您不需要另一个 bool 值,您应该使用整数来表示游戏状态,或者创建一个枚举:
typedef enum:NSInteger {
gameStateBegin,
gameStatePlaying,
gameStateOver,
gameStateWiting
}gameState;
- (void)onTimer {
switch (self.state) {
case gameStateBegin: {
startTime = currentTime;
self.state = gameStatePlaying;
break;
}
case gameStatePlaying: {
countDownInt = 10.0 - (int)(currentTime - startTime);
if (countDownInt>0) {
countDown.text = [NSString stringWithFormat:@"TIME REMAINING: %i", countDownInt];
}
else if (countDownInt <= 0) {
self.state = gameStateOver;
}
break;
}
case gameStateOver: {
self.state = gameStateWiting;
[self runAction:[SKAction sequence:@[
[SKAction runBlock:^{
countDown.text = @"";
}],
[SKAction waitForDuration:0.5],
[SKAction runBlock:^{
//transition
}],
]]];
break;
}
default:
break;
}
}
关于ios - 怎么设置game over在游戏结束的时候和开始的条件一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751863/