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 – coding the GameLayer interface


GameLayer is the main container in our game.

  1. Follow the steps to add a new file to your Classes folder. This is a C++ file called GameLayer.

  2. Select your GameLayer.h. Just below the first define preprocessor command, add:

    #define GOAL_WIDTH 400
  3. We define the width of the goals in pixels.

  4. Next, add the declarations for our sprites and our score text labels:

    #include "cocos2d.h"
    #include "GameSprite.h"
    
    using namespace cocos2d;
    
    class GameLayer : public Layer
    {
        GameSprite* _player1;
        GameSprite* _player2;
        GameSprite* _ball;
        
        Vector<GameSprite*> _players;
        Label* _player1ScoreLabel;
        Label* _player2ScoreLabel;

    We have the GameSprite objects for two players (the weird looking things called mallets), and the ball (called a puck). We'll store the two players in a Cocos2d-x Vector. We also have two text labels to display the score for each player.

  5. Declare a variable to store the screen size. We'll use this a lot for positioning...