多个整数 - 它们不能同时工作(Obj-C)

时间:2016-09-21 18:16:30

标签: ios objective-c

我正在制作一个简单的小游戏。

我有两个整数:

1)使对象的位置在Y轴和X轴上随机生成。

2)一个用于评分功能。

我使用NSTimers使对象移动,并使用得分每秒加1。

问题在于,当两个整数存在时,游戏都会被窃听,并且计时器开始变得疯狂。如果我删除得分int /计时器,对象移动完美。反过来说。

我似乎无法找到问题,因为它应该有效。

有什么想法吗?

ViewController.h:

int scoreNumber;
int randomPosition;

ViewController.m:

#define IsIphone4 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON )

#define IsIphone5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

#define IsIphone6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON )

#define IsIphone6Plus ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )736 ) < DBL_EPSILON )

- (void)obstaclesMoving {

obstacle.center = CGPointMake(obstacle.center.x - 1, obstacle.center.y);
obstacle2.center = CGPointMake(obstacle2.center.x - 1, obstacle2.center.y);
obstacle3.center = CGPointMake(obstacle3.center.x - 1, obstacle3.center.y);

if (IsIphone4) {

    if (obstacle.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 70;
        obstacle.center = CGPointMake(320, randomPosition);
    }

    if (obstacle2.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 360;
        obstacle2.center = CGPointMake(320, randomPosition);

    }

    if (obstacle3.center.x < 0) {
        randomPosition = arc4random_uniform(100);
        randomPosition = randomPosition + 215;
        obstacle3.center = CGPointMake(320, randomPosition);

    }

}

if (IsIphone5) {

    if (obstacle.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 70;
        obstacle.center = CGPointMake(320, randomPosition);
    }

    if (obstacle2.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 448;
        obstacle2.center = CGPointMake(320, randomPosition);

    }


    if (obstacle3.center.x < 0) {
        randomPosition = arc4random_uniform(100);
        randomPosition = randomPosition + 259;
        obstacle3.center = CGPointMake(320, randomPosition);

    }

}

if (IsIphone6) {

    if (obstacle.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 70;
        obstacle.center = CGPointMake(375, randomPosition);
    }

    if (obstacle2.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 547;
        obstacle2.center = CGPointMake(375, randomPosition);

    }


    if (obstacle3.center.x < 0) {
        randomPosition = arc4random_uniform(100);
        randomPosition = randomPosition + 309;
        obstacle3.center = CGPointMake(375, randomPosition);

    }

}

if (IsIphone6Plus) {

    if (obstacle.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 70;
        obstacle.center = CGPointMake(414, randomPosition);
    }

    if (obstacle2.center.x < 0) {
        randomPosition = arc4random_uniform(60);
        randomPosition = randomPosition + 616;
        obstacle2.center = CGPointMake(414, randomPosition);

    }


    if (obstacle3.center.x < 0) {
        randomPosition = arc4random_uniform(100);
        randomPosition = randomPosition + 343;
        obstacle3.center = CGPointMake(414, randomPosition);

    }

}

}

- (IBAction)characterUp:(id)sender {

characterDrop = 5;

}

- (IBAction)characterDown:(id)sender {

characterDrop = -5;

}

- (void)characterMoving {

character.center = CGPointMake(character.center.x, character.center.y - characterDrop);

characterDrop = characterDrop - 0.1;

if ((CGRectIntersectsRect(character.frame, ground.frame)) && (characterDrop < -1)) {

    [self gameOver];

    //Sound for G.O.
}

if ((CGRectIntersectsRect(character.frame, roof.frame)) && (characterDrop < -1)) {

    [self gameOver];

    //Sound for G.O.
}

}

- (IBAction)startGame:(id)sender {

startGameButton.hidden = YES;

characterMovement = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(characterMoving) userInfo:nil repeats:YES];

characterDrop = -5;

obstacleTimer = [NSTimer scheduledTimerWithTimeInterval:0.0055 target:self selector:@selector(obstaclesMoving) userInfo:nil repeats:YES];

scorer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scoring) userInfo:nil repeats:YES];

}

