Cocos2d-x结构学习(四)CCLayer、CCApplicationProtocol、CCApplication

1、CCLayer(层):继承自CCNode、CCTouchDelegate、CCAccelerometerDelegate和CCKeypadDelegate类。

class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate
{
public:
    CCLayer();
    virtual ~CCLayer();
    virtual bool init();          //初始化函数
    
    static CCLayer *create(void);   //创建一个层
 
    virtual void onEnter();        //进入和退出
    virtual void onExit();
    virtual void onEnterTransitionDidFinish();
    
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);       //触摸事件处理
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);    //复数触摸事件处理
    virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

    virtual void didAccelerate(CCAcceleration* pAccelerationValue);    //加速度注册处理
    void registerScriptAccelerateHandler(int nHandler);
    void unregisterScriptAccelerateHandler(void);

    virtual void registerWithTouchDispatcher(void);         //触摸派遣注册相关
    virtual void unregisterScriptTouchHandler(void);
    virtual bool isTouchEnabled();
    virtual void setTouchEnabled(bool value);
    
    virtual void setTouchMode(ccTouchesMode mode);    //触摸模式
    virtual int getTouchMode();
    
    virtual void setTouchPriority(int priority);     //触摸事件优先级,默认为0
    virtual int getTouchPriority();

    virtual bool isAccelerometerEnabled();                //加速度相关处理
    virtual void setAccelerometerEnabled(bool value);
    virtual void setAccelerometerInterval(double interval);

    virtual bool isKeypadEnabled();                            //键盘相关
    virtual void setKeypadEnabled(bool value);

    void registerScriptKeypadHandler(int nHandler);        //脚本键盘处理相关
    void unregisterScriptKeypadHandler(void);

    virtual void keyBackClicked(void);                 //返回键和菜单键处理
    virtual void keyMenuClicked(void);
    
    inline CCTouchScriptHandlerEntry* getScriptTouchHandlerEntry() { return m_pScriptTouchHandlerEntry; };    //相关脚本句柄获取
    inline CCScriptHandlerEntry* getScriptKeypadHandlerEntry() { return m_pScriptKeypadHandlerEntry; };
    inline CCScriptHandlerEntry* getScriptAccelerateHandlerEntry() { return m_pScriptAccelerateHandlerEntry; };
protected:   
    bool m_bTouchEnabled;
    bool m_bAccelerometerEnabled;
    bool m_bKeypadEnabled;
    
private:
    CCTouchScriptHandlerEntry* m_pScriptTouchHandlerEntry;
    CCScriptHandlerEntry* m_pScriptKeypadHandlerEntry;
    CCScriptHandlerEntry* m_pScriptAccelerateHandlerEntry;
    
    int m_nTouchPriority;
    ccTouchesMode m_eTouchMode;
    
    int  excuteScriptTouchHandler(int nEventType, CCTouch *pTouch);       //执行脚本句柄
    int  excuteScriptTouchHandler(int nEventType, CCSet *pTouches);
};
2、CCApplicationProtocol:应用协议,根据不同平台选择,Windows、Linux等
class CC_DLL CCApplicationProtocol
{
public:

    virtual ~CCApplicationProtocol() {}
    virtual bool applicationDidFinishLaunching() = 0;

    virtual void applicationDidEnterBackground() = 0;

    virtual void applicationWillEnterForeground() = 0;

    virtual void setAnimationInterval(double interval) = 0;

    virtual ccLanguageType getCurrentLanguage() = 0;
    
    virtual TargetPlatform getTargetPlatform() = 0;
};
3、CCApplication:应用程序类,继承自CCApplicationProtocol

class CC_DLL CCApplication : public CCApplicationProtocol
{
public:
    CCApplication();
    virtual ~CCApplication();

    virtual int run();             //运行消息循环
    static CCApplication* sharedApplication();   //获取一个实例指针

    virtual void setAnimationInterval(double interval);
    virtual ccLanguageType getCurrentLanguage();
    
    virtual TargetPlatform getTargetPlatform();

    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);   //设置资源路径
    CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);

    void setStartupScriptFilename(const std::string& startupScriptFile);   //启动脚本相关
    const std::string& getStartupScriptFilename(void)
    {
        return m_startupScriptFilename;
    }

protected:
    HINSTANCE           m_hInstance;
    HACCEL              m_hAccelTable;
    LARGE_INTEGER       m_nAnimationInterval;
    std::string         m_resourceRootPath;
    std::string         m_startupScriptFilename;

    static CCApplication * sm_pSharedApplication;
};
4、#define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated) 


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。