我正在浏览用于android的Ray Wenderlich's Space Game教程。伟大的教程。我是一名中级C ++程序员,我正在努力了解这里发生的事情。只是为了理解我已经重写了它。删除宏。
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;
}
}
}
我的疑惑:
这些疑惑让我疯狂。请帮忙 !!
1)类型转换是告诉编译器=
右侧的对象是=
左侧的一种对象。所以这里没有初始化。我们不是创建一个新对象,只需键入一个新的对象,编译器就假定该对象属于该类。
2)以下是CCPointObject
类的定义
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;
}
};
不知道你在哪里放了日志,但是。 setOffset
和getOffset
隐含地定义在宏CC_SYNTHESIZE
内。毫无疑问,这些将被调用,否则编译器会引发错误或应用程序将在运行时崩溃。
3)CC_SYNTHESIZE(struct _ccArray *, m_pParallaxArray, ParallaxArray)
这里m_pParallaxArray
用于存储CCPointObject
,其中每个CCPointObject
保存正好一个视差节点的孩子的信息,即参考孩子本身m_pChild
和m_pChild
m_tRatio
你可以在CCPointObject
和CCParallaxNodeExtras.cpp
看到CCParallaxNode.cpp
类的定义