当前位置:编程学习 > wap >>

cocos2d-x学习笔记-触屏事件详解

游戏跟视频最大的区别就是互动,玩家可以操控游戏中的角色,现在的移动设备几乎人手一台,基本上全部都是基于触屏操作的,今天就来学习一下cocos2d-x是怎么实现对触屏操作的处理的。
1.首先来了解一下相关的几个类、处理触屏事件时操作和执行的流程
CCTouch:它封装了触摸点,可以通过locationInView函数返回一个CCPoint。
CCTouchDelegate:它是触摸事件委托,就是系统捕捉到触摸事件后交由它或者它的子类处理,所以我们在处理触屏事件时,必须得继承它。它封装了下面这些处理触屏事件的函数:
[cpp] 
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 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); 
ccTouchesCancelled和ccTouchCancelled函数很少用,在接到系统中断通知,需要取消触摸事件的时候才会调用此方法。如:应用长时间无响应、当前view从window上移除、触摸的时候来电话了等。

CCTargetedTouchDelegate和CCStandardTouchDelegate是CCTouchDelegate的子类,类结构图如下:

CCStandardTouchDelegate用于处理多点触摸;CCTargetedTouchDelegate用于处理单点触摸。

CCTouchDispatcher:实现触摸事件分发,它封装了下面这两个函数,可以把CCStandardTouchDelegate和CCTargetedTouchDelegate添加到分发列表中:

[cpp] view plaincopy
void addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority); 
void addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches); 
CCTouchHandler:封装了CCTouchDelegate和其对应的优先级,优先级越高,分发的时候越容易获得事件处理权,CCStandardTouchHandler和CCTargetedTouchHandler是它的子类。

下面分析一下触屏事件处理和执行流程:
用户自定义类继承CCTouchDelegate,重写触屏事件处理函数和registerWithTouchDispatcher函数,在init或者onEnter函数中调用registerWithTouchDispatcher函数,如:

[cpp] 
void GameLayer::registerWithTouchDispatcher() 

    cocos2d::CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); 

把相应的CCTouchDelegate添加到CCTouchDispatcher的分发列表中。addTargetedDelegate函数会创建CCTouchDelegate对应的CCTouchHandler对象并添加到CCMutableArraym_pTargetedHandlers中,看源码:
[cpp
void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches) 
{    
    CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches); 
    if (! m_bLocked) 
    { 
        forceAddHandler(pHandler, m_pTargetedHandlers); 
    } 
    else 
    { 
        /**....*/ 
    } 

  
void CCTouchDispatcher::forceAddHandler(CCTouchHandler *pHandler, CCMutableArray *pArray) 

    unsigned int u = 0; 
  
    CCMutableArray::CCMutableArrayIterator iter; 
    for (iter = pArray->begin(); iter != pArray->end(); ++iter) 
    { 
        CCTouchHandler *h = *iter; 
         if (h) 
         { 
            if (h->getPriority() < pHandler->getPriority()) 
            { 
                ++u; 
            } 
  
            if (h->getDelegate() == pHandler->getDelegate()) 
            { 
                CCAssert(0, ""); 
                return; 
            } 
         } 
    } 
  
    pArray->insertObjectAtIndex(pHandler, u); 

事件分发时就是从m_pTargetedHandlers中取出CCXXXTouchHandler,然后调用delegate的:pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent);,执行的是CCTouchDispatcher的touches函数,考虑到篇幅问题,就不贴出具体代码了。该函数首先会先处理targeted 再处理standard,所以CCTargetedTouchDelegate比CCStandardTouchDelegate优先级高。那什么时候触发执行touches函数呢?CCTouchDispatcher继承了EGLTouchDelegate类,EGLTouchDelegate类源码:
[cpp] 
class CC_DLL EGLTouchDelegate 

public: 
    virtual void touchesBegan(CCSet* touches, CCEvent* pEvent) = 0; 
    virtual void touchesMoved(CCSet* touches, CCEvent* pEvent) = 0; 
    virtual void touchesEnded(CCSet* touches, CCEvent* pEvent) = 0; 
    virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent) = 0; 
  
    virtual ~EGLTouchDelegate() {} 
}; 

CCTouchDispatcher中实现了这四个函数,正是在这四个函数中调用了touches函数:
[cpp] 
void CCTouchDispatcher::touchesBegan(CCSet *touches, CCEvent *pEvent) 

    if (m_bDispatchEvents) 
    { 
        this->touches(touches, pEvent, CCTOUCHBEGAN); 
    } 

/**其他三个方法类似 **/ 

这几个触屏处理函数是由具体平台底层调用的,在AppDelegate.cpp中有这段代码:
[cpp] 
CCDirector *pDirector = CCDirector::sharedDirector(); 
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); 

继续跟进setOpenGLView函数,发现了这段代码:
[cpp] 
CCTouchDispatcher *pTouchDispatcher = CCTouchDispatcher::sharedDispatcher(); 
m_pobOpenGLView->setTouchDelegate(pTouchDispatcher); 
pTouchDispatcher->setDispatchEvents(true); 

调用了具体平台下的CCEGLView类中的setTouchDelegate函数。由于我是在windows平台下,所以CCEGLView此时对应CCEGLView_win32.h文件的CCEGLView类,对应的setTouchDelegate函数为:
[cpp] 
void    setTouchDelegate(EGLTouchDelegate * pDelegate); 

系统最终通过CCEGLView类的WindowProc函数处理鼠标在Windows窗口的DOWN、MOVE、UP事件,通过pDelegate分别调用touchesBegan、touchesMoved、touchesEnded函数。
[cpp] 
LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 

    switch (message) 
    { 
    case WM_LBUTTONDOWN: 
        if (m_pDelegate && m_pTouch && MK_LBUTTON == wParam) 
  

补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,