分析Cocos2d-x横版ACT手游源代码 2、服务器场景

前端之家收集整理的这篇文章主要介绍了分析Cocos2d-x横版ACT手游源代码 2、服务器场景前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
还是一样 直接上代码代码  下一篇  上传  大家可以自己看
NFServerChangeLayer.h
</pre><pre name="code" class="cpp">/************************************************************************/
/*			服务器切换层                                                                    */
/************************************************************************/
class CNFServerChangeLayer : public CCLayer,public CMsgReceiver,public CMsgSender
{
protected:
	//标签
	enum 
	{
		enTagScrollLayer = 1,};

	int m_nCurrentServerID;

public:
	static CNFServerChangeLayer * CreateLayer(int nCurrentServerID,int nServerTotalNum);	//当前服务器ID,服务器总数量

	//消息接收
	virtual void RecMsg(int nMsgID,void* pInfo,int nSize);

protected:
	virtual bool InitLayer(int nCurrentServerID,int nServerTotalNum);     

	//按钮回调:返回
	void OnBtnCallBack(CCObject * pObj);
	//按钮回调:向左滑动
	void OnBtnLeftMove(CCObject * pObj);
	//按钮回调:向右滑动
	void OnBtnRightMove(CCObject * pObj);
};


#endif
<span style="white-space:pre">	</span>.cpp文件
#include "NFServerChangeLayer.h"
#include "NFServerChangeScrollLayer.h"


CNFServerChangeLayer * CNFServerChangeLayer::CreateLayer(int nCurrentServerID,int nServerTotalNum)
{
<span style="white-space:pre">	</span>CNFServerChangeLayer *pRet = new CNFServerChangeLayer();
<span style="white-space:pre">	</span>if (pRet && pRet->InitLayer(nCurrentServerID,nServerTotalNum))
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>pRet->autorelease();
<span style="white-space:pre">		</span>return pRet;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>CCLog("Fun CNFServerChangeLayer::CreateLayer Error!");
<span style="white-space:pre">	</span>delete pRet;
<span style="white-space:pre">	</span>pRet = NULL;
<span style="white-space:pre">	</span>return NULL;
}


