The Ccnode of Cocos2d Institute (III)

Source: Internet
Author: User
Tags addchild in degrees

http://www.xuanyusong.com/archives/950

In the previous chapter, we understand the project path and working principle of cocos2d, this time the author will Real thing to explain the code, we first look at cocos2d most commonly used, but also the author believes that the core of the class--ccnode.

As I said before, all classes of cocos2d start with CC, so the name of the class is actually node, and the class is the name, and the instance of the class is one. Cocos2d class is tree-like inheritance, and in memory, each instance is also associated with a "tree" data structure. , the importance of the tree in the cocos2d, no wonder a great man said, "To get rich, fewer children, many trees." (fog) So if there are children shoes do not understand the tree, it is recommended to look at the data structure, I believe it will be helpful for your coding. (in the following articles, the authors are guided by the knowledge of the tree to the reader)

In the actual application, in addition to the root node, each node has a parent node, any node can exist n child nodes, the structure diagram is as follows:

Each circle in the diagram is a node (that is, the Ccnode instance), the numbers in the circle are their numbers, and all the nodes are grouped together to form a tree. As we can see, 1 is the root node of the tree because it has no parent node, all the remaining nodes are its direct or indirect child nodes, 2 and 3 are child nodes of 1, 4 and 5 are child nodes of 2, and 6 are child nodes of 3. They can find each other through the connection between each other, that is 2 know 1 is its parent node, 4 and 5 is its child nodes, but do not know that 3 is its "brother" (no connection), only through 1 indirect search, so this "association" efficiency is the lowest, and there is no universal, in the development should try to avoid.

At the end of the previous chapter, the scene is a big stage for all the actors, and only the elements that are added to the scene will be displayed on the screen, so if the tree in the diagram is a system that works correctly, 1 must be a scene, 2 and 3 are probably layers, followed by a series of "actors". Perhaps some children's shoes will ask: What is the class of scenes and layers, they are not ccnode, why can they be added to the tree as their parent node? In fact, I also said in the last chapter, COCOS2D class is a layer of inheritance down, they have a common interface (all in the Ccnode declaration), the scene class is Ccscene, the layer class is Cclayer, they are ccnode subclasses, that is, they are actually a node And can be placed anywhere in the tree. Examples like this in the cocos2d there are many (such as elves), so that Ccnode is the core of cocos2d is not too much.

Knowing the importance of Ccnode, let's study its internal structure together.

First, let's look at what properties it has: (it inherits from NSObject, so it doesn't have to worry about compatibility with the foundation framework at all =)

Float Rotation_

The rotation angle of the node, if it is a picture, will see the rotation effect, so basically for the screen output service.

Float scalex_, Scaley_

The scale of the node, x is horizontal, y is portrait, the value is 1 when the original scale, 0.5 is half, 2 means magnification 1 time times, if it is a picture, you will see the effect, but also for the screen output service.

Cgpoint Position_

The coordinates of the node, the property is a struct, position_.x is its horizontal axis, position_.y is the ordinate, basically also to display to the screen makes sense.

Float Skewx_, Skewy_

The skew angle of the node, similar to the Twist feature in Windows Paint Board, X is a transverse twist, Y is a longitudinal twist, in degrees, and also on screen.

Cgpoint Anchorpointinpoints_

The anchor point coordinates of the node, the so-called anchor point is when the rotation, scaling or other way to operate the reference point of the node, the case of the anchor point in the center, when magnified it, the picture will spread around, when rotating an anchor point in the upper left corner of the picture, the picture will be rotated in the upper left corner axis.

Cgpoint Anchorpoint_

The ratio of the node's anchor point to the node position, such as a node with coordinates of (0, 0) and a width of 100, then anchorpoint_.x = 0.5,ANCHORPOINT_.Y = 50 when its anchor point coordinates are (50, 0.5).

Cgsize Contentsize_

The width of the node, it is also a structure, contentsize_.width represents a wide, contentsize_.height is high, it and position_ mates can be used to detect collisions, but need attention, contentsize_ The size is not changed by the rotation and scaling of the nodes.

Cccamera *camera_

node of the lens, cocos2d although the 2D game engine, but in fact it is the 3D way to draw 2D effect, so there is no strange lens presence. Cccamera is also a class of cocos2d, which by default is positive to the node, the distance is exactly the point can be displayed to the screen at 1:1, when we manually modify its parameters, the node will be rendered distorted or scaled effect.

Nsinteger Zorder_

The z-axis of a node, when there are multiple nodes on the screen, the engine determines the drawing order based on their z-axis size, and the z-axis is large to cover the small z-axis.

Ccarray *children_

