SwitchControl in Cocos2d-X, cocos2dx authoritative guide

Source: Internet
Author: User

SwitchControl in Cocos2d-X, cocos2dx authoritative guide

The SwitchControl controls act as switches in real life.

Since the control is relatively simple, I will not explain it too much and directly go to the code


First, add three images to the Resource folder under the project directory.


Add the following code in SwitchControl. h:

#ifndef   _SwitchControl_H_#define  _SwitchControl_H_#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class SwitchControl : public CCLayer {public:    static CCScene* scene();CREATE_FUNC(SwitchControl);bool init();void switchValueChanged(CCObject*, CCControlEvent);};#endif


Add the following code to SwitchControl. cpp:

# Include "SwitchControl. h "CCScene * SwitchControl: scene () {CCScene * s = CCScene: create (); SwitchControl * layer = SwitchControl: create (); s-> addChild (layer); return s;} bool SwitchControl: init () {CCLayer: init (); // obtain the window size. CCSize winSize = CCDirector :: sharedDirector ()-> getWinSize (); // set the text opened by the ControlSwitch control to No "CCLabelTTF * on = CCLabelTTF: create (" ON "," Arial ", 16 ); // set the text "OFF" CCLabelTTF * off = CCLabelTTF: create ("OFF", "Arial", 16) when the ControlSwitch control is disabled ); // set the color of the text opened by the ControlSwitch control on-> setColor (ccc3 (0, 0, 0 )); // set the color off when the ControlSwitch control is disabled-> setColor (ccc3 (0, 0, 0); // create the ControlSwitch control CCControlSwitch * control = CCControlSwitch: create (CCSprite:: create ("switch-mask.png"), CCSprite: create ("switch-on.png"), CCSprite: create ("switch-off.png"), CCSprite: create ("switch-thumb.png"), on, off); // Add the ControlSwitch control addChild (control); // set the position of the ControlSwitch control-> setPosition (ccp (winSize. width/2, winSize. height/2); // register the valuechange message. When valuechange occurs, call the switchValueChanged function control-> addTargetWithActionForControlEvents (this, cccontrol_selector (SwitchControl: switchValueChanged), CCControlEventValueChanged ); return true;} void SwitchControl: switchValueChanged (CCObject * sender, CCControlEvent ev) {if (ev = CCControlEventValueChanged) {CCControlSwitch * control = (CCControlSwitch *) sender; if (control-> isOn () {CCLog ("Switch if ON");} else {CCLog ("Swith is Off ");}} else {CCLog ("other events ");}}

Execution result:


Demo effect:


Zookeeper


Usage and consideration of several drawing methods of cocos2d-x

CCRenderTexture's own understanding: CCRenderTexture is similar to a blank canvas. You can record the movement marks of the brush in the touch event through the CCSprite * to draw various artistic effects. The record method is very simple. First, the CCRenderTexture calls its own begin () function, enables the record function, then calls the brush-visit () to draw itself on this canvas, and finally the CCRenderTexture calls end () end record, then OK.
Here I think the CCRenderTexture overwrites the Texture of the paint brush to its own Texture instead of creating new textures, so the consumption is relatively low, even if the painting is frequent, the number of frames can also remain stable, which is a very good class.
As long as the record function is enabled for begin (), any subsequent CCNode * object can draw itself on it as long as visit () is called. So, the general game screenshot function, can fully use the CCRenderTexture to achieve, specific can see in tests example, cocos2d-x has provided related examples, look at the source code can be understood.
The advantage of using CCRenderTexture can be very simple to achieve the ideal drawing effect (as long as you build a small brush image, then load it with CCSprite, and then call a CCSprite visit () as appropriate () that's it.) The number of frames is low, and it is easy to implement the screenshot function of the game. The first thing you should think of is "you draw my guess.
The disadvantage is that the hardware or OpenGL versions of each Android phone are different. As a result, some mobile phones use the CCRenderTexture, such as HTC. Even the official tests example is hard to escape. This fatal weakness leads to the fact that "you draw my guess" cannot be used across mobile phones.
This Bug I think is not a problem with the cocos2d-x engine, some cool people say it is because Texture is being painted repeatedly (only draw once OK, so the screenshot function should not be affected ), maybe every Android phone OpenGL is not the same, so the problem has not been solved, can only wait for the cocos2d-x or other cool people can fix it.

The use of CCArray in the COCOS2d-x removes the element's doubts

Use dynamic_cast conversion. If the type does not match, a null pointer is returned.
CCSprite * sp = CCSprite: create ();
CCLabelTTF * pLabel = dynamic_cast <CCLabelTTF *> (sp );

In this case, pLabel = nullptr.

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.