bool CNFServerChangeLayer::InitLayer(int nCurrentServerID,int nServerTotalNum)
{
<span style="white-space:pre">	</span>do
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//初始化父类
<span style="white-space:pre">		</span>CC_BREAK_IF(CCLayer::init()==false);


<span style="white-space:pre">		</span>m_nCurrentServerID = nCurrentServerID;


<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>/*<span style="white-space:pre">			</span>背景图片                                   */
<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>//创建背景
<span style="white-space:pre">		</span>CCSprite * pBg = CCSprite::create("ui/serverselect_bg.png");
<span style="white-space:pre">		</span>CC_BREAK_IF(pBg==NULL);
<span style="white-space:pre">		</span>pBg->setPosition(SCREEN_CENTER);
<span style="white-space:pre">		</span>pBg->setScaleX( (float)CCDirector::sharedDirector()->getWinSize().width/(float)pBg->getContentSize().width );
<span style="white-space:pre">		</span>addChild(pBg,enZOrderBack);


<span style="white-space:pre">		</span>//创建上次登录图片
<span style="white-space:pre">		</span>CCSprite * pBg1 = CCSprite::create("ui/serverselect_title_1.png");
<span style="white-space:pre">		</span>CC_BREAK_IF(pBg1==NULL);
<span style="white-space:pre">		</span>pBg1->setPosition(ccp(150+_NF_SCREEN_WIDTH_DIS_,420));
<span style="white-space:pre">		</span>addChild(pBg1,enZOrderBack);


<span style="white-space:pre">		</span>//创建全服务器图片
<span style="white-space:pre">		</span>CCSprite * pBg2 = CCSprite::create("ui/serverselect_title_2.png");
<span style="white-space:pre">		</span>CC_BREAK_IF(pBg2==NULL);
<span style="white-space:pre">		</span>pBg2->setPosition(ccp(160+_NF_SCREEN_WIDTH_DIS_,300));
<span style="white-space:pre">		</span>addChild(pBg2,enZOrderBack);


<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>/*<span style="white-space:pre">			</span>创建按钮                                   */
<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>CCMenu * pMenu = CCMenu::create();
<span style="white-space:pre">		</span>CC_BREAK_IF(pMenu==NULL);
<span style="white-space:pre">		</span>pMenu->setPosition(CCPointZero);
<span style="white-space:pre">		</span>addChild(pMenu,enZOrderFront);


<span style="white-space:pre">		</span>//创建返回按钮
<span style="white-space:pre">		</span>//CCMenuItemSprite * pBtnBack = CCMenuItemSprite::create(
<span style="white-space:pre">		</span>//<span style="white-space:pre">	</span>CCSprite::create("ui/serverselect_btn_back_n.png"),<span style="white-space:pre">		</span>//<span style="white-space:pre">	</span>CCSprite::create("ui/serverselect_btn_back_p.png"),<span style="white-space:pre">		</span>//<span style="white-space:pre">	</span>this,<span style="white-space:pre">		</span>//<span style="white-space:pre">	</span>menu_selector(CNFServerChangeLayer::OnBtnCallBack));
<span style="white-space:pre">		</span>//CC_BREAK_IF(pBtnBack==NULL);
<span style="white-space:pre">		</span>//pBtnBack->setPosition(ccp(SCREEN_WIDTH - 30,SCREEN_HEIGHT-30));
<span style="white-space:pre">		</span>//pMenu->addChild(pBtnBack,enZOrderFront);


<span style="white-space:pre">		</span>//创建当前服务器按钮
<span style="white-space:pre">		</span>CCMenuItemSprite * pBtnCurrentServer = CCMenuItemSprite::create(
<span style="white-space:pre">			</span>CCSprite::create("ui/btn_enter_n.png"),<span style="white-space:pre">			</span>CCSprite::create("ui/btn_enter_p.png"),<span style="white-space:pre">			</span>this,<span style="white-space:pre">			</span>menu_selector(CNFServerChangeLayer::OnBtnCallBack));
<span style="white-space:pre">		</span>CC_BREAK_IF(pBtnCurrentServer==NULL);
<span style="white-space:pre">		</span>pBtnCurrentServer->setPosition(ccp(190+_NF_SCREEN_WIDTH_DIS_,380));
<span style="white-space:pre">		</span>pMenu->addChild(pBtnCurrentServer,enZOrderFront);


<span style="white-space:pre">		</span>//创建左侧按钮
<span style="white-space:pre">		</span>CCMenuItemSprite * pBtnLeft = CCMenuItemSprite::create(
<span style="white-space:pre">			</span>CCSprite::create("ui/serverselect_btn_back_n.png"),<span style="white-space:pre">			</span>CCSprite::create("ui/serverselect_btn_back_p.png"),<span style="white-space:pre">			</span>menu_selector(CNFServerChangeLayer::OnBtnLeftMove));
<span style="white-space:pre">		</span>CC_BREAK_IF(pBtnLeft==NULL);
<span style="white-space:pre">		</span>pBtnLeft->setPosition(ccp(50+_NF_SCREEN_WIDTH_DIS_,150));
<span style="white-space:pre">		</span>pMenu->addChild(pBtnLeft,enZOrderFront);


<span style="white-space:pre">		</span>//创建右侧按钮
<span style="white-space:pre">		</span>CCMenuItemSprite * pBtnRight = CCMenuItemSprite::create(
<span style="white-space:pre">			</span>CCSprite::create("ui/serverselect_btn_back_n.png"),<span style="white-space:pre">			</span>menu_selector(CNFServerChangeLayer::OnBtnRightMove));
<span style="white-space:pre">		</span>CC_BREAK_IF(pBtnRight==NULL);
<span style="white-space:pre">		</span>pBtnRight->setPosition(ccp(SCREEN_WIDTH-_NF_SCREEN_WIDTH_DIS_-50,150));
<span style="white-space:pre">		</span>pMenu->addChild(pBtnRight,enZOrderFront);


<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>/*<span style="white-space:pre">				</span>创建label                                   */
<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>CCDictionary *pDicLang = CCDictionary::createWithContentsOfFile("ui_xml/serverselect_xml.xml");
<span style="white-space:pre">		</span>CC_BREAK_IF(pDicLang==NULL);


<span style="white-space:pre">		</span>//服务器名称label
<span style="white-space:pre">		</span>CCString *pStrServerName = dynamic_cast<CCString*>(pDicLang->objectForKey("server_name"));
<span style="white-space:pre">		</span>char szName[NAME_LEN] = {0};
<span style="white-space:pre">		</span>sprintf(szName,pStrServerName->m_sString.c_str(),nCurrentServerID);
<span style="white-space:pre">		</span>CCLabelTTF * pLabelServerName = CCLabelTTF::create(szName,"Arial",20);
<span style="white-space:pre">		</span>CC_BREAK_IF(pLabelServerName==NULL);
<span style="white-space:pre">		</span>pLabelServerName->setPosition(pBtnCurrentServer->getPosition());
<span style="white-space:pre">		</span>addChild(pLabelServerName,enZOrderFront);




<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>/*<span style="white-space:pre">					</span>创建滑动层                                   */
<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>//创建滑动层
<span style="white-space:pre">		</span>CNFServerChangeScrollLayer * pLayer = CNFServerChangeScrollLayer::CreateLayer(nServerTotalNum);<span style="white-space:pre">	</span>//Item数组,x轴起始位置
<span style="white-space:pre">		</span>CC_BREAK_IF(pLayer==NULL);
<span style="white-space:pre">		</span>pLayer->SetMsg(this);
<span style="white-space:pre">		</span>addChild(pLayer,enZOrderFront,enTagScrollLayer);




<span style="white-space:pre">		</span>return true;
<span style="white-space:pre">	</span>} while (false);
<span style="white-space:pre">	</span>CCLog("Fun CNFServerChangeLayer::init Error!");
<span style="white-space:pre">	</span>return false;
}


