In order to facilitate game developers, Cocos2d-x has built in three special Layer; specific as follows: LayerColor: a pure solid color block. LayerGradient: A color block, but you can set gradient effects for the two colors. Menu: a very common game Menu.
LayerColor and LayerGradient are very simple and contain only one color block. The difference is that the former creates a solid color block, while the latter creates a gradient block. Let's take a look at the specific effect:
LayerColor has the following initialization Methods: If the width and height of the initialization method are specified, a color block of the specified size is created; if the size of the initialization method is not specified, creates a Color Block of the screen size.
The method and initialization method of LayerColor are as follows:
/** creates a fullscreen black layer */ static LayerColor* create(); /** creates a Layer with color, width and height in Points */ static LayerColor * create(const Color4B& color, GLfloat width, GLfloat height); /** creates a Layer with color. Width and height are the window size. */ static LayerColor * create(const Color4B& color);CC_CONSTRUCTOR_ACCESS: LayerColor(); virtual ~LayerColor(); bool init(); bool initWithColor(const Color4B& color, GLfloat width, GLfloat height); bool initWithColor(const Color4B& color);
LayerGradient is similar to LayerColor, but it must specify two colors and gradient directions during initialization. In the initialization method, the start parameter is the starting color, the end parameter is the ending color, and the v parameter is the direction vector. The method for creating and initializing LayerGradient is as follows:
/** Creates a fullscreen black layer */ static LayerGradient* create(); /** Creates a full-screen Layer with a gradient between start and end. */ static LayerGradient* create(const Color4B& start, const Color4B& end); /** Creates a full-screen Layer with a gradient between start and end in the direction of v. */ static LayerGradient* create(const Color4B& start, const Color4B& end, const Point& v);CC_CONSTRUCTOR_ACCESS: LayerGradient(); virtual ~LayerGradient(); virtual bool init(); /** Initializes the Layer with a gradient between start and end. * @js init * @lua init */ bool initWithColor(const Color4B& start, const Color4B& end); /** Initializes the Layer with a gradient between start and end in the direction of v. * @js init * @lua init */ bool initWithColor(const Color4B& start, const Color4B& end, const Point& v);
After creating a color block, you can also modify the color block size using the methods listed below:
/** change width in Points*/ void changeWidth(GLfloat w); /** change height in Points*/ void changeHeight(GLfloat h); /** change width and height in Points @since v0.8 */ void changeWidthAndHeight(GLfloat w ,GLfloat h);
Menu: A game Menu is an indispensable part of a game. In the Cocos2d-x, a menu consists of a menu item and the menu itself. MenuItem indicates a Menu item. Each Menu item is an independent button that defines the visual presentation and Response Actions of the Menu, it organizes menu items, adds them to the scene, and converts the touch events of the screen to various menu items.
To create a menu item, you must specify the normal status, press status, response object after clicking, and response method. The following uses an image menu item as an example to describe how to create a menu item. The relevant code is as follows:
// Create a close menu item with close icon, it's an auto release object. menuItemImage * pCloseItem = MenuItemImage: create (CloseNormal.png, // picture CloseSelected.png in normal state, // picture CC_CALLBACK_1 (HelloWorld: menuCloseCallback, this) in pressed State )); // Response Function and object CC_BREAK_IF (! PCloseItem );
When the button is clicked, the callback function is called to the menuCloseCallback function: Execute the final game logic: Director: getInstance ()-> end ();
Haomeng's main friendly reminder: using more features provided by the engine will save a lot of time and effort!