Cocos2d-x结构学习(十)CCMenu、CCLayerRGBA、CREATE_FUNC、CCMenuItem、tCCMenuState

前端之家收集整理的这篇文章主要介绍了Cocos2d-x结构学习(十)CCMenu、CCLayerRGBA、CREATE_FUNC、CCMenuItem、tCCMenuState前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、CCMenu:菜单类,继承自CCLayerRGBA

class CC_DLL CCMenu : public CCLayerRGBA
{
    bool m_bEnabled;                  //标记菜单是否接收事件
    
public:
    CCMenu() : m_pSelectedItem(NULL) {}
    virtual ~CCMenu(){}

    static CCMenu* create();                                //几个创建菜单函数
    static CCMenu* create(CCMenuItem* item,...);
    static CCMenu* createWithArray(CCArray* pArrayOfItems);
    static CCMenu* createWithItem(CCMenuItem* item);
    static CCMenu* createWithItems(CCMenuItem *firstItem,va_list args);

    bool init();                                      //初始化相关函数
    bool initWithArray(CCArray* pArrayOfItems);

    void alignItemsVerticallyWithPadding(float padding);                //下面是和菜单对齐相关的一组函数
    void alignItemsHorizontally();
    void alignItemsHorizontallyWithPadding(float padding);
    void alignItemsInColumns(unsigned int columns,...);
    void alignItemsInColumns(unsigned int columns,va_list args);
    void alignItemsInColumnsWithArray(CCArray* rows);
    void alignItemsInRows(unsigned int rows,...);
    void alignItemsInRows(unsigned int rows,va_list args);
    void alignItemsInRowsWithArray(CCArray* columns);

    void setHandlerPriority(int newPriority);        //设置处理的优先级

    virtual void addChild(CCNode * child);                                  //孩子节点处理相关函数
    virtual void addChild(CCNode * child,int zOrder);
    virtual void addChild(CCNode * child,int zOrder,int tag);
    virtual void registerWithTouchDispatcher();
    virtual void removeChild(CCNode* child,bool cleanup);

    virtual bool ccTouchBegan(CCTouch* touch,CCEvent* event);                 //触摸处理相关函数
    virtual void ccTouchEnded(CCTouch* touch,CCEvent* event);
    virtual void ccTouchCancelled(CCTouch *touch,CCEvent* event);
    virtual void ccTouchMoved(CCTouch* touch,CCEvent* event);

    virtual void onExit();                  //退出

    virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
    virtual bool isOpacityModifyRGB(void) { return false;}
    
    virtual bool isEnabled() { return m_bEnabled; }                         //判断和设置是否混合
    virtual void setEnabled(bool value) { m_bEnabled = value; };
protected:
    CCMenuItem* itemForTouch(CCTouch * touch);
    tCCMenuState m_eState;
    CCMenuItem *m_pSelectedItem;
};
2、CCLayerRGBA:继承自CCLayer和CCRGBAProtocol,颜色相关类
class CC_DLL CCLayerRGBA : public CCLayer,public CCRGBAProtocol
{
public:
    CREATE_FUNC(CCLayerRGBA);
    CCLayerRGBA();
    virtual ~CCLayerRGBA();
    virtual bool init();
    
    virtual GLubyte getOpacity();
    virtual GLubyte getDisplayedOpacity();
    virtual void setOpacity(GLubyte opacity);
    virtual void updateDisplayedOpacity(GLubyte parentOpacity);
    virtual bool isCascadeOpacityEnabled();
    virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);
    
    virtual const ccColor3B& getColor();
    virtual const ccColor3B& getDisplayedColor();
    virtual void setColor(const ccColor3B& color);
    virtual void updateDisplayedColor(const ccColor3B& parentColor);
    virtual bool isCascadeColorEnabled();
    virtual void setCascadeColorEnabled(bool cascadeColorEnabled);
    
    virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
    virtual bool isOpacityModifyRGB() { return false; }
protected:
	GLubyte		_displayedOpacity,_realOpacity;
	ccColor3B	_displayedColor,_realColor;
	bool		_cascadeOpacityEnabled,_cascadeColorEnabled;
};
3、CREATE_FUNC:定义一个创建函数的宏,使编写更简便
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}
4、CCMenuItem:菜单项,继承自CCNodeRGBA
class CC_DLL CCMenuItem : public CCNodeRGBA
{
protected:
    bool m_bSelected;              //是否被选中
    bool m_bEnabled;

public:
    CCMenuItem()
    : m_bSelected(false),m_bEnabled(false),m_pListener(NULL),m_pfnSelector(NULL),m_nScriptTapHandler(0)
    {}
    virtual ~CCMenuItem();

    static CCMenuItem* create();
    bool initWithTarget(CCObject *rec,SEL_MenuHandler selector);
    CCRect rect();
    virtual void activate();

    virtual void selected();            //选中
    virtual void unselected();         //取消选中
    
    virtual void registerScriptTapHandler(int nHandler);          //脚本处理例程相关
    virtual void unregisterScriptTapHandler(void);
    int getScriptTapHandler() { return m_nScriptTapHandler; };

    virtual bool isEnabled();                   //启用和选择相关
    virtual void setEnabled(bool value);
    virtual bool isSelected();      
    
    void setTarget(CCObject *rec,SEL_MenuHandler selector);   //设置回调函数
protected:
    CCObject*       m_pListener;
    SEL_MenuHandler    m_pfnSelector;
    int             m_nScriptTapHandler;
};
4、tCCMenuState:菜单状态
typedef enum  
{
    kCCMenuStateWaiting,kCCMenuStateTrackingTouch
} tCCMenuState;
原文链接:https://www.f2er.com/cocos2dx/343804.html

猜你在找的Cocos2d-x相关文章