//按钮回调:返回
void CNFServerChangeLayer::OnBtnCallBack( CCObject * pObj )
{
<span style="white-space:pre">	</span>//发消息:更改服务器
<span style="white-space:pre">	</span>tagServerSelect Info;
<span style="white-space:pre">	</span>Info.nServerID = m_nCurrentServerID;
<span style="white-space:pre">	</span>SendMsg(enMsg_ServerSelect,&Info,sizeof(Info));


<span style="white-space:pre">	</span>//移除自身
<span style="white-space:pre">	</span>removeFromParentAndCleanup(true);
}


//按钮回调:向左滑动
void CNFServerChangeLayer::OnBtnLeftMove( CCObject * pObj )
{
<span style="white-space:pre">	</span>do
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//滑动层
<span style="white-space:pre">		</span>CNFServerChangeScrollLayer * pLayer = dynamic_cast<CNFServerChangeScrollLayer *>(getChildByTag(enTagScrollLayer));
<span style="white-space:pre">		</span>CC_BREAK_IF(pLayer==NULL);
<span style="white-space:pre">		</span>pLayer->SetRightMove();


<span style="white-space:pre">		</span>return ;
<span style="white-space:pre">	</span>} while (false);
<span style="white-space:pre">	</span>CCLog("Fun CNFServerChangeLayer::OnBtnLeftMove Error!");
}


//按钮回调:向右滑动
void CNFServerChangeLayer::OnBtnRightMove( CCObject * pObj )
{
<span style="white-space:pre">	</span>do
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//滑动层
<span style="white-space:pre">		</span>CNFServerChangeScrollLayer * pLayer = dynamic_cast<CNFServerChangeScrollLayer *>(getChildByTag(enTagScrollLayer));
<span style="white-space:pre">		</span>CC_BREAK_IF(pLayer==NULL);
<span style="white-space:pre">		</span>pLayer->SetLeftMove();


<span style="white-space:pre">		</span>return ;
<span style="white-space:pre">	</span>} while (false);
<span style="white-space:pre">	</span>CCLog("Fun CNFServerChangeLayer::OnBtnRightMove Error!");
}




