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

我有两个整数:

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

2)一种用于评分功能。

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

问题在于,当这两种情况同时存在时,游戏就会被窃听,并且计时器开始变得疯狂。如果删除计分int / timer,则对象将完美移动。反之亦然。

我似乎找不到该问题,因为它应该可以工作。

有任何想法吗?

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.
}

最佳答案

我认为更干净的方法是移至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 */
  }
}

关于ios - 多个整数-它们不能同时工作(OBJ-C),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39623787/

10-13 09:21