The play is coming, the front says the feast point can have n child nodes, Children_ is the variable that stores the address of these child nodes, the node is to find their own child nodes through them. The type of Children_ is Ccarray, which is an array class that cocos2d itself encapsulates, each element stores a child node address, which introduces the tag mechanism that, when nodes have multiple subnodes, can find the one they want based on their tag.

Ccnode *parent_

Another focus--the parent node of the node. Because each node in the tree inherits from Ccnode, it is most foolproof to define the parent node with the Ccnode type, which allows the node to find its own parent node, since the parent node has at most one, so it is not necessary to save it with an array.

Nsinteger Tag_

The label of the node, mainly to help its parent find themselves, because the child nodes of each node is not necessarily unique, when the need to traverse the parameter can be used to distinguish, so the Brother node label try not to be the same.

Bool Isrunning_

Indicates whether the node is in the update logic, said before the node is not joined to the tree is not displayed on the screen, in fact, its logical operation is the same, that no parent node is not displayed, and does not handle its own logic, all its activities will stop. Then when the node is added to the tree, Isrunning_ will be automatically set to Yes, and it will begin to perform its own operation, when the node leaves the tree, Isrunning_ will be set to No, if you want to pause the operation in the tree, it will need to isrunning_ Manually set to No.

Bool Visible_

Corresponds to Isrunning_, which is used to indicate whether the node is drawing, and when Visible_ is no, the nodes are not drawn to the screen even in the tree, which is the equivalent of hiding them.

These are the more important properties of ccnode, but we do not operate them very often in development, but rather change them by the related methods declared in the class, and I will say the methods in Ccnode.

+ (ID) node

Instantiating a Ccnode object, in effect, encapsulates the alloc, Init, and Autorelease methods.

-(ID) init

Initialization, the parameter of some numeric type is 0, the scale is set to 1, the parameter of pointer type is set to nil.

-(ccarray*) Children

Returns the Children_ of the object, that is, gets all the child nodes.

-(Bool) visible

Returns the Visible_ parameter that is used to detect whether the node is currently being drawn.

-(void) setvisible: (Bool) visible

Sets the drawing state of the node, yes is visible, no is not visible

-(ccnode*) Parent

Returns the Parent_ of a node, that is, getting the parent node

-(void) SetParent: (ccnode*) Parent

Set the parent node, but do not modify the actual application, because it is very easy to make a bug, the correct method of modification will be described later.

-(Nsinteger) tag

Returns the Tag_ of the object, which is the label for the node.

-(Nsinteger) ZOrder

Returns the Zorder_ of the object, which is the z-axis.

-(Bool) isrunning

Returns the Isrunning_ of an object that detects whether the node is currently updating logic.

-(float) rotation

Returns the Rotation_ of the object, that is, gets the rotation angle of the node.

-(void) Setrotation: (float) rotation

Sets the Rotation_ value of the object, which is the rotation node.

-(Cgpoint) position

Returns the Position_ of the object, that is, the coordinates of the node.

-(void) SetPosition: (cgpoint) position

Sets the position_ of the object, that is, changes the coordinates of the node.

-(Cgpoint) Anchorpoint

Returns the Anchorpoint_ of the object, which is the anchor point for the node.

-(void) Setanchorpoint: (cgpoint) Anchorpoint

Sets the Anchorpoint_ of the object, that is, modifies the node's strokes.

-(Cgsize) contentsize

Returns the Contentsize_ of the object, that is, gets the width of the node.

-(void) Setcontentsize: (cgsize) contentsize

Sets the contentsize_ of the object, which changes the width of the node.

-(float) skewx

Returns the skewx_ of the object, that is, gets the lateral skew angle of the node.

-(void) Setskewx: (float) skewx

Sets the skewx_ of the object, which is the horizontal skew angle of the node.

-(float) skewy

Returns the Skewy_ of the object, that is, the angle of the vertical skew for the node.

-(void) Setskewy: (float) skewy

Sets the skewy_ of the object, which is the vertical skew angle of the node.

-(float) ScaleX

Returns the scalex_ of the object, that is, the horizontal scale of the node.

-(void) Setscalex: (float) ScaleX

Sets the scalex_ of the object, which is the horizontal scaling node.

-(float) ScaleY

Returns the Scaley_ of the object, that is, the vertical scale of the node.

-(void) Setscaley: (float) ScaleY

Sets the Scaley_ of the object, which is the vertical scaling node.

-(CGRect) BoundingBox

Returns an object's collision box (made up of coordinates and wide-height combinations), which changes as the node scales or rotates.

-(cccamera*) camera

Returns the Camera_ of the object, which is the lens that gets the node.

-(ccnode*) Getchildbytag: (Nsinteger) ATag

