Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the core class in cocos2dx

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about what the core class in cocos2dx is, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

I. Node class (Node)

Any object to be displayed on the screen is the node class, the most common are scene (Scene), layer (Layer), menu (Menu) wizard (Sprite), the menu class is a subclass of the layer, the default anchor point for the initialization of the layer is (0c0), and the default anchor point for the wizard is (0.5). The position of a layer or a sprite on its parent class the setposition () function actually refers to the location of the anchor point. And the rotation, scaling and translation of node all kinds of action are based on anchor points.

II. Director category (Director)

The Director class is the core of the cocos2D-x game engine, which is used to create and control the display of the main screen, the start, end and pause of the game, all call the methods of the Director class, and call the standard method Director::getinstance ()-> function name of the methods in Director.

These methods of director category are more important.

Scene* getRunningScene (); get the scene that is running now. Note: the director class can only move one scene void runWithScene (Scene* scene) at a time; enter the main loop of the director by passing parameters to the scene.

Void replaceScene (Scene * scene); switch scenes

Void end (); end the game

Third, scene class

Scene has a subclass CCTransitionScene switching scene class, which has many subclasses, that is, many ways to switch scenes. All of these classes have these classes because when you run the Director::getinstance ()-> repleaceScene () method (when switching scenes), there will be a memory "peak". The new scene has not been released, and the old scene has already come in. The sum of the memory occupied by these two is this "peak". At this time, the phone will get stuck, and switching scene classes is equivalent to reading a progress bar.

There are many types of switching scenarios, that is, many forms. Here is an example of turning the page.

Scene * sce = HellowScene::create (); Director::getinstance ()-> replaceScene (CCTransitionPageTurn::create (1.0pence scepene false))

Fourth, the setting layer class (Layer)

The set layer class inherits not only the Node class, but also CCTouchDelegate,CCAcceleromelerDelegate, and CCKeypadDelegate, so it is also responsible for input, touch, and acceleration sensor functions.

CCLayerColor class, generally can be used as a mask, it can set the rbga value, version 3.0 can be set by setting its touch level higher than the button's touch level to prevent mask penetration problems, but not after 3.0. We can

Layer * cover = LayerColor::create (ccc4 (130130130200); cover- > setContentSize (CCSizeMake (800480)); cover- > setPosition (0L0); this- > addChild (cover); auto callback = [] (Touch *, Event *) {return true;}; auto listener = EventListenerTouchOneByOne::create (); listener- > onTouchBegan = callback; listener- > setSwallowTouches (true); / / do not propagate _ eventDispatcher- > addEventListenerWithSceneGraphPriority (listener,cover)

This prevents the penetration problem from happening. Just add an event listener, set the event listener to swallow touch, and return true in the onTouchBegan callback function.

CCScrollView is in the cocos extension library, using the # include "cocos-ext.h" header file and adding the USING_NS_CC_EXT; namespace. If you want to use JSON exported by cocosstudio, or if you use Extension extension libraries, you need to add libCocosStudio,libExtension,libGUI manually. Take the addition of the libExtension library as an example

First, right-click in Solutions, select add-> existing projects, find "cocos2d\ extensions\ proj.win32\ libExtensions.vcxproj" in the pop-up dialog box, and click OK.

Step 2, right-click your project and select "reference." Click add New reference below in the pop-up property page. In the pop-up child dialog box, check the three lib projects we just added and click OK to finish adding the project's reference to the library.

Step 3, add an include directory for your project.

$(EngineRoot)

$(EngineRoot) cocos

$(EngineRoot) cocos\ editor-support

Project-> Properties-> cplink + General-> attach include path

A small example of a simple Scroll class call:

Bool HelloWorld::init () {if (! Layer::init ()) {return false;} CCNode * pContainer = Layer::create (); pContainer- > setContentSize (CCSizeMake (800960)); CCSprite * spr = Sprite::create ("Guide_Light_New.png"); spr- > setPosition (400240); pContainer- > addChild (spr) ScrollView * scrolls = ScrollView::create (CCSizeMake (800240), pContainer); scrolls- > setDirection (ScrollView::Direction::VERTICAL); this- > addChild (scrolls); return true;}

CCMenu class, as long as note that this class is a subclass of Layer, is a layer, it can put a lot of MenuItem.

Control class and its subclasses. Here is an example of ControlSlider class.

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 (400240) Slider- > addTargetWithActionForControlEvents (this,cccontrol_selector (HelloWorld::sliderCallback), Control::EventType::VALUE_CHANGED); this- > addChild (slider); label = Label::create ("0", "", 25); label- > setPosition (400120); this- > addChild (label); return true;} void HelloWorld::sliderCallback (cocos2d::Object * sender, Control::EventType controlEvent) {auto slide_control = (ControlSlider*) sender / / get ControlSlider int current_value = slide_control- > getValue () through the callback parameter sender; / / get the current value of slide char str [32]; sprintf (str, "% d", current_value); label- > setString (str);}