- (void)scoring {

scoreNumber = scoreNumber + 1;
scoreLabelInGame.text = [NSString stringWithFormat:@"%i", scoreNumber];
scoreLabelGameOver.text = [NSString stringWithFormat:@"%i", scoreNumber];

}

- (void)gameOver {

if (scoreNumber > bestScoreNumber) {

    [[NSUserDefaults standardUserDefaults] setInteger:scoreNumber forKey:@"bestScoreSaved"];
    bestScoreLabel.text = [NSString stringWithFormat:@"New best: %i", scoreNumber];

}

character.hidden = YES;
obstacle.hidden = YES;
obstacle2.hidden = YES;
obstacle3.hidden = YES;
startGameButton.hidden = YES;
scoreLabelInGame.hidden = YES;
characterUpButton.hidden = YES;
characterDownButton.hidden = YES;
scoreLabelGameOver.hidden = NO;
bestScoreLabel.hidden = NO;
gameOverLabel.hidden = NO;
restartGameButton.hidden = NO;
backButton.hidden = NO;

[characterMovement invalidate];
[obstacleTimer invalidate];
[obstacleTimer2 invalidate];
[obstacleTimer3 invalidate];
[obstacleTimer4 invalidate];
[scorer invalidate];

}

- (void)viewDidLoad {

bestScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"bestScoreSaved"];
bestScoreLabel.text = [NSString stringWithFormat:@"Best: %li", (long)bestScoreNumber];

scoreNumber = 0;

scoreLabelGameOver.hidden = YES;
bestScoreLabel.hidden = YES;
gameOverLabel.hidden = YES;
restartGameButton.hidden = YES;
backButton.hidden = YES;

[super viewDidLoad];
// Do any additional setup after loading the view.
}

3 个答案:

答案 0 :(得分:2)

我认为更简洁的做法是转移到CADisplayLink或单NSTimer,并将您的观点移出其调用的选择器。

/* Add _amountOfTicks as an instance Variable */
{
  int _amountOfTicks;
  int scoreNumber;
  int randomPosition;
}

/* replace timers in startGame */
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
displayLink.frameInterval = 1;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

- (void)tick {

  _amountOfTicks++;
  if (_amountOfTicks > 16){
    _amountOfTicks = 1;
  }

  if (_amountOfTicks == 2) {
     [self characterMoving] 
  }

  if (_amountOfTicks == 4) {
     [self obstaclesMoving] 
  }

  if (_amountOfTicks == 16) {
     [self scoring] 
  }

}

此外,在下面的方法中,您将center.x减小1,然后在检查宏时将其更改回正下方的屏幕宽度。

- (void)obstaclesMoving {

obstacle.center = CGPointMake(obstacle.center.x - 1, obstacle.center.y); /* reduce by 1 */
obstacle2.center = CGPointMake(obstacle2.center.x - 1, obstacle2.center.y);
obstacle3.center = CGPointMake(obstacle3.center.x - 1, obstacle3.center.y);

if (IsIphone4) {

  if (obstacle.center.x < 0) {
    randomPosition = arc4random_uniform(60);
    randomPosition = randomPosition + 70;
    obstacle.center = CGPointMake(320, randomPosition); /* changes it back to screen width which makes what you did up above useless */
  }
}

答案 1 :(得分:0)

来自docs

  

计时器的时间间隔的有效分辨率是有限的   大约50-100毫秒

我的猜测是这可能是个问题。

如果您需要每1分钟增加一次分数,为什么每隔0.0055秒调用一次该功能?

您还需要将[super viewDidLoad];移至viewDidLoad功能的顶部。

同样正如@rmaddy所说,你不应该明确检查屏幕尺寸。

答案 2 :(得分:0)

我可以告诉你,运行分辨率为1/180秒的计时器会让你遇到麻烦。由于帧最多每60秒更新一次,因此用户无法看到它。无法保证定时器实际上会经常运行(很可能他们不会赢)。

你不能使用这样的计时器。您只能使用它们触发一个事件,然后您需要确切地检查它是什么时间以及您应该做什么。

可能发生的事情是您设法打开自动布局。