Continue with the previous article "cocos2dx advanced" debugging Article 1) Basic Introduction. This article mainly describes some minor modifications to Cocos2dx.
Let's talk about Cocosdx's debugging design. It consists of two parts: log output and node information feedback.
Log output
In fact, the core function is cocos2d: If CCLog is called directly, information will be output. This function encapsulates platform functions. If you need it, you can view the relevant platform code.
Some people want to say that my output needs to be classified and then work only in debug, so we have the following extended macro definitions.
#define __CCLOGWITHFUNCTION(s, ...) \ CCLog("%s : %s",__FUNCTION__, CCString::createWithFormat(s, ##__VA_ARGS__)->getCString())// cocos2d debug#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0#define CCLOG(...) #define CCLOGINFO(...) #define CCLOGERROR(...)#define CCLOGWARN(...) #elif COCOS2D_DEBUG == 1#define CCLOG(format, ...) cocos2d::CCLog(format, ##__VA_ARGS__)#define CCLOGERROR(format,...) cocos2d::CCLog(format, ##__VA_ARGS__)#define CCLOGINFO(format,...) do {} while (0)#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)#elif COCOS2D_DEBUG > 1#define CCLOG(format, ...) cocos2d::CCLog(format, ##__VA_ARGS__)#define CCLOGERROR(format,...) cocos2d::CCLog(format, ##__VA_ARGS__)#define CCLOGINFO(format,...) cocos2d::CCLog(format, ##__VA_ARGS__)#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)#endif // COCOS2D_DEBUG
_ CCLOGWITHFUNCTION can print the current function name.
Node Information
Cocos2dx adds the description function to output debugging information more conveniently.
// Early versions of char * description (); // 3.0 betavirtual std: string getDescription () const;
With the above output function, you can easily display relevant information
Now, let's talk about the information output function.
In earlier versions, char * is returned, but memory leakage is introduced. Although debugging information is involved, there is always something wrong.
The description function is not inherited. Some classes do not exist, so it is embarrassing. After 3.0, I changed it.
Description can only be used on the inheritance chain of ccnode. It is a little restrictive. You have to do it yourself elsewhere.
There is no major problem with the output function, but the output depends on the DEBUG definition. If the output is not visible, the debug macro definition is incorrect.
Okay, no more. Start transformation.
We know that the bottom layer of Cocos2dx is ccobject. For better debugging, we will start from here.
Add a function to the ccobject class:
# If (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32) public: // output function virtual void Dump () {} protected: // format the output information virtual std: string dumpInfo () {return std:: string () ;}# endif
Therefore, we have a unified debugging function Dump and a dumpInfo for formatting the output. What is the use of this? Isn't it just two function definitions? Well, let me give an example.
Let's talk about the output node information, you can refer to the http://4137613.blog.51cto.com/4127613/1350243 of the old G
To implement this function, you can
/// // Ccnode. h // class CC_DLL CCNode: public CCObject {// skip other, add # if (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32) public: virtual void Dump (); protected: void dump (int); virtual std: string dumpInfo () at the bottom of the class (); # endif // end };
The function in ccobject is reloaded.
///////////////////////////// ccnode.cpp///////////////////////////#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)void CCNode::Dump(){ CCLog("==================== dump ===================="); dump(0); CCLog("==============================================");}std::string CCNode::dumpInfo(){ std::stringstream ss; ss <<"[type]" <<typeid(this).name() <<" [tag]" <<getTag() <<" [visible]" <<getIsVisible() <<" [postion]" <<getPositionX()<<","<<getPositionY(); return ss.str();}void CCNode::dump( int index){ std::string info(dumpInfo()); std::string strStruct; for(int i=0;i<index;++i) strStruct+="| "; strStruct+="+ - "; CCLog("%s%s",strStruct.c_str(),info.c_str()); if(m_pChildren && m_pChildren->count() > 0) { // draw children zOrder < 0 ccArray *arrayData = m_pChildren->data; for(int i=0 ; i < arrayData->num; i++ ) { CCNode* pNode = (CCNode*) arrayData->arr[i]; if ( pNode) { pNode->dump(index +1); } } }}#endif
Additional instructions:
dump(
int
) The function is used for recursive calling. index indicates the depth.
Typeid is an operator used to obtain the specific type of a variable. VS needs to enable the runtime type information and C ++ exception when compiling shi
If you are not satisfied with the common output content of ccnode, You can reload dumpInfo to customize the output content.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140207/222T04L8-0.jpg "title =" 2014-02014-02014-02014-01-20 1161.51.05.png "alt =" wkiol1lcubtzdj1_alxl9xmd5o434.jpg "/>
Here is another useful example.
Touch operations are now the mainstream Method for mobile games. Cocos2dx also implements two methods of targeted single point) and standard multi point ). You can easily monitor touch events by registering handler.
The design is good, but the reality is always a little troublesome. After the code is complicated, the function is often not responded after it is touched. Why? The code is correct. Let's go to CCTouchDispatcher. after cpp, I felt dizzy. Oh, my God, help me ......
At this time, the savior came,
//// // CCTouchDispatcher. h // The CCTouchDispatcher class adds the debugging overload function # if (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32) virtual void Dump (); # endif
Implementation
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)void CCTouchDispatcher::Dump(){ CCLog("========= dump for CCTouchDispatcher ========="); // optimization to prevent a mutable copy when it is not necessary unsigned int uTargetedHandlersCount = m_pTargetedHandlers->count(); unsigned int uStandardHandlersCount = m_pStandardHandlers->count(); CCLog("TargetedHandlersCount=%d",uTargetedHandlersCount); CCLog("StandardHandlersCount=%d",uStandardHandlersCount); // // process the target handlers 1st // if (uTargetedHandlersCount > 0) { CCLog("========= Targeted Handlers"); CCTargetedTouchHandler *pHandler; CCMutableArray<CCTouchHandler*>::CCMutableArrayIterator arrayIter; for (arrayIter = m_pTargetedHandlers->begin(); arrayIter != m_pTargetedHandlers->end(); ++arrayIter) { pHandler = (CCTargetedTouchHandler *)(*arrayIter); if (! pHandler) break; CCLog("[%d]%s,SwallowsTouches=%d",pHandler->getPriority(),typeid(*(pHandler->getDelegate())).name(),pHandler->isSwallowsTouches()); } } // // process standard handlers 2nd // if (uStandardHandlersCount > 0 ) { CCLog("========= Standard Handlers"); CCMutableArray<CCTouchHandler*>::CCMutableArrayIterator iter; CCStandardTouchHandler *pHandler; for (iter = m_pStandardHandlers->begin(); iter != m_pStandardHandlers->end(); ++iter) { pHandler = (CCStandardTouchHandler*)(*iter); if (! pHandler) { break; } CCLog("[%d]%s",pHandler->getPriority(),typeid(*(pHandler->getDelegate())).name()); } } CCLog("==============================================");}#endif
Call method, wherever you need
cocos2d::CCTouchDispatcher::sharedDispatcher()->Dump();
The effect is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140207/222T054A-1.jpg "title =" 2014-02014-02014-02014-01-20 1162.18.41.png "alt =" wKiom1LcwAbRApMtAACN_L8muCE796.jpg "/>
Well, the rest depends on everyone. Next time, we will make some minor changes to CCLOG.
This article is from the "game Life" blog. For more information, contact the author!