Cocos2d-x v3.2 FlappyBird Class Object code analysis (6)

Source: Internet
Author: User

Cocos2d-x v3.2 FlappyBird Class Object code analysis (6)

Today we will talk about three classes. These three classes should be relatively simple.

HelpLayer class

NumberLayer class

GetLocalScore class

The HelpLayer class is mainly composed of two graphics genie. One is the name of the game, and the other is a prompt on how to play the game:


The NumberLayer class involves the creation of custom fonts. There are many digital images in the extracted resources:


Now we want to make them like this: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140919/20140919083815135.png" alt = "\">

This is similar to the Game image resources. It can be used directly in this way, saving a lot of things. How can we do it? Here we need to use a software, specific usage method and process, I will not introduce it here.

Http://blog.csdn.net/swejackies/article/details/8967903

I am very grateful for the detailed introduction. Another problem in the NumberLayer class is single-instance implementation. single-instance class: as the name suggests, it only has one instance. Sometimes, a function class has many instances, which makes the program very complex, the Singleton class solves this problem.

GetLocalScore class, local data access, this class is also very simple, are called, some interfaces of the cocos2d-x, so no difficulty.

The Code Analysis for these three classes is as follows:


// HelpLayer. h # pragma once # include "cocos2d. h" class HelpLayer: public cocos2d: Layer {public: HelpLayer ();~ HelpLayer (); bool init (); // callBack function void callBack (); CREATE_FUNC (HelpLayer); private: // single-touch listening cocos2d: EventListenerTouchOneByOne * listener; cocos2d :: sprite * gameName; cocos2d: Sprite * tutorial ;};

// HelpLayer. cpp # include "HelpLayer. h" USING_NS_CC; HelpLayer: HelpLayer () {} HelpLayer ::~ HelpLayer () {} bool HelpLayer: init () {if (! Layer: init () {return false;} auto origin = Director: getInstance ()-> getVisibleOrigin (); auto visibleSize = Director: getInstance () -> getVisibleSize (); // game name gameName = Sprite: createWithSpriteFrameName ("text_ready.png"); gameName-> setPosition (Point (origin. x + visibleSize. width * 0.5, origin. y + visibleSize. height * 0.8); this-> addChild (gameName); // gameplay prompt tutorial = Sprite: createWithSpriteFrameName ("tutorial.png"); tutorial-> setPosition (Point (origin. x + visibleSize. width * 0.5, origin. y + visibleSize. height * 0.4); this-> addChild (tutorial); // single-touch listening. As mentioned in the previous chapters, listener = EventListenerTouchOneByOne: create (); listener-> setSwallowTouches (false); listener-> onTouchBegan = [] (Touch * t, Event * e) {log ("touch began"); return true ;}; listener-> onTouchEnded = [=] (Touch * t, Event * e) {// The layer disappears after a click, so when the click is detected, // The listener _ eventDispatcher-> removeEventListener (listener) will be removed from the event distributor; // This gradient animation, because there are two genie executing the animation, and two such animations are required // Therefore, fade_2 cloned a fadeauto fade = FadeOut: create (0.5f ); // clone auto fade_2 = fade-> clone (); // animation execution + callback function auto callBack = CallFuncN: create (CC_CALLBACK_0 (HelpLayer: callback, this )); auto sequence = Sequence: create (fade, callback, NULL); tutorial-> runAction (sequence); gameName-> runAction (fade_2 );}; // The Last listener may not be mentioned. Every listener must be added to the event distributor, which is the following code. // The first parameter is our listener, the second parameter is to add this listener object // here our click object is the entire layer, so it is this_eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this); return true ;} // remove the void HelpLayer: callBack () {this-> removeChild (tutorial); this-> removeChild (gameName );}

// NumberLayer. h # pragma once # include "cocos2d. h" class NumberLayer: public cocos2d: Layer {public: NumberLayer ();~ NumberLayer (); bool init (); // void addScore (); // initialize the score void initScore (); // obtain the score int getScore (); // obtain the single-instance object static NumberLayer * getInstance (); private: cocos2d: Label * numberLabel; int score ;};

// NumberLayer. cpp # include "NumberLayer. h "USING_NS_CC; // unique object (the initial value must be null). Note that static NumberLayer * instance = NULL; NumberLayer: NumberLayer () {} NumberLayer ::~ NumberLayer () {}// NumberLayer * NumberLayer: getInstance () {// determines whether the object exists if (instance = NULL) {// if not, create NumberLayer * temp = new NumberLayer (); // then initialize temp-> init (); instance = temp ;} // if it already exists, return the return instance;} bool NumberLayer: init () {if (! Layer: init () {return false;} auto origin = Director: getInstance ()-> getVisibleOrigin (); auto visibleSize = Director: getInstance () -> getVisibleSize (); // initialization score = 0; // This is the Custom font that uses auto str =__ String: createWithFormat ("% d", score ); numberLabel = Label: createWithBMFont ("font1.fnt", str-> getCString (); numberLabel-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height * 0.85); this-> addChild (numberLabel); return true;} void NumberLayer: addScore () {score ++; auto str =__ String :: createWithFormat ("% d", score); numberLabel-> setString (str-> getCString ();} void NumberLayer: initScore () {score = 0; auto str =__ String: createWithFormat ("% d", score); numberLabel-> setString (str-> getCString ();} int NumberLayer: getScore () {return score ;}

// GetLocalScore. h # pragma once # include "cocos2d. h" class GetLocalScore {public: GetLocalScore ();~ GetLocalScore (); static GetLocalScore * getInstance (); // determine whether the local file bool isHaveLocalFile () exists; // obtain the score int getHighScore () in the local file (); // write the local file score void setHighScore (int); private :};


// GetLocalScore. cpp # include "GetLocalScore. h" USING_NS_CC; static GetLocalScore * instance = NULL; GetLocalScore: GetLocalScore () {} GetLocalScore ::~ GetLocalScore () {}// GetLocalScore * GetLocalScore: getInstance () {if (instance = NULL) {GetLocalScore * temp = new GetLocalScore (); instance = temp;} return instance;} bool GetLocalScore: isHaveLocalFile () {// get this value directly. If yes, return trueif (UserDefault: getInstance () -> getBoolForKey ("isHaveLocalFile") {return true;} else {// if it does not exist, create it and write the score. // if the written data is of the bool type, use setBoolForKey // if the data to be written is of the int type, use setIntegerForKey // and another data type. The method is similar. Here we will not introduce UserDefault: getInstance () -> setBoolForKey ("isHaveLocalFile", true); UserDefault: getInstance ()-> setIntegerForKey ("highScore", 0); UserDefault: getInstance ()-> flush (); return false ;}} // get the score int GetLocalScore: getHighScore () {if (isHaveLocalFile () {return UserDefault: getInstance () -> getIntegerForKey ("highScore") ;}else {return 0 ;}// write score void GetLocalScore: setHighScore (int n) {if (isHaveLocalFile () {UserDefault:: getInstance ()-> setIntegerForKey ("highScore", n); UserDefault: getInstance ()-> flush ();} else {UserDefault: getInstance () -> setBoolForKey ("isHaveLocalFile", true); UserDefault: getInstance ()-> setIntegerForKey ("highScore", n); UserDefault: getInstance () -> flush ();}}

This is today.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.