I am making a board game style game. There is two players, player1 and player 2. Player 1 starts by pressing the player 1 play button. Their sprite moves forward a random number of tiles. Then player 2 starts their turn by pressing the player 1 play button. At the end of each turn I logged whose turn it was when I pressed the player 2 play button after pressing the player 1 play button. It logs saying it is still player 1's turn. Why is this? This is my code:
- (void)player1play {
isPlayer1turn = YES;
CCLOG(@"Beggining of player 1's turn");
NSLog(@"%hhd", isPlayer1turn);
if (_randNumLabel == nil)
{
CCLOG(@"Nil");
}
if (_randNumLabel !=nil)
{
CCLOG(@"play button pressed!");
int max = 6;
int randNumber = (arc4random() % max) + 1; // Generates a number between 1-6.
CCLOG(@"Random Number %d", randNumber);
_randNumLabel.string = [NSString stringWithFormat:@"Number: %d", randNumber];
}
//Movement code
isPlayer1turn = NO;
CCLOG(@"End of player 1's turn");
NSLog(@"%hhd", isPlayer1turn);
}
- (void)player2play {
if (isPlayer1turn == YES)
{
isPlayer1turn = NO;
CCLOG(@"Beggining of player 2's turn");
NSLog(@"%hhd", isPlayer1turn);
if (_randNumLabel == nil)
{
CCLOG(@"Nil");
}
if (_randNumLabel !=nil)
{
CCLOG(@"play button pressed!");
int max = 6;
int randNumber = (arc4random() % max) + 1; // Generates a number between 1-6.
CCLOG(@"Random Number %d", randNumber);
_randNumLabel.string = [NSString stringWithFormat:@"Number: %d", randNumber];
}
isPlayer1turn = TRUE;
CCLOG(@"End of player 2's turn");
NSLog(@"%hhd", isPlayer1turn);
}
}
How can I fix this? Or would it be better to have just one play button?
After pressing the player 1 play button:
014-06-18 20:50:33.450 Sunk[8091:60b] Beggining of player 1's turn
2014-06-18 20:50:33.451 Sunk[8091:60b] 1
2014-06-18 20:50:33.451 Sunk[8091:60b] play button pressed!
2014-06-18 20:50:33.452 Sunk[8091:60b] Random Number 1
2014-06-18 20:50:33.453 Sunk[8091:60b] End of player 1's turn
2014-06-18 20:50:33.454 Sunk[8091:60b] 0
After pressing the player 2 play button:
2014-06-18 20:52:26.666 Sunk[8091:60b] 1
2014-06-18 20:52:26.667 Sunk[8091:60b] play button pressed!
2014-06-18 20:52:26.668 Sunk[8091:60b] Random Number 5
2014-06-18 20:52:26.668 Sunk[8091:60b] End of player 1's turn
2014-06-18 20:52:26.669 Sunk[8091:60b] 0