16. Cocos2dx 3.0 game development finding three nodes: parent Node, subnode, silly points unclear

Source: Internet
Author: User

 

 

Cocos2d-x uses the scene, layer, Genie hierarchy to organize the game elements, at the same time, this hierarchy also corresponds to the rendering level of the game, so the game elements can be organized into a tree structure, it is called a rendering tree.
The Cocos2d-x abstracts every game element on the rendering tree into a Node, that is, a Node. All game elements are inherited from Node, so they all have the features provided by Node.
Node defines the general features of a plotting object, including location, scaling, visibility, rotation angle, and so on. The most basic functions of a Node include: 1. Including other Node objects; 2. Accepting various events and callback functions, such as timer events; 3. Running actions.
The rendering tree contains nodes of many subnodes. Similar to a canvas, the rendering tree allows other canvases to overwrite themselves in different order. Once the Node scaling and rotation are changed, this will affect the display of the node (including its subnodes. For example, if you set the Rotation Angle of a node to 90 degrees, it rotates 90 degrees, but its subnodes remain unchanged in relative positions and angles. The drawing order is described by the localZOrder attribute, which is the same as the localZOrder parameter in addChild.
Coordinate System drawing Coordinate System in the Cocos2d-x. It is the most common coordinate system, and OpenGL uses the same coordinate system, with the lower left corner as the origin, to the right for the x axis positive direction, to the y axis positive direction, in the Cocos2d-x, all plotting operations use the drawing coordinate system, such as the Position and AnchorPoint attributes in the game element. Texture coordinate system. The texture coordinate system uses the upper left corner as the origin, the right side as the positive direction of the x axis, and the downward side as the positive direction of the y axis. In the Cocos2d-x, this coordinate system is used only when some rectangles are truncated from the texture, such as the TextureRect attribute of Sprite.
The plotting property of the Sprite. With these attributes, We can precisely control the way the genie is presented.
Size _ contentSize: Get or set the content Size of this node. Any node must determine its content size for graphic transformation. For the genie, _ contentSize is the size of the texture display part. For large nodes such as layers or scenes in full screen mode, _ contentSize is the screen size.
Point _ anchorPoint and Point _ position: _ anchorPoint are used to set an anchorPoint to precisely control the node position and transformation. The two parameters x and y of _ anchorPoint are usually real numbers between 0 and 1, indicating the position of the anchorPoint relative to the node length and width. For example, you can use the lower-left corner of a node as the anchor, and set the value to (0.5, 0.5). You can use the lower-right corner of a node as the anchor, the value is (1, 0 ). The default value of _ anchorPoint is (0.5, 0.5), and that of other nodes is ). _ Position is used to set the node position. Because _ position refers to the coordinate value of the anchor in the parent node, the position displayed on the node is usually related to the anchor. Therefore, if the layer and the scenario remain in the default position, you only need to set the sprite position in the layer to half the window width to display it in the center of the screen. When multiple nodes are nested, the origin of the coordinate system of each node is located in the lower left corner of its content. Therefore, the coordinates of the anchor in the parent node are actually the coordinates of the anchor in the lower left corner of the parent node. For large nodes such as scenes or layers, their _ ignoreAnchorPointForPosition attribute is true. In this case, the engine considers _ anchorPoint to always be (), while other nodes set this attribute to flase, their anchor points are not ignored.
Float Rotation: gets or sets the Rotation Angle of a node. The node rotates clockwise for a certain amount based on its own anchor. The unit is angle. The rotation angle can be any real number.
Float _ scaleX (and float _ scaleY and float _ scaleZ): Scale is used to obtain or set the scaling ratio of nodes. Nodes are scaled at the center of the anchor. The Scale value represents the overall scaling ratio, while _ scaleX and _ scaleY represent the zooming ratio in the X and Y directions respectively. By default, the values of these three attributes are 1, indicating that the node is not scaled. If the Scale attribute is set, _ scaleX and _ scaleY both change to the same value. Of course, we can also set different values for _ scaleX and _ scaleY, so that the value of the Scale attribute is meaningless.
Bool _ visible: gets or sets the node visibility. When _ visible is true, the node is displayed, and the node is not displayed. When the node is not displayed, the plotting method (visit and draw) is not called ). What makes this attribute different is that its accessors do not follow the naming rules of the attribute. The following is the method of its accessors:
    /**     * Sets whether the node is visible     *     * The default value is true, a node is default to visible     *     * @param visible true if the node is visible, false if the node is hidden.     */    virtual void setVisible(bool visible);    /**     * Determines if the node is visible     *     * @see `setVisible(bool)`     *     * @return true if the node is visible, false if the node is hidden.     */    virtual bool isVisible() const;

