Application of Cocos2d-X Middle Layer

Source: Internet
Author: User

Application of Cocos2d-X Middle Layer
(Layer) Laye: A Node subclass that processes player event responses. Different from the scenario, a layer usually contains content directly displayed on the screen and can accept user input events, including touch, accelerometer, and keyboard input. We need to add genie, text tags, or other game elements to the layer, and set the game element attributes, such as location, direction, and size, and set the action of game elements. Normally, the object functions in the layer are similar, and the coupling is tight. The logic Code related to the game content in the layer is also written in the layer. After the layer is organized, you only need to add layers to the scene in sequence to display them. To add a layer to a scenario, we can use the addChild method.

You can use the following three classes to set layer attributes:

CCLayerColor: You can change the background color and set the size.

CCLayerGradient: Set the background with gradient effect

CCLayerMultplex: it can contain several layers, but only displays one layer. This allows switching between multiple layers.


First, you need to understand some basic concepts.

Three colors: most colors can be synthesized by Red, green, and blue according to different proportions. Similarly, the vast majority of monochrome light can be divided into three colors: Red, green, and blue.


Use the three parameters of ccc4 (R, G, B, transparency) IN THE Cocos2d-x to represent (RGB) tricolor

Ccc4 (255, 0, 0,255) indicates red, and 255 in the last parameter indicates that the transparency is 255, indicating that the transparency is not transparent.

Ccc4 (0,255, 0,255) indicates green

Ccc4 (0, 0,255,255) indicates blue

Different combinations of the preceding three parameters can represent different colors, for example, ccc4 (255,255,255,255) indicates white, ccc4 (0, 0, 0,255) indicates black, and other good combinations, we can see


Create a scenario named Layer and add the following code in Layer. h.

# Ifndef _ Layer_H _ # define _ Layer_H _ // prevents code re-inclusion # include "cocos2d. h "USING_NS_CC; class Layer: public CCLayer {public: // create a scenario static CCScene * scene (); // initialize the scenario bool init (); CREATE_FUNC (Layer );}; # endif

Add the following code to Layer. cpp:

# Include "Layer. h "# include" HelloWorldScene. h "CCScene * Layer: scene () {// create a scenario CCScene * s = CCScene: create (); // create a layer Layer * layer = Layer :: create (); // Add the layer to the scenario s-> addChild (layer); return s;} bool Layer: init () {return true ;}


Finally, call the created Layer scenario: Open the AppDelegate. cpp file, add the header file "Layer. h" to AppDelegate. cpp, and set boolAppDelegate: applicationDidFinishLaunching () in

// create a scene. it's an autorelease object    //CCScene *pScene = Sprite::scene();

Modify

  // create a scene. it's an autorelease object     CCScene *pScene = Layer::scene();


Modify the background color:

Method 1: Add the following code to the bool Layer: init () function under Layer. cpp.

// Initialize the parent class layer CCLayer: init (); // set the background color to Red CCLayerColor * layer = CCLayerColor: create (ccc4 (255, 0, 0,255 )); // Add the layer to the scenario addChild (layer); return true


Program Execution result:


Method 2:

class Layer : public CCLayer

Modify

class Layer : public CCLayerColor


Add the following code to the bool Layer: init () function under Layer. cpp:

// Initialize the parent class layer CCLayerColor: initWithColor (ccc4 (255,255, 0,255); return true;

Program Execution result: (same as method 1)



Set the background color of the window to red or green.

Change the code in the bool Layer: init () function under Layer. cpp

// Initialize the parent class layer and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255); // obtain the window size CCSize winSize = CCDirector:: sharedDirector ()-> getWinSize (); // set the color of the window background to green CCLayerColor * layer = CCLayerColor: create (ccc4 (0,255, 0,255), winSize. width/2, winSize. height/2); addChild (layer); return true;

Execution result:


Set the gradient effect of the color in the window:
Change the code in the bool Layer: init () function under Layer. cpp

// Initialize the parent class layer and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255 )); // set the gradient effect to re-green gradient to Blue CCLayerGradient * layer = CCLayerGradient: create (ccc4 (0,255, 0,255), ccc4 (0, 0,255,255); addChild (layer ); return true;

Execution result


Set the gradient effect of the color in the window and implement the transparency gradient:
Change the code in the bool Layer: init () function under Layer. cpp

// Initialize the parent class layer, and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255); // set the gradient effect to green and gradient to blue, transparency gradient CCLayerGradient * layer = CCLayerGradient: create (ccc4 (0,255, 0, 0), ccc4 (0, 0,255,255); addChild (layer); return true;

Program Execution result:


Modify the gradient direction: Modify the gradient direction by modifying the function parameters.
Change the code in the bool Layer: init () function under Layer. cpp

// Initialize the parent class layer, and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255); // set the gradient effect to green and gradient to blue, opacity gradient ccp (255,255) from bottom up gradient CCLayerGradient * layer = CCLayerGradient: create (ccc4 (0,255, 0,255,255), ccc4 (0,), ccp ));; addChild (layer); return true;


Execution result:


Modify the gradient direction: Use the setVector () function to modify the gradient direction.
Change the code in the bool Layer: init () function under Layer. cpp

// Initialize the parent class layer and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255 )); // set the gradient effect to the blue CCLayerGradient * layer = CCLayerGradient: create (ccc4 (255,255, 0,255), ccc4 (0, 0,255,255); addChild (layer ); // modify the gradient direction, and change the gradient direction to bottom-up layer-> setVector (ccp (); return true;

Execution result:


Switch the background color by clicking the mouse

Change the code in Layer. h

# Ifndef _ Layer_H _ # define _ Layer_H _ // prevents code re-inclusion # include "cocos2d. h "USING_NS_CC; class Layer: public CCLayerColor {public: // create a scenario static CCScene * scene (); // initialize the scenario bool init (); // CCLayerMultiplex object for multi-Layer switching * multi; // response to the mouse click event bool ccTouchBegan (CCTouch *, CCEvent *); // create FUNCTION CREATE_FUNC (Layer );}; # endif

Change the code in Layer. cpp

# Include "Layer. h "# include" HelloWorldScene. h "CCScene * Layer: scene () {// create a scenario CCScene * s = CCScene: create (); // create a layer Layer * layer = Layer :: create (); // Add the layer to the scenario s-> addChild (layer); // return scenario return s;} bool Layer: init () {// initialize the parent class layer, and set the color of the window background to Red CCLayerColor: initWithColor (ccc4 (255, 0, 0,255 )); // set the background color of the window to green CCLayerColor * layer1 = CCLayerColor: create (ccc4 (0,255, 0,255 )); // set the background color of the window to Red CCLayerColor * layer2 = CCLayerColor: create (ccc4 (255, 0, 0,255); // multi-layer multi = CCLayerMultiplex: create (layer1, layer2, NULL); // multi-> switchTo (1); addChild (multi); // use the mouse to switch multiple layers setTouchEnabled (true); setTouchMode (kCCTouchesOneByOne); return true ;} // process the mouse click event bool Layer: ccTouchBegan (CCTouch *, CCEvent *) {static int index = 0; multi-> switchTo (index = 1-index ); return true ;}


Execution result: you can click the mouse to switch the background color of the program.


Related Article

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.