//消息接收
void CNFServerChangeLayer::RecMsg( int nMsgID,int nSize )
{
<span style="white-space:pre">	</span>switch (nMsgID)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>case enMsg_ServerSelect:
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>tagServerSelect * pCmd = (tagServerSelect *)pInfo;


<span style="white-space:pre">			</span>//发消息:更改服务器
<span style="white-space:pre">			</span>tagServerSelect Info;
<span style="white-space:pre">			</span>Info.nServerID = pCmd->nServerID;


<span style="white-space:pre">			</span>SendMsg(enMsg_ServerSelect,sizeof(Info));


<span style="white-space:pre">			</span>//移除自身
<span style="white-space:pre">			</span>removeFromParentAndCleanup(true);


<span style="white-space:pre">		</span>}break;
<span style="white-space:pre">	</span>}
}

NFServerChangeScrollLayer.h  文件
/************************************************************************/
/*<span style="white-space:pre">				</span>服务器选择界面,滑动层                                   */
/************************************************************************/
class CNFServerChangeScrollLayer : public CCLayerColor,public CMsgSender
{
protected:
<span style="white-space:pre">	</span>// 标签
<span style="white-space:pre">	</span>enum
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>enTagMenu = 1,<span style="white-space:pre">		</span>enTagBtn = 100,<span style="white-space:pre">	</span>};


<span style="white-space:pre">	</span>float<span style="white-space:pre">			</span>m_fTouchPassPosX;<span style="white-space:pre">			</span>//上一次计算触摸时,手指滑过的位置
<span style="white-space:pre">	</span>float<span style="white-space:pre">			</span>m_fTouchBeginPosX;<span style="white-space:pre">			</span>//用户触摸最初始的位置
<span style="white-space:pre">	</span>bool<span style="white-space:pre">			</span>m_bTouching;<span style="white-space:pre">				</span>//是否取消了触摸(保证不会重复调用TouchEnd)
<span style="white-space:pre">	</span>bool<span style="white-space:pre">			</span>m_bIsTouchBtn;<span style="white-space:pre">				</span>//是否触摸到按钮


<span style="white-space:pre">	</span>float<span style="white-space:pre">			</span>m_fPageWidth;<span style="white-space:pre">				</span>//每一页的宽度
<span style="white-space:pre">	</span>float<span style="white-space:pre">			</span>m_fEndLayerPos;<span style="white-space:pre">				</span>//结束边界的位置
<span style="white-space:pre">	</span>float<span style="white-space:pre">			</span>m_fStartNodePosX;<span style="white-space:pre">			</span>//初始节点x轴位置


<span style="white-space:pre">	</span>CCRect<span style="white-space:pre">			</span>m_DisPlayRect;<span style="white-space:pre">				</span>//显示区域


public:
<span style="white-space:pre">	</span>static CNFServerChangeScrollLayer* CreateLayer(int nItemNum);


<span style="white-space:pre">	</span>//向左滑
<span style="white-space:pre">	</span>void SetLeftMove();


<span style="white-space:pre">	</span>//向右滑
<span style="white-space:pre">	</span>void SetRightMove();


protected:
<span style="white-space:pre">	</span>bool InitLayer(int nItemNum);


<span style="white-space:pre">	</span>//设置裁剪区域
<span style="white-space:pre">	</span>void visit();


<span style="white-space:pre">	</span>//触摸函数
<span style="white-space:pre">	</span>virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
<span style="white-space:pre">	</span>virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
<span style="white-space:pre">	</span>virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
<span style="white-space:pre">	</span>virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);




<span style="white-space:pre">	</span>//按钮回调
<span style="white-space:pre">	</span>void OnBtnCallBack(CCObject * pObj);


<span style="white-space:pre">	</span>virtual void onExit();


};




#endif

#include "NFServerChangeScrollLayer.h"


#define _ONE_PAGE_SERVER_NUM_<span style="white-space:pre">	</span>12<span style="white-space:pre">		</span>//每一页所拥有的服务器的个数
#define _X_START_POS_<span style="white-space:pre">	</span>78<span style="white-space:pre">				</span>//x轴初始坐标
#define _PAGE_WIDTH_<span style="white-space:pre">	</span>564<span style="white-space:pre">				</span>//页面宽度
#define _PAGE_HEIGHT_<span style="white-space:pre">	</span>285<span style="white-space:pre">				</span>//页面高度