Float _ skewX and float _ skewY: obtain or set the oblique angle. The node is centered on the anchor and is deformed at a certain angle in the direction of the x or y axis. _ SkewX is the clockwise deformation of the parallel x axis, and _ skewY is the clockwise deformation of the parallel y axis, in the unit of angle. The default values of _ skewX and _ skewY are 0, indicating that the node is not skewed.
Int _ tag: gets or sets the node label. In the Cocos2d-x, _ tag acts like an identifier to quickly locate the desired node from all the child nodes of the node. _ Tag can be used to locate subnodes. Therefore, the _ tag of two nodes cannot be the same when added to all nodes of the same Node. Otherwise, the positioning will be troublesome. Methods related to _ tag include getTag and removeChildByTag.
Void * _ userData; // <A user assingned void pointer, Can be point to any cpp object void * _ userData;: obtains or sets additional information related to nodes. _ UserData is of the void * type. We can use this attribute to save any data.
Other Node attribute tables

Manage organization nodes

Timer events use game elements such as scenes, layers, and genie to build a game framework, but the game remains static. In all games, the status of the game changes with the passage of time. At the same time, we also need to make some logical judgments on a regular basis. In order to solve the above problems, the concept of timer is introduced. A timer is a tool that continuously triggers game events at a certain interval. Obviously, the timer is the tool needed to make the game dynamically change. The Cocos2d-x provides us with two ways to implement the timing mechanism-using the update method and using the schedule method; the two methods are briefly described below.
The first timer mechanism of the update timer is the update method of the Node refresh event. This method is triggered once before each frame is drawn. Since the drawing frame rate is limited, and each update will eventually be reflected on the screen, refresh between frames is sufficient to meet the requirements of most game logic processing. <Search small 3> collision detection mechanism can be fully implemented through update events.
Node does not enable the update event by default. To enable the timer, we need to call the scheduleUpdate method and reload the update to execute our own code. Correspondingly, we can use the unscheduleUpdate method to stop the timer.
Another timer mechanism of the schedule timer is the schedule method provided by Node, which can call a function continuously at a certain interval. Because of the engine scheduling mechanism, the interval must be greater than the interval of two frames. Otherwise, multiple calls during the two frames will be merged into one call. Therefore, the schedule timer is usually used in scheduled calls with a long interval. Generally, the interval between events should be more than 0.1 seconds. In actual development, many scheduled operations are implemented through the schedule timer;
For example, you can find the timer refresh of the time bar in the middle school. The following describes how to use the schedule timer by taking "look for three" as an example. In the game, sometimes the clock beats and the timeRunning method is constructed. In the initialization method, add the following code to enable the Timer:
this->schedule(schedule_selector(PlayGame::timeRunning),1.0f);
The timeRunning method updates the logic and the display of time clocks:
void PlayGame::timeRunning(float dt){ if (second <= 0) {  second = 0;  pLabelTime->setString(toTimeMode(second));  this->unschedule(schedule_selector(PlayGame::timeRunning));  return; } second --; pLabelTime->setString(toTimeMode(second));}

The schedule method of Node accepts a function pointer and starts a timer. The trigger interval and delay can be specified by using different overloading methods of schedule. Schedule_selector is a macro that converts a specified function to a function pointer and is used to create the function pointer required by the schedule method. The function that imports this macro should contain a float parameter, indicating the interval between the previous event triggering.
Timer related methods
The timer mechanism is the basis of the Cocos2d-x scheduling mechanism, and the action mechanism of the cocos2d-x also relies on the timer. Since Cocos2d-x scheduling is purely a serial mechanism, so all functions run in the same thread, there is no parallel program of all kinds of trouble, which greatly simplifies the programming complexity.
Other process control-related events
The default implementation of these events is usually responsible for enabling and pausing the timer and action. Therefore, you must call the parent class method in the overload method. For example, we can set the event listener at the beginning of the game layer:

Haomeng master friendly reminder: Learn to use Node to build your game rendering tree ,,,

 

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.