Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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