/************************************************************************/
/*<span style="white-space:pre">				</span>滑动层                         */
/************************************************************************/
CNFServerChangeScrollLayer* CNFServerChangeScrollLayer::CreateLayer(int nItemNum)
{<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>CNFServerChangeScrollLayer *pRet = new CNFServerChangeScrollLayer();
<span style="white-space:pre">	</span>if (pRet && pRet->InitLayer(nItemNum))
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>pRet->autorelease();
<span style="white-space:pre">		</span>return pRet;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>CC_SAFE_DELETE(pRet);
<span style="white-space:pre">	</span>return NULL;
}


bool CNFServerChangeScrollLayer::InitLayer(int nItemNum)
{<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>do
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//初始化父类
<span style="white-space:pre">		</span>CC_BREAK_IF(CCLayerColor::initWithColor(ccc4(0,0))==false);


<span style="white-space:pre">		</span>//独占触屏注册值小于-128才能屏蔽Menu(Menu默认是-128)
<span style="white-space:pre">		</span>CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-129,true);


<span style="white-space:pre">		</span>//初始x轴坐标
<span style="white-space:pre">		</span>m_fStartNodePosX = _X_START_POS_ + _NF_SCREEN_WIDTH_DIS_;


;<span style="white-space:pre">		</span>//页面数量
<span style="white-space:pre">		</span>int nPageNum = nItemNum%_ONE_PAGE_SERVER_NUM_ > 0 ? nItemNum/_ONE_PAGE_SERVER_NUM_+1 : nItemNum/_ONE_PAGE_SERVER_NUM_;


<span style="white-space:pre">		</span>//每一页宽度
<span style="white-space:pre">		</span>m_fPageWidth = _PAGE_WIDTH_;


<span style="white-space:pre">		</span>//页面高度
<span style="white-space:pre">		</span>float<span style="white-space:pre">	</span>fPageHeight = _PAGE_HEIGHT_;


<span style="white-space:pre">		</span>//x轴最小坐标
<span style="white-space:pre">		</span>m_fEndLayerPos = m_fPageWidth*(1-nPageNum);


<span style="white-space:pre">		</span>//x,y轴之间的间隔
<span style="white-space:pre">		</span>float fItemDisX = 11;
<span style="white-space:pre">		</span>float fItemDisY = 20;




<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>/*<span style="white-space:pre">			</span>创建按钮                                   */
<span style="white-space:pre">		</span>/************************************************************************/
<span style="white-space:pre">		</span>CCDictionary *pDicLang = CCDictionary::createWithContentsOfFile("ui_xml/serverselect_xml.xml");
<span style="white-space:pre">		</span>CC_BREAK_IF(pDicLang==NULL);


<span style="white-space:pre">		</span>CCMenu * pMenu = CCMenu::create();
<span style="white-space:pre">		</span>CC_BREAK_IF(pMenu==NULL);
<span style="white-space:pre">		</span>pMenu->setPosition(CCPointZero);
<span style="white-space:pre">		</span>addChild(pMenu,enTagMenu);


