Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

Table of Contents (19 chapters)
Learning Cocos2d-x Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the parallax scrolling layer


Next, all we have to do is include the ScrollingBgLayer class in HelloWorldScene.h.

In HelloWorldScene.h, add the ScrollingBg.h header file:

#include "ScrollingBgLayer.h"

Create a new variable of the ScrollingBgLayer type named scrollingBgLayer:

ScrollingBgLayer* scrollingBgLayer;

Next, in the HelloWorldScene.cpp file, initialize the variable in the init() function:

    scrollingBgLayer = new ScrollingBgLayer(3.0);
    this->addChild(scrollingBgLayer);

Here, we give 3.0 as the base speed. The respective speeds of the different objects will be based on this speed.

Next, call the update function of ScrollingBgLayer in the HelloWorldScenes update function:

scrollingBgLayer->update();

Now if you build and run the project, you will see the background layer scrolling from right to left giving the feeling of motion to the scene.

This is how a scrolling background, also known as parallax layer, can be added to make the scene a little more dynamic than just static...