Today we are going to talk about three classes, these three classes should be fairly simple
Helplayer class
Numberlayer class
Getlocalscore class
Helplayer class, mainly put two graphics elves up, one is the name of the game, a hint game how to play, on a picture:
Numberlayer class, related to the production of custom fonts, we extracted resources, there are many digital pictures:
And now we're going to make them like this:
This is similar to the game picture resources, made so that you can directly take to use, save a lot of things, that how to do, here we need to use a called software, the specific use of methods and processes, I do not introduce here, we directly poke here
http://blog.csdn.net/swejackies/article/details/8967903
The introduction is very detailed, but also thanks to this author. Numberlayer class There is another problem in this class is a singleton implementation, Singleton class: As the name implies it has only one instance, sometimes a function class has a lot of instances, will make the program become very complex, the Singleton class solves the problem.
Getlocalscore class, local data access, this class is also very simple, are called, cocos2d-x some of the interface, so there is no difficulty.
Here is the code analysis for these three classes:
Helplayer.h#pragma once#include "Cocos2d.h" Class Helplayer:public Cocos2d::layer{public:helplayer (); ~HelpLayer () ; bool Init ();//callback function void CallBack (); Create_func (helplayer);p rivate://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 hint 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, I'm not going to tell you this way. Listener=eventlistenertouchonebyone::create (); Listener->setswallowtouches (false);listener-> Ontouchbegan=[] (Touch * t,event * e) {log ("touch began"); return true;}; Listener->ontouchended=[=] (Touch *t,event * e) {//This layer will disappear with a click, so when the click is detected here//The listener will be removed from the event dispatcher Eventdispatcher->removeeventlistener (listener);//This FadeAnimation, because there are two sprites performing animations, there are two such animations//So fade_2 clones 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);};/ /last time listening may not speak, each listen to add to the event dispatcher, is the following code///The first parameter is our listening, the second parameter is to add this listener object//Here we click on the object is this whole layer, so is This_ Eventdispatcher->addeventlistenerwithscenegraphpriority (listener,this); return true;} Remove both sprites 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 ();//plus void Addscore ();//Initialize fractional void Initscore ();//Get fractional int getscore ();//Get Singleton object static Numberlayer * getinstance ();p Rivate:cocos2d::label * Numberlabel;int score;};
Numberlayer.cpp#include "NumberLayer.h" using_ns_cc;//the only existence of its own object (this way must be assigned the initial value of NULL), note that it is statically static Numberlayer * instance =null; Numberlayer::numberlayer () {}numberlayer::~numberlayer () {}//The main part of the Singleton class Numberlayer * Numberlayer::getinstance () {// Determine if the object exists if (instance==null) {//If it does not exist, create Numberlayer * temp=new numberlayer ();//Then Initialize Temp->init (); instance= temp;} If it already exists, return it directly to the return instance;} BOOL Numberlayer::init () {if (! Layer::init ()) {return false;} Auto Origin=director::getinstance ()->getvisibleorigin (); auto Visiblesize=director::getinstance () Getvisiblesize ();//Initialize fraction score=0;//This is the custom font used by 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 if there is a local file bool Ishavelocalfile ();//Get a local file score int gethighscore ();// Write local file score void Sethighscore (int);p rivate:};
Getlocalscore.cpp#include "GetLocalScore.h" using_ns_cc;static getlocalscore * instance=null; Getlocalscore::getlocalscore () {}getlocalscore::~getlocalscore () {}//This side of the singleton does not introduce Getlocalscore * Getlocalscore:: GetInstance () {if (instance==null) {Getlocalscore * temp=new getlocalscore (); instance=temp;} return instance;} BOOL Getlocalscore::ishavelocalfile () {//go directly to get this value, if present, return Trueif (Userdefault::getinstance ()->getboolforkey (" Ishavelocalfile ")) {return true;} else{//If it does not exist, create and write the score//if the data being written is of type bool, use setboolforkey//if the data being written is of type int, with setintegerforkey//and other types of writing, the method is similar, Userdefault::getinstance ()->setboolforkey ("Ishavelocalfile", true) is not introduced here; 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 fractional 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 ();}}
We'll be here today.
Cocos2d-x v3.2 flappybird Specific code analysis for each class object (6)