<span style="white-space:pre">		</span>//创建数组
<span style="white-space:pre">		</span>CCObject *pObj = NULL;
<span style="white-space:pre">		</span>for (int k=1;k<=nItemNum;k++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>int i = k%_ONE_PAGE_SERVER_NUM_ > 0 ? k%_ONE_PAGE_SERVER_NUM_ : _ONE_PAGE_SERVER_NUM_;<span style="white-space:pre">			</span>//页面内ITem的ID:1~12
<span style="white-space:pre">			</span>int j = k%_ONE_PAGE_SERVER_NUM_ > 0 ? k/_ONE_PAGE_SERVER_NUM_+1 : k/_ONE_PAGE_SERVER_NUM_;<span style="white-space:pre">		</span>//当前页面ID


<span style="white-space:pre">			</span>int nCrossNum = i%3 == 0 ? 3 : i%3;<span style="white-space:pre">				</span>//横排
<span style="white-space:pre">			</span>int nVerticalNum = i%3 > 0 ? i/3+1 : i/3;<span style="white-space:pre">		</span>//竖排


<span style="white-space:pre">			</span>//创建按钮
<span style="white-space:pre">			</span>CCMenuItemSprite * pBtn = CCMenuItemSprite::create(
<span style="white-space:pre">				</span>CCSprite::create("ui/btn_enter_n.png"),<span style="white-space:pre">				</span>CCSprite::create("ui/btn_enter_p.png"),<span style="white-space:pre">				</span>this,<span style="white-space:pre">				</span>menu_selector(CNFServerChangeScrollLayer::OnBtnCallBack));
<span style="white-space:pre">			</span>CC_BREAK_IF(pBtn==NULL);
<span style="white-space:pre">			</span>pBtn->setPosition(ccp( fItemDisX+ (nCrossNum-1)*(pBtn->getContentSize().width+fItemDisX) + m_fStartNodePosX + pBtn->getContentSize().width*0.5f + (j-1)*m_fPageWidth,fPageHeight-nVerticalNum*(pBtn->getContentSize().height+fItemDisY) + fItemDisY ));
<span style="white-space:pre">			</span>pMenu->addChild(pBtn,enTagBtn+k);


<span style="white-space:pre">			</span>//服务器名称label
<span style="white-space:pre">			</span>CCString *pStrServerName = dynamic_cast<CCString*>(pDicLang->objectForKey("server_name"));
<span style="white-space:pre">			</span>char szName[NAME_LEN] = {0};
<span style="white-space:pre">			</span>sprintf(szName,k);
<span style="white-space:pre">			</span>CCLabelTTF * pLabelServerName = CCLabelTTF::create(szName,20);
<span style="white-space:pre">			</span>CC_BREAK_IF(pLabelServerName==NULL);
<span style="white-space:pre">			</span>pLabelServerName->setPosition(pBtn->getPosition());
<span style="white-space:pre">			</span>addChild(pLabelServerName,enZOrderFront);
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>}




<span style="white-space:pre">		</span>//显示区域
<span style="white-space:pre">		</span>m_DisPlayRect = CCRectMake(m_fStartNodePosX,10,m_fPageWidth,fPageHeight);


<span style="white-space:pre">		</span>m_bTouching = false;
<span style="white-space:pre">		</span>m_bIsTouchBtn = false;


<span style="white-space:pre">		</span>return true;
<span style="white-space:pre">	</span>} while (false);
<span style="white-space:pre">	</span>CCLog("Fun CQXPVEItemScrollLayer::initWithNodes Error!");
<span style="white-space:pre">	</span>return false;


}


//触摸:开始
bool CNFServerChangeScrollLayer::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
<span style="white-space:pre">	</span>CCPoint ptTouchPoint =pTouch->getLocation();


<span style="white-space:pre">	</span>//设置初始触摸点
<span style="white-space:pre">	</span>m_fTouchPassPosX = ptTouchPoint.x;
<span style="white-space:pre">	</span>m_fTouchBeginPosX = ptTouchPoint.x;


<span style="white-space:pre">	</span>//判断是否触摸到裁切区域内
<span style="white-space:pre">	</span>if(m_DisPlayRect.containsPoint(ptTouchPoint))
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_bTouching = true;


<span style="white-space:pre">		</span>//判断是否触摸到按钮
<span style="white-space:pre">		</span>CCMenu *pMenu = dynamic_cast<CCMenu*>(getChildByTag(enTagMenu));
<span style="white-space:pre">		</span>if(pMenu!=NULL)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>CCArray *pChildren = pMenu->getChildren();
<span style="white-space:pre">			</span>if(pChildren!=NULL)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>CCObject *pItem=NULL;
<span style="white-space:pre">				</span>CCARRAY_FOREACH(pChildren,pItem)
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>CCMenuItemSprite *pBtn= dynamic_cast<CCMenuItemSprite*>(pItem);
<span style="white-space:pre">					</span>if(pBtn!=NULL)
<span style="white-space:pre">					</span>{
<span style="white-space:pre">						</span>CCPoint local = pBtn->convertToNodeSpace(pTouch->getLocation());
<span style="white-space:pre">						</span>CCRect r = pBtn->rect();
<span style="white-space:pre">						</span>r.origin = CCPointZero;
<span style="white-space:pre">						</span>if (r.containsPoint(local))
<span style="white-space:pre">						</span>{
<span style="white-space:pre">							</span>pMenu->ccTouchBegan(pTouch,pEvent);
<span style="white-space:pre">							</span>m_bIsTouchBtn=true;
<span style="white-space:pre">						</span>}
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}




