Cocos2d-x Ding Jie niu (1): ccparallaxnode source code analysis

Source: Internet
Author: User

/** Ccpointobject is used to record the ratio and offset attributes of the ccparallaxnode child node **/class ccpointobject: ccobject {// parallax rate cc_synthesize (ccpoint, m_tratio, ratio) // offset cc_synthesize (ccpoint, m_toffset, offset) in the cursor cc_synthesize (ccnode *, m_pchild, child) // weak ref static ccpointobject * pointwithccpoint (ccpoint ratio, ccpoint offset) {ccpointobject * pret = new ccpointobject (); pret-> initwithccpoint (ratio, Fset); pret-> autorelease (); Return pret;} bool initwithccpoint (ccpoint ratio, ccpoint offset) {m_tratio = ratio; m_toffset = offset; m_pchild = NULL; return true ;}}; // condition constructor ccparallaxnode: ccparallaxnode () {m_pparallaxarray = ccarraynew (5); m_tlastposition = ccpointmake (-100,-100);} condition :: ~ Else () {If (m_pparallaxarray) {ccarrayfree (m_pparallaxarray); m_pparallaxarray = NULL;} else * vertex: node () {return ccparallaxnode: Create ();} required * nodes: Create () {ccparallaxnode * pret = new ccparallaxnode (); pret-> autorelease (); Return pret;} void ccparallaxnode: addchild (ccnode * child, unsigned int zorder, int tag) {cc_unused_param (zorder); CC _ Unused_param (child); cc_unused_param (TAG); ccassert (0, "parallaxnode: Use addchild: Z: parallaxratio: positionoffset instead");} void ccparallaxnode :: addchild (ccnode * child, unsigned int Z, const ccpoint & ratio, const ccpoint & offset) {ccassert (child! = NULL, "argument must be non-nil"); // record attribute ccpointobject * OBJ = ccpointobject: pointwithccpoint (ratio, offset); obj-> setchild (child ); // ccarrayappendobjectwithresize (m_pparallaxarray, (ccobject *) OBJ); // when a child is added when the ccparallaxnode is not (0, 0), a strange thing will happen. ccpoint Pos = m_tposition; POS. X = POS. x * ratio. X + offset. x; POS. y = POS. y * ratio. Y + offset. y; child-> setposition (POS); ccnode: addchild (child, Z, Child-> gettag ();} void ccparallaxnode: removechild (ccnode * child, bool cleanup) {// Delete attribute for (unsigned int I = 0; I <m_pparallaxarray-> num; I ++) {ccpointobject * point = (ccpointobject *) m_pparallaxarray-> arr [I]; If (point-> getchild () -> isequal (child) {ccarrayremoveobjectatindex (m_pparallaxarray, I, true); break ;}// delete node ccnode: removechild (child, cleanup);} void ccparallaxnode :: removeallchildren Withcleanup (bool cleanup) {groups (m_pparallaxarray); ccnode: Groups (cleanup);} ccpoint ccparallaxnode: absoluteposition () {ccpoint ret = m_tposition; ccnode * Cn = this; // perform layer-by-layer calculations to obtain absolute coordinates. as to why this is required, the following section comments the while (CN-> getparent ()! = NULL) {Cn = cn-> getparent (); ret = ccpadd (Ret, CN-> getposition ();} return ret ;} /* the positions are updated at visit because: -Using a timer is not guaranteed that it will called after all the positions were updated-overriding "Draw" will only precise if the children have a z> 0 */void ccparallaxnode :: visit () {// ccpoint Pos = position _; // ccpoint Pos = [self converttoworldspace: ccpointzero]; Ccpoint Pos = This-> absoluteposition (); // obtain the absolute coordinate if (! Ccpoint: ccpointequaltopoint (Pos, m_tlastposition) // dirty processing {// calculate the position of all children relative to ccparallaxnode. Note that when we move ccparallaxnode, this is actually a change in children's position. This change is the core design of this category. For (unsigned int I = 0; I <m_pparallaxarray-> num; I ++) {// ccpointobject * point = (ccpointobject *) m_pparallaxarray-> arr [I]; // For example, the absolute position of ccparallaxnode is 100, indicating that the child location is-100. The movement of ccparallaxnode is imperceptible, but the child location changes. // Simply, the camera moves like a camera scene. The camera is not moving, and the landscape changes. // if the ratio is 1, the postion = offset // if the ratio is (0, 1 ), position <OFFSET, slow moving speed // If ratio is (1, XX), then postion> offset, fast moving float x =-pos. X + POS. x * point-> getratio (). X + point-> getoffset (). x; // same as X float y =-pos. Y + POS. y * point-> getratio (). Y + point-> getoffset (). y; // the child's location is calculated based on the above two rows. Therefore, manually setting its postion does not apply to point-> getchild ()-> setposition (CCP (X, y);} // update the previous position m_tlastposition = Pos;} ccnode: visit ();}

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.