废话不多说,直接上代码再看:
头文件
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;
//计算玩家牌和出的牌的初始位置 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值或牌的触摸优先级”,为什么要这么做呢?这是因为牌的顺序换了之后,底下的牌有的会覆盖到另一个牌之上,改变触摸优先级也是同理。