Node class
Any object that is to be displayed on the screen is a node class, the most common is the scene (scene), layer (layers), Menu Wizard (Sprite), the menu class is a subclass of the layer, the initialization default anchor point of the layer is (0,0), the default anchor point of the wizard is (0.5,0.5). The position of a layer or an elf on its parent class setposition () function, in fact, refers to the location of the anchor point. And node's rotation, scaling, and panning are all based on the anchor point.
Second, director of the class (director)
The Director class is the core of the Cocos2d-x game engine, which is used to create and control the display of the home screen, the start of the game, the end, the pause, the method of calling the Director class, and the standard way to invoke the method in director Director::getinstance () The name of the function
Director class These methods are more important
scene* getrunningscene (); Gets the scene that is now running, note: The director can only move a scene void Runwithscene (Scene *scene) at a similar time; Enter the director loop by passing in the parameter scene
void Replacescene (Scene *scene); Toggle Scene
void end (); End game
Third, the scene class
Scene has a subclass cctransitionscene switch Scene class, this class has many subclasses, that is, a lot of ways to switch scenes, all of which have these classes because when running Director::getinstance ()->repleacescene () method (when switching scenes), there will be a memory "peak", the new scene has not been released, the old scene has come in, the sum of the memory of the two is the "peak", this time the phone will appear the card situation, and switching the scene class is equivalent to a reading progress bar role.
There are many classes of switching scenes, which is a lot of forms, here is an example of page flipping
Scene *sce = Hellowscene::create ();
Director::getinstance ()->replacescene (Cctransitionpageturn::create (1.0,sce,false));
Four, the scene layer Class (layer)
The scene layer class not only inherits the node class, but also inherits Cctouchdelegate,ccacceleromelerdelegate, and Cckeypaddelegate, so it is also responsible for input, touch, and acceleration sensor functions.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/36/wKioL1UCfiXSz_3iAAC3-bTdE7U253.jpg "title=" ' W ( N9cen}alww9p50xays%w.png "alt=" Wkiol1ucfixsz_3iaac3-btde7u253.jpg "/>
Cclayercolor class, generally can be used as a Montpallay, it can set the RBGA value, 3.0 version can be set its touch level than the button touch level high to prevent mask penetration problem, but after 3.0 is not. We can
Layer *cover = layercolor::create (CCC4 (130,130,130,200));
cover->setcontentsize (Ccsizemake (800,480));
cover->setposition (0,0);
This->addchild (cover);
Auto callback = [] (Touch *, Event *)
{
return true;
};
Auto Listener = eventlistenertouchonebyone::create ();
Listener->ontouchbegan = callback;
listener->setswallowtouches (TRUE);//Don't spread down
_eventdispatcher->addeventlistenerwithscenegraphpriority (listener,cover);
This prevents penetration problems from occurring. Just add an event listener, set this event listener to devour the touch, and return true in the Ontouchbegan callback function.
Ccscrollview in the Cocos Extension library, use to refer to the # include "cocos-ext.h" header file, as well as join the Using_ns_cc_ext; If you want to use Cocosstudio exported JSON, or use extension to extend the library, Libcocosstudio,libextension,libgui is added manually. The following is an example of adding a libextension library
In the first step, right-click in the solution, select Add, existing project, and in the dialog box that pops up, find "Cocos2d\extensions\proj.win32\libextensions.vcxproj" and click OK.
In the second step, right-click on your project and select "Reference ...". Click "Add New Reference" below in the pop-up property page. In the pop-up child dialog box, tick the three LIB projects we just joined, click OK, and then complete the reference to the library by adding the project.
The third step is to add the Include directory to your project.
$ (engineroot)
$ (engineroot) Cocos
$ (engineroot) Cocos\editor-support
Item, Properties->c/c++ general, additional include path
A small example of a simple scroll class invocation:
BOOL Helloworld::init ()
{
if (! Layer::init ())
{
return false;
}
Ccnode *pcontainer = Layer::create ();
Pcontainer->setcontentsize (Ccsizemake (800,960));
Ccsprite *SPR = sprite::create ("Guide_light_new.png");
Spr->setposition (400,240);
Pcontainer->addchild (SPR);
ScrollView *scrolls = scrollview::create (Ccsizemake (800,240), pcontainer);
Scrolls->setdirection (ScrollView::D irection::vertical);
This->addchild (scrolls);
return true;
}
Ccmenu class, as long as the attention of this class is a layer of a sub-class, is a level, it can put a lot of MenuItem.
Control class and its subclasses, Controlslider class write an example here
BOOL Helloworld::init ()
{
if (! Layer::init ())
{
return false;
}
Controlslider *slider = controlslider::create ("Bossxt.png", "Bossxt1.png", "vip1.png");
Slider->setminimumvalue (0.0f);
Slider->setmaximumvalue (100.0f);
Slider->setposition (400,240);
Slider->addtargetwithactionforcontrolevents (This,cccontrol_selector (Helloworld::slidercallback), Control:: eventtype::value_changed);
This->addchild (slider);
Label = Label::create ("0", "", 25);
Label->setposition (400,120);
This->addchild (label);
return true;
}
void Helloworld::slidercallback (Cocos2d::object *sender, Control::eventtype controlevent)
{
Auto Slide_control = (controlslider*) sender;//through the callback parameter sender gets Controlslider
int current_value = Slide_control->getvalue ();//Get Slide Current value
Char str[32];
sprintf (str, "%d", current_value);
label->setstring (str);
}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/44/wKiom1UDoYaRAqgJAABAeUarVSE402.jpg "title=" b$o{_ wkzr1tqxlhqqox7% (u.png "alt=" Wkiom1udoyaraqgjaabaeuarvse402.jpg "/>
Five, Sprite class sprite
The Sprite class can be defined as a picture, or as part of a picture, and the sprite and its subclasses can be children of the sprite batch class.
Sprite class
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/3E/wKioL1UDqaPwfWNOAAC-GlAVvng981.jpg "title=" Qq20150314111925.png "alt=" Wkiol1udqapwfwnoaac-glavvng981.jpg "/>
Texture Atlas is to save memory by placing some of the images we need in a fixed-size image. Because the OpenGL mechanism will process the single graph into the corresponding size of the length and width are 2 of the n-th side of the picture, so put the picture together can save space.
Mapping class Cctexture2d, Map class cctexture2d is the concept of OpenGL, in OpenGL, called the picture as a map, in COCOS2DX is the meaning of the picture object, you can create a Sprite object. It inherits directly from the ref class
Sprite Batch Class Ccspritebatchnode, when you want to process two or more of the same sprite, if you render each time, each render will call the OpenGL function, because when the system renders a map on the screen, the graphics processing hardware must first prepare the shading
And then render the graphics, and finally finish the cleanup after rendering. The above is a fixed overhead per render, so the frame rate will drop by about 15% or more. If you make only one preparation for all the same poster that needs to be rendered, one render, one cleanup will solve the problem. You can then use the Ccspritebatchnode class to batch these sprites, such as * * * in the game screen, and so on. Use it as a parent to create a sprite, and it is used to manage the Sprite class, which can improve the efficiency of the program. (* * * and so on a lot of the same wizard), here it is necessary to note that the more the Ccspritebatchnode class of Elves, the more efficient the effect is more obvious. The Ccspritebatchnode class can be understood as the Cclayer class, except that the Ccspritebatchnode class accepts only the Ccsprite class and its subclasses.
BOOL Helloworld::init ()
{
if (! Layer::init ())
{
return false;
}
Ccspritebatchnode *batch = ccspritebatchnode::create ("Info_prop9.png");
batch->setposition (0,0);
this->addchild (batch);//The default anchor point is 0,0,ccspritebatchnode equivalent to a layer, but it only has the wizard
for (int i = 0; i <; i++)
{
int x = Ccrandom_0_1 () *700+50;
int y = ccrandom_0_1 () *380+50;//ccrandom_0_1 () This macro is a random number that produces 0-1
Ccsprite *SPR = Sprite::createwithtexture (Batch->gettexture ());
spr->setposition (x, y);
Batch->addchild (SPR);
}
return true;
}
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/40/wKioL1UD1o-Bjah4AASdi-J0930211.jpg "title=" Qq20150314142940.png "alt=" Wkiol1ud1o-bjah4aasdi-j0930211.jpg "/>
The Sprite frame class Ccspriteframe, the concept of the sprite frame is generated relative to the animation. An elf is a fixed node that can have many Sprite frames (ccspriteframe), which are animated by switching between them.
The sprite Frame cache class Ccspriteframecache is used to store sprite frames, which are cached in advance to help improve the efficiency of the program Ccspriteframecache is a singleton pattern that is not part of a sprite and is shared by all sprites.
VI, Camera class Cccamera
All nodes have a camera class Cccamera. Only through the camera class will the nodes be rendered. When a node has a scaling rotation and a change in position, it needs to overwrite the Cccamera class so that the node can be re-rendered through the Cccamera class.
Can be through Sprite *SPR = sprite::create ("Aaa.png");
Cccamera *came = Spr->getcamera (), to obtain the camera belonging to the node, and then do some operations, such as CAME->SETEYEXYZ (0, 0, m_z); by setting the m_ The value of z can make an elf look farther away from himself.
Seven, Container class Ccarray
Ccarray *array = Ccarray::create (); It does not determine the type of storage pair, each of which can be different.
Eight, draw the graphic class
You can override draw (Renderer *renderer, const kmmat4& transform, BOOL transformupdated) functions in the node class Ccnode and draw graphics in them
void HelloWorld::d Raw (Renderer *renderer, const kmmat4& transform, BOOL transformupdated)
{
Node::d Raw (renderer,transform,transformupdated);
Draw Line
Gllinewidth (3.0f);
CCDRAWCOLOR4B (255,0,255,255);
Ccdrawline (Point (0,0), point (800,480));
Draw a Circle
Gllinewidth (2);
CCDRAWCOLOR4B (0, 255, 255, 255);
Ccdrawcircle (Point (400,240), Cc_degrees_to_radians, (+), and true);
Draw polygons
Ccpoint vertices2[] = {CCP (30,130), CCP (30,230), CCP (50,200)};
Ccdrawpoly (Vertices2, 3, true);
Draw Bezier Curves
Ccdrawquadbezier (CCP (0,480), CCP (400,240), CCP (800,480), 50);
}
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> IX, Timers
In the game, it is often necessary to update some data or the character position at intervals, and the functions of these time schedules are provided in Cocos2d-x, and all subclasses of the Ccnode class have such functions.
1. Update Timer
Turn on timer
This->scheduleupdate ();
overriding virtual function update
void Helloworld::update (float dt)
{
}
2. Custom Timer
Turn on timer, delay 2s execution, execute 3+1 times, execute interval 1s
This->schedule (Schedule_selector (Helloworld::log), 1,3,2);
callback function
void Helloworld::log (float dt)
{
}
This article is from the "Cocos2dx Learning Journey" blog, please be sure to keep this source http://zhuoshenger.blog.51cto.com/9697184/1620351
Core classes in the COCOS2DX