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

The rocket sprite


Time to tackle the second object in the game: the rocket.

Once again, I already put in place the part of the logic that's old news to you. But please review the code already inside Rocket.cpp. We have a method to reset the rocket every time a new game starts (reset), and a method to show the selected state of the rocket (select(bool flag)) by changing its displayed texture:

if (flag) {
    this->setDisplayFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("rocket_on.png"));
} else {
    this->setDisplayFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("rocket.png"));
}

This will either show the rocket with a glow around it, or not.

And finally a method to check collision with the sides of the screen (collidedWithSides). If there is a collision, we adjust the rocket so it moves away from the screen side it collided with, and we release it from any pivot point.

What we really need to worry about here is the rocket's update method. And that's what...