Returns an element in the Children_ array according to Tag_, which is a child node of the node based on the tag.

-(void) AddChild: (ccnode*) Child Z: (Nsinteger) z tag: (nsinteger) ATag

Add a Ccnode object to the Children_ array, that is, add a child node to the node, Z is the z-axis of the child node, and Atag is the label of the child node, and this method points the Parent_ pointer of the child node to itself, so this is the way to set the parent node's positive solution. More secure than the setparent.

-(void) AddChild: (ccnode*) Child Z: (Nsinteger) Z

-(void) AddChild: (ccnode*) Child

The Add child node method is defaulted to tag or z, and defaults to 0.

-(void) Removefromparentandcleanup: (Bool) cleanup

Removes a node from the Children_ array of its parent node, that is, out of the tree, cleanup indicates whether it is purged from memory.

-(void) RemoveChild: (ccnode*) Child cleanup: (Bool) cleanup

Removes a node from the Children_ array, which deletes a child node.

-(void) Removechildbytag: (Nsinteger) ATag cleanup: (Bool) cleanup

Deletes an element from the Children_ array according to Tag_, which deletes a child node of the node according to the label.

-(void) Removeallchildrenwithcleanup: (Bool) cleanup

Empties the object's Children_ array, which deletes all child nodes.

-(void) _setzorder: (Nsinteger) Z

Sets the zorder_ of the object, which changes the z axis of the node.

-(void) Reorderchild: (ccnode*) Child Z: (Nsinteger) Z

Removes a node from the child node queue and re-inserts the queue to change its z-axis.

-(void) visit

A drawing function to display the contents of a node (many pictures or text) to the screen.

-(void) OnEnter

is equivalent to an activation process that executes when a node is added to the tree, allowing it to update its own logic.

-(void) onExit

Deactivation is the function of pausing a logical update of a node.

-(ccaction*) Runaction: (ccaction*) action

A class that is often used to perform a behavior in which the behavior pattern is determined by the parameter action, Ccaction is the behavior class mentioned in the previous chapter, and Runaction is the bridge between the entity class and the behavior class. When the behavior is complete, the action is automatically freed (with Autorelease), so you don't have to worry about memory issues. It should be noted, however, that when Isrunning_ is no, the behavior of the node execution pauses, meaning that OnExit has the function of pausing all the nodes ' behavior.

-(void) stopallactions

Stop all behavior that the node is performing, note that this is a stop, not a pause, and all the behavior class instances in the node after the function execution are purged from memory.

-(void) Stopaction: (ccaction*) action

Stops a node's behavior and does nothing if the behavior is not performed.

-(void) Stopactionbytag: (Nsinteger) ATag

Stops a node's behavior according to the label.

-(ccaction*) Getactionbytag: (Nsinteger) ATag

Gets an executing behavior based on the tag.

-(void) Schedule: (SEL) selector interval: (cctime) Interval

Schedule is a very important mechanism in cocos2d, in the author's view its practicality is second only to the behavior class, its function and ccaction have similar place, is to carry out one kind of behavior, the difference is that schedule is executing the logic by invoking the method, And he has a timer inside it, which can be called every time, repeatedly, until it is stopped manually or the node is deleted (paused). Here the parameter selector is the execution method, interval is the interval time (seconds), Cctime is actually a floating point.

-(void) Unschedule: (SEL) Selector: (SEL) Selector

Stop a schedule task, notice here is stop is not paused.

-(void) unscheduleallseletors

Stop All schedule tasks

-(void) resumeschedulerandactions

Resumes the behavior and schedule of the running node.

-(void) pauseschedulerandactions

Pauses the behavior and schedule of the node.

The above is the entity class commonly used parameters and methods, hope that you can learn cocos2d helpful. It is important to note that when a node executes a method at the beginning of a set (that is, an assignment method) or a behavior, the effect is not limited to that node, but also to all its child nodes, taking the example diagram at the beginning, when Node 1 executes the SetPosition method, nodes 1, 2, 3, 4, 5, 6 shifts occur, but if the setposition is performed by node 2, only nodes 2, 4, 5 will move, and nodes 1, 3, 6 will not be affected. The use of this will make development more labor-saving, and conversely will add a lot of trouble to themselves, so in the design of the structure of the tree need to move some brains (of course, the author believes that this is more than the pros and cons).

With Ccnode, nature also need to learn ccaction, then the next chapter I will take you to understand this class, after mastering them, I will write some examples, in order to deepen understanding.

    • This article fixed link: http://www.xuanyusong.com/archives/950
    • Reprint Please specify: Lost Universe May 21, 2012 Yu Yussong Momo Program Research Institute published

The Ccnode of Cocos2d Institute (III)

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.