5. Genie Sprite

The sprite class can be defined by a picture, or part of a picture, and Sprite and its subclasses can be used as children of the sprite batch class.

Sprite class

Texture atlas is to put some of the pictures we need on a fixed-size picture, which can save memory. Because the OpenGL mechanism processes a single image into a picture of the corresponding size to the n power of 2, putting the pictures together can save space.

Map class CCTexture2D, map class CCTexture2D is the concept of OpenGL, in OpenGL, the picture is called map, in Cocos2dx is the meaning of picture object, you can create sprite objects through it. It inherits directly from the Ref class

In the sprite batch class CCSpriteBatchNode, when you want to process two or more of the same sprites, if you render one by one, the OpenGL function will be called every time, because when the system renders a map on the screen, the graphics processing hardware must first prepare the wizard.

Dye, then render the drawing, and finally finish the clean-up work after rendering. The above is the fixed cost of each rendering, so that the frame rate will be reduced by about 15% or more. If you only prepare, render, and clean up all the same maps that need to be rendered, you can solve this problem. You can use the CCSpriteBatchNode class to batch these sprites, such as * * on the game screen, and so on. Use it as a parent layer to create child sprites, and use it to manage sprite classes, which can improve the efficiency of the program. (*, etc., many of the same sprites appear at one time.) it is important to note that the more sprite classes are added to the CCSpriteBatchNode class, the more effective the efficiency will be. You can think of the CCSpriteBatchNode class as the CCLayer class, but the CCSpriteBatchNode class only accepts the CCSprite class and its subclasses.

Bool HelloWorld::init () {if (! Layer::init ()) {return false;} CCSpriteBatchNode * batch = CCSpriteBatchNode::create ("info_prop9.png"); batch- > setPosition (0Let0); this- > addChild (batch); / / the default anchor point is 0PriteBatchNode, which is equivalent to a layer, except that it can only put wizard for (int I = 0; I) in it.

< 100; i++) { int x = CCRANDOM_0_1()*700+50; int y = CCRANDOM_0_1()*380+50;//CCRANDOM_0_1()这个宏是产生0-1之间的随机数 CCSprite *spr = Sprite::createWithTexture(batch->

GetTexture (); spr- > setPosition (XMagney); batch- > addChild (spr);} return true;}

Elf frame class CCSpriteFrame, the concept of elf frame is relative to animation. A sprite is a fixed node, it can have many sprite frames (CCSpriteFrame), and switching between them forms an animation.

The sprite frame cache class CCSpriteFrameCache is used to store sprite frames. Caching them in advance helps to improve the efficiency of the program. CCSpriteFrameCache is a singleton mode that does not belong to a sprite and is shared by all sprites.

VI. Camera CCCamera

All nodes have a camera class CCCamera. Only through the camera class will the node be rendered. When a node is scaled, rotated and positioned, the CCCamera class needs to be overridden so that the node can be re-rendered through the CCCamera class.

You can use Sprite * spr = Sprite::create ("aaa.png")

CCCamera * came = spr- > getCamera (); to get the camera that belongs to the node, and then do some operations, such as came- > setEyeXYZ (0,0, myogz); you can make a wizard look farther and farther away from itself by setting the value of Memphz.

7. Container CCArray

CCArray * array = CCArray::create (); it does not need to determine the type of storage object, each object type can be different.

8. Drawing graphics

The draw (Renderer * renderer, const kmMat4& transform, bool transformUpdated) function can be overridden and graphed in the node class CCNode

Void HelloWorld::draw (Renderer * renderer, const kmMat4& transform, bool transformUpdated) {Node::draw (renderer,transform,transformUpdated); / / Line drawing glLineWidth (3.0f); ccDrawColor4B (255Power0255255); ccDrawLine (Point (0meme 0), Point (800480)); / / circle glLineWidth (2); ccDrawColor4B (0255,255,255) CcDrawCircle (Point (400240), 50, CC_DEGREES_TO_RADIANS (90), 50, true); / draw polygons CCPoint vertices2 [] = {ccp (30130), ccp (30230), ccp (50200)}; ccDrawPoly (vertices2, 3, true); / / draw Bezier curves ccDrawQuadBezier (ccp (0480), ccp (400240), ccp (800480), 50);}

9. Timer

In the game, you often need to update some data or character location at regular intervals. These time scheduling functions are provided in Cocos2D-x, and all subclasses of the CCNode class have such functions.

1. Update timer

/ / start the timer

This- > scheduleUpdate ()

/ / override virtual function update

Void HelloWorld::update (float dt) {}

two。 Custom timer

/ / start the timer, delay execution by 2 seconds, execute every 3 seconds, and execute interval 1 second.

This- > schedule (schedule_selector (HelloWorld::log), 1meme 3mem2)

/ / callback function

Void HelloWorld::log (float dt) {} after reading the above, do you have any further understanding of what the core classes in cocos2dx are? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report