Game Development Reference
In-Depth Information
Time for action - updating scores
Time to type the last method in the game.
1. We start by playing a nice effect for a goal and stopping our ball:
void GameLayer::playerScore (int player) {
SimpleAudioEngine::getInstance()->playEffect("score.wav");
_ball->setVector(Vec2(0,0));
2. Then we update the score for the scoring player, updating the score label in the
process. And the ball moves to the court of the player against whom a point was
just scored:
char score_buffer[10];
if (player == 1) {
_player1Score++;
_player1ScoreLabel->setString(std::to_string(_player1Score));
_ball->setNextPosition(Vec2(_screenSize.width *
0.5, _screenSize.height * 0.5 + 2 * _ball->radius()));
} else {
_player2Score++;
_player2ScoreLabel->setString(std::to_string(_player2Score));
_ball->setNextPosition(Vec2(_screenSize.width *
0.5, _screenSize.height * 0.5 - 2 * _ball->radius()));
}
The players are moved to their original position and their _touch properties are
cleared:
_player1->setPosition(Vec2(_screenSize.width *
0.5, _player1->radius() * 2));
_player2->setPosition(Vec2(_screenSize.width *
Search WWH ::




Custom Search