Search code examples
androidcocos2d-xgame-engineraytracing

Explaination of RayWenderlich's CCParallaxNodeExtras.h


I was going through the Ray Wenderlich's Space Game tutorial for android. Great Tutorial. I am an intermediate C++ programmer and I am trying to understand what's happening inside here. Just to understand I've rewritten it. Removing macros.

class CCPointObject: CCObject 
{
 protected:
      CCPoint m_tRatio;
      CCPoint m_tOffset;
      CCNode *m_pChild; 
 public:

virtual CCPoint getRatio() const
{
    CCLog("getRatio_CALLED");
    return m_tRatio;
}

virtual void setRatio(CCPoint newRatio)
{
    CCLog("setRatio_CALLED");
    m_tRatio = newRatio;
}
    .
    ..
    ...
    // similarly getChild/setChild/getOffset/setOffset is defined. The code works perfectly fine.
};

void CCParallaxNodeExtras::incrementOffset(CCPoint offset,CCNode* node)
{
    for( unsigned int i = 0; i < m_pParallaxArray->num; i++) 
    {
        CCPointObject *point = (CCPointObject *)m_pParallaxArray->arr[i];
        CCNode * curNode = point->getChild();
        if( curNode->isEqual(node) ) 
        {
            point->setOffset( ccpAdd(point->getOffset(), offset) );
            break;
        }
    }
}

MY DOUBTS:

  • when point is initialized (by casting CCObject* to CCPointObject*), how m_tOffset, m_tRatio and m_pChild is initialized to actual values of that Object's offset and ratio ?
  • I've given CCLogs in setOffset/getOffset functions of CCPointObject, but they have been never called so which setOffset/getOffset is called ? As CCPointObject is inherited from CCObject so I thought, might be CCObject's setOffset/getOffset is being called but, as i checked, there is no setOffset/getOffset declared in CCObject.
  • I looked in to ccParallaxNode.h and its there,

    //array that holds the offset / ratio of the children

    CC_SYNTHESIZE(struct _ccArray *, m_pParallaxArray, ParallaxArray)

    and _ccArray has CCObject** that means Array of CCObjects and CCObject does not have any variable as m_tOffset/m_tRatio or m_pChild so, where actually the offset/ratio is stored ?

These doubts are driving me nuts. Please help !!


Solution

  • 1) Type casting is to tell the compiler that the object to the right side of = is a type of object which is on the left side of =. So there is no initialization taking place here. We are not creating a new object, simply type casting it by which compiler assumes that the object belong to that class.

    2)Following is the definition of CCPointObject class

    class CCPointObject : CCObject
    {
        CC_SYNTHESIZE(CCPoint, m_tRatio, Ratio)
        CC_SYNTHESIZE(CCPoint, m_tOffset, Offset)
        CC_SYNTHESIZE(CCNode *,m_pChild, Child)    // weak ref
    
        static CCPointObject * pointWithCCPoint(CCPoint ratio, CCPoint offset)
        {
            CCPointObject *pRet = new CCPointObject();
            pRet->initWithCCPoint(ratio, offset);
            pRet->autorelease();
            return pRet;
        }
        bool initWithCCPoint(CCPoint ratio, CCPoint offset)
        {
            m_tRatio = ratio;
            m_tOffset = offset;
            m_pChild = NULL;
            return true;
        }
    };
    

    Dont know exactly where did you put the log but. setOffset and getOffset are defined implicitly inside the macro CC_SYNTHESIZE. No doubt these will be called, otherwise the complier raises an error or the app will crash during runtime.

    3)CC_SYNTHESIZE(struct _ccArray *, m_pParallaxArray, ParallaxArray) Here m_pParallaxArray is used to store CCPointObject, where each CCPointObject holds information of exactly one child of parallax node, i.e reference to the child itself m_pChild and m_pChild m_tRatio

    You can see definition of CCPointObject class in CCParallaxNodeExtras.cpp and CCParallaxNode.cpp