<span style="white-space:pre">		</span>return true;<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>return false;
}


//触摸:移动
void CNFServerChangeScrollLayer::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)
{<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>if(m_bTouching==false)return;


<span style="white-space:pre">	</span>CCPoint ptTouchPoint =pTouch->getLocation();
<span style="white-space:pre">	</span>CCPoint pNewPos=getPosition();


<span style="white-space:pre">	</span>//移动层
<span style="white-space:pre">	</span>pNewPos.x+=ptTouchPoint.x-m_fTouchPassPosX;
<span style="white-space:pre">	</span>this->setPosition(pNewPos);


<span style="white-space:pre">	</span>//重置初始点
<span style="white-space:pre">	</span>m_fTouchPassPosX=ptTouchPoint.x;


<span style="white-space:pre">	</span>//若触摸到按钮
<span style="white-space:pre">	</span>if (m_bIsTouchBtn==true)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>CCMenu *pMenu = dynamic_cast<CCMenu*>(getChildByTag(enTagMenu));
<span style="white-space:pre">		</span>if (pMenu!=NULL)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>pMenu->ccTouchCancelled(pTouch,pEvent);
<span style="white-space:pre">			</span>m_bIsTouchBtn = false;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}


//触摸:结束
void CNFServerChangeScrollLayer::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
<span style="white-space:pre">	</span>if(m_bTouching==false)return;


<span style="white-space:pre">	</span>m_bTouching = false;


<span style="white-space:pre">	</span>CCPoint ptTouchPoint =pTouch->getLocation();
<span style="white-space:pre">	</span>CCPoint ptMyPos = getPosition();




<span style="white-space:pre">	</span>//判断移到哪一页
<span style="white-space:pre">	</span>float fDisX = ptTouchPoint.x - m_fTouchBeginPosX;
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//判定滑动
<span style="white-space:pre">	</span>if ( _NF_ABS(fDisX)>= 20)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//向左滑动
<span style="white-space:pre">		</span>if (fDisX < 0)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>//若未过界
<span style="white-space:pre">			</span>if (getPositionX() > m_fEndLayerPos)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>int fLen = (int)getPositionX()%(int)m_fPageWidth;
<span style="white-space:pre">				</span>ptMyPos.x = (int)( getPositionX() - fLen - m_fPageWidth);
<span style="white-space:pre">				</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">				</span>runAction(pMoveTo);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>//向右滑
<span style="white-space:pre">		</span>if (fDisX > 0)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>//若未过界
<span style="white-space:pre">			</span>if (getPositionX() < 0)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>int fLen = (int)getPositionX()%(int)m_fPageWidth;
<span style="white-space:pre">				</span>ptMyPos.x = (int)( getPositionX() - fLen);
<span style="white-space:pre">				</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">				</span>runAction(pMoveTo);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>//判断是否越界
<span style="white-space:pre">	</span>if (getPositionX()>0)
<span style="white-space:pre">	</span>{<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>ptMyPos.x = 0;
<span style="white-space:pre">		</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">		</span>runAction(pMoveTo);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else if (getPositionX()<m_fEndLayerPos)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>ptMyPos.x = m_fEndLayerPos<0?m_fEndLayerPos:0;
<span style="white-space:pre">		</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">		</span>runAction(pMoveTo);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>//滑动层,当前坐标
<span style="white-space:pre">	</span>CCPoint LayerPos;
<span style="white-space:pre">	</span>LayerPos.x = getPositionX();
<span style="white-space:pre">	</span>LayerPos.y = getPositionY();


<span style="white-space:pre">	</span>//若触摸到按钮
<span style="white-space:pre">	</span>if (m_bIsTouchBtn==true)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>CCMenu *pMenu = dynamic_cast<CCMenu*>(getChildByTag(enTagMenu));
<span style="white-space:pre">		</span>if (pMenu!=NULL)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>pMenu->ccTouchEnded(pTouch,pEvent);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>m_bIsTouchBtn = false;
<span style="white-space:pre">	</span>}


}


void CNFServerChangeScrollLayer::ccTouchCancelled( CCTouch *pTouch,CCEvent *pEvent )
{
<span style="white-space:pre">	</span>m_bTouching = false;
}


