废话不说,直接上代码:
头文件
class Player : public CCObject{public: Player(); ~Player(); voID updatePkWeiZhi();//设置牌的位置 private: CC_SYNTHESIZE(bool,m_isDiZhu,IsDiZhu);//是否为地主 CC_SYNTHESIZE(bool,m_isCall,Call);//是否已叫地主 CC_SYNTHESIZE(int,m_iCallNum,CallNum);//叫地主的分数 CC_SYNTHESIZE(CCArray*,m_arrPk,ArrPk);//手里拥有的扑克牌 CC_SYNTHESIZE(CCPoint,m_point,Point);//牌在桌面的初始位置 CC_SYNTHESIZE(int,m_iPlayerClass,PlayerClass);//玩家种类:0为玩家,1为电脑,2为显示的三张牌,3为玩家要出的牌,4为电脑1要出的牌,5为电脑2要出的牌 std::vector<PaiXing> m_vecPX;//保存牌型 CC_SYNTHESIZE(bool,m_isOutPk,IsOutPk);//玩家是否出牌true:出 false:不出};
源文件
voID Player::updatePkWeiZhi(){ CCSize size = CCDirector::sharedDirector()->getVisibleSize(); int x,y;
<span > </span>//计算玩家牌和出的牌的初始位置 if(m_iPlayerClass == 0 || m_iPlayerClass == 3) { x = size.wIDth/2-((m_arrPk->count()-1)*pkJianJu+pkWIDth)/2; y = m_point.y; } else if(m_iPlayerClass == 1 || m_iPlayerClass == 4 || m_iPlayerClass == 5) { x = m_point.x; y = m_point.y; } else if(m_iPlayerClass == 2) { x = size.wIDth/2-(m_arrPk->count()*pkWIDth+(m_arrPk->count()-1)*pkJianJu)/2; y = m_point.y; } int num = 0; CCObject* object; //对牌进行排序 if(m_iPlayerClass != 3 && m_iPlayerClass != 4 && m_iPlayerClass != 5) for(int i=0; m_arrPk->count()!=0 && i<m_arrPk->count()-1; ++i) { for(int j=0; j<m_arrPk->count()-1-i; ++j) { Poker* pk1 = (Poker*)m_arrPk->objectAtIndex(j); Poker* pk2 = (Poker*)m_arrPk->objectAtIndex(j+1); if(pk1->getNum() < pk2->getNum()) m_arrPk->exchangeObject(pk1,pk2); } } //更新位置 CCARRAY_FOREACH(m_arrPk,object){ Poker* pk = (Poker*)object; if (m_iPlayerClass == 0 || m_iPlayerClass == 3) { pk->showFront(); pk->setposition(ccp(x+num*pkJianJu+pkWIDth/2,y)); } else if(m_iPlayerClass == 1 || m_iPlayerClass == 4 || m_iPlayerClass == 5) { pk->showFront(); if(m_iPlayerClass == 1) pk->showLast(); pk->setposition(ccp(x,y-num*pkJianJu)); } else if(m_iPlayerClass == 2) { pk->setposition(ccp(x+num*pkJianJu+num*pkWIDth+pkWIDth/2,y)); } ++num; } //改变牌的z值或牌的触摸优先 int i=m_arrPk->count()-1; CCARRAY_FOREACH(m_arrPk,object){ Poker* pk = (Poker*)object; //改变z值 if (m_iPlayerClass == 1 || m_iPlayerClass == 4 || m_iPlayerClass == 5) pk->setZOrder(size.height - pk->getpositionY()); if (m_iPlayerClass == 0 || m_iPlayerClass == 3) pk->setZOrder(pk->getpositionX()); //改变优先级 /* Poker* pk1 = (Poker *)m_arrPk->objectAtIndex(i--); pk->settouchPriority(pk1->getpositionX());*/ }}
这个类最主要的是 updatePkWeiZhi(),它是为了把玩家手中的牌从大到小排序并居中显示。请注意最后一块代码“改变牌的z值或牌的触摸优先级”,为什么要这么做呢?这是因为牌的顺序换了之后,底下的牌有的会覆盖到另一个牌之上,改变触摸优先级也是同理。
源码请前往前三章下载!
总结以上是内存溢出为你收集整理的cocos2dx《单机斗地主》源码解剖之四 玩家类的解剖全部内容,希望文章能够帮你解决cocos2dx《单机斗地主》源码解剖之四 玩家类的解剖所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)