//裁切
void CNFServerChangeScrollLayer::visit()
{
<span style="white-space:pre">	</span>kmGLPushMatrix();


<span style="white-space:pre">	</span>if (m_pGrid && m_pGrid->isActive())
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_pGrid->beforeDraw();
<span style="white-space:pre">		</span>this->transformAncestors();
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>this->transform();


<span style="white-space:pre">	</span>glEnable(GL_SCISSOR_TEST);


<span style="white-space:pre">	</span>CCSize size=CCEGLView::sharedOpenGLView()->getFrameSize();


<span style="white-space:pre">	</span>float fScaleX=size.width/SCREEN_WIDTH;
<span style="white-space:pre">	</span>float fScaleY=size.height/SCREEN_HEIGHT;


<span style="white-space:pre">	</span>glScissor(m_DisPlayRect.origin.x*fScaleX,m_DisPlayRect.origin.y*fScaleY,m_DisPlayRect.size.width*fScaleX,m_DisPlayRect.size.height*fScaleY);


<span style="white-space:pre">	</span>if (m_pChildren)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for (unsigned int i=0;i<m_pChildren->data->num;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>CCNode* pNode=(CCNode*)m_pChildren->data->arr[i];
<span style="white-space:pre">			</span>if (pNode->getZOrder()<0)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>pNode->visit();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>else
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>break;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>draw();
<span style="white-space:pre">		</span>for (unsigned int i=0;i<m_pChildren->data->num;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>CCNode* pNode=(CCNode*)m_pChildren->data->arr[i];
<span style="white-space:pre">			</span>pNode->visit();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>draw();
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>glDisable(GL_SCISSOR_TEST);
<span style="white-space:pre">	</span>if (m_pGrid &&m_pGrid->isActive())
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_pGrid->afterDraw(this);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>kmGLPopMatrix();
}


//按钮回调
void CNFServerChangeScrollLayer::OnBtnCallBack( CCObject * pObj )
{
<span style="white-space:pre">	</span>do
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>int nTag = (dynamic_cast<CCMenuItemSprite *>(pObj))->getTag();


<span style="white-space:pre">		</span>//发消息:更改服务器
<span style="white-space:pre">		</span>tagServerSelect Info;
<span style="white-space:pre">		</span>Info.nServerID = nTag-100;


<span style="white-space:pre">		</span>//发消息,切换服务器
<span style="white-space:pre">		</span>SendMsg(enMsg_ServerSelect,sizeof(Info));


<span style="white-space:pre">		</span>//移除自身
<span style="white-space:pre">		</span>removeFromParentAndCleanup(true);
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return ;
<span style="white-space:pre">	</span>} while (false);
<span style="white-space:pre">	</span>CCLog("Fun CNFServerSelectScrollLayer::OnBtnCallBack Error!");
}


void CNFServerChangeScrollLayer::onExit()
{
<span style="white-space:pre">	</span>//取消独占
<span style="white-space:pre">	</span>CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);


<span style="white-space:pre">	</span>CCLayer::onExit();
}


void CNFServerChangeScrollLayer::SetLeftMove()
{
<span style="white-space:pre">	</span>//若当前未触摸
<span style="white-space:pre">	</span>if (m_bTouching==false)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//若未过界
<span style="white-space:pre">		</span>if (getPositionX() > m_fEndLayerPos)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>CCPoint ptMyPos;
<span style="white-space:pre">			</span>ptMyPos.x = (int)( getPositionX() - m_fPageWidth);
<span style="white-space:pre">			</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">			</span>runAction(pMoveTo);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}


void CNFServerChangeScrollLayer::SetRightMove()
{
<span style="white-space:pre">	</span>//若当前未触摸
<span style="white-space:pre">	</span>if (m_bTouching==false)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//若未过界
<span style="white-space:pre">		</span>if (getPositionX() < 0)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>CCPoint ptMyPos;
<span style="white-space:pre">			</span>ptMyPos.x = (int)( getPositionX() + m_fPageWidth);
<span style="white-space:pre">			</span>CCAction *pMoveTo=CCMoveTo::create(0.3f,ptMyPos);
<span style="white-space:pre">			</span>runAction(pMoveTo);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}
原文链接:https://www.f2er.com/cocos2dx/346066.html

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