Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每隔N个回合,彩票开奖一次,每期开奖奖金固定5万,暂不累积。摇奖效果一般,以后考虑用物理引擎实现


1、定义彩票开奖类

  1. bool LotteryPublish::init()
  2. {
  3. addItemSpriteFrameCache();
  4. SpriteFrame* spf;
  5. spf = itemSpriteFrameCache->getSpriteFrameByName("publish_ly01.png");
  6.  
  7. Sprite::initWithSpriteFrame(spf);
  8. setItemAnimate();
  9. return true;
  10. }
  11.  
  12. void LotteryPublish::addItemSpriteFrameCache()
  13. {
  14. itemSpriteFrameCache = SpriteFrameCache::getInstance();
  15. itemSpriteFrameCache->addSpriteFramesWithFile("images/publish_ly.plist","images/publish_ly.png");
  16.  
  17. memset(name,20);
  18.  
  19. for (int i=1; i<=21; i++)
  20. {
  21. sprintf(name,"publish_ly%02d.png",i);
  22. item_anim_vector.pushBack(itemSpriteFrameCache->getSpriteFrameByName(name));
  23. }
  24.  
  25. }
  26. //开奖动画
  27. void LotteryPublish::setItemAnimate()
  28. {
  29. if(!AnimationCache::getInstance()->getAnimation("publish_ly_animation"))
  30. {
  31. AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(item_anim_vector,0.1f),"publish_ly_animation");
  32. }
  33. normal_anmi = Animate::create(AnimationCache::getInstance()->getAnimation("publish_ly_animation"));
  34. normal_anmi->retain();
  35. }

2、创建开奖画面的对话框。
  1. void GameBaseScene::initPopPublishLottery()
  2. {
  3. popDialogLottery = PopupLayer::create(DIALOG_BG);
  4. popDialogLottery->setContentSize(CCSizeMake(Dialog_Size_Width,Dialog_Size_Height+180));
  5. popDialogLottery->setTitle(LanguageString::getInstance()->getLanguageString(PUBLISH_LOTTERY)->getCString());
  6. popDialogLottery->setContentText("",20,60,250);
  7. popDialogLottery->setPopType(LOTTERY_PUBLISH);//开奖类型的对话框
  8. popDialogLottery->setPlayerVector(players_vector);//传入角色容器,开奖画面会根据这个显示角色购买的彩票号码
  9. popDialogLottery->setTag(100);
  10. this->addChild(popDialogLottery);
  11. popDialogLottery->setVisible(false);
  12. }
  13.  
  14.  
  15.  
  16. 显示Go按钮之前,根据回合数显示开奖界面
  17. void GameBaseScene::receivedNotificationOMsg(Object* data)
  18. {
  19. .............
  20. case MSG_GO_SHOW_TAG:
  21. {
  22. //便于测试,每一回合结束都显示开奖画面
  23. if(gameRoundCount !=0 && gameRoundCount%1 == 0)
  24. {
  25. //前面角色买地等,会播放动画,所以这里延迟一下,显示开奖画面
  26. scheduleOnce(schedule_selector( GameBaseScene::popPublishLottery),2.0f);
  27. }else
  28. {
  29. showGoButton();
  30. }
  31. break;
  32. }
  33. ............
  34. }
  35.  
  36. //把开奖画面显示出来,并播放摇奖动画
  37. void GameBaseScene::popPublishLottery(float dt)
  38. {
  39. popDialogLottery->setVisible(true);
  40. //开奖画面中添加角色购买的彩票号码
  41. popDialogLottery->addPlayersLottery();
  42. //播放摇奖动画
  43. popDialogLottery->runPublishAnmi();
  44. }

3、在PopupLayer.h中添加开奖对话框枚举LOTTERY_PUBLISH
  1. enum POP_TYPE
  2. {
  3. NORMAL,LOTTERY,LOTTERY_PUBLISH,STOCK,};
  4.  
  5. //当对话框进入后调用setPublishLotteryContext,在对话框中添加开奖画面
  6. void PopupLayer::onEnter()
  7. {
  8. ......
  9. case LOTTERY_PUBLISH:
  10. {
  11. setPublishLotteryContext(contentSize);
  12. break;
  13. }
  14.  
  15. .....
  16. }
  17.  
  18. //在对话框中添加开奖画面
  19. void PopupLayer::setPublishLotteryContext(Size size)
  20. {
  21. Size winSize = Director::getInstance()->getWinSize();
  22. lp = LotteryPublish::create();
  23. addChild(lp);
  24. lp->setPosition((winSize)/2);
  25.  
  26. addPlayersInfo(size);
  27.  
  28. }

//添加角色图标
  1. void PopupLayer::addPlayersInfo(Size size)
  2. {
  3. Size winSize = Director::getInstance()->getWinSize();
  4. Size center =(winSize-size)/2;
  5. int j=0;
  6. for(auto it=players_vector.begin();it!=players_vector.end();it++)
  7. {
  8. RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);
  9.  
  10. SpriteFrame* spf;
  11. int tag = player->getTag();
  12. switch(tag)
  13. {
  14. case PLAYER_1_TAG:
  15. {
  16. spf = player->player_spriteFrameCache->getSpriteFrameByName("player1_anim_01.png");
  17. break;
  18. }
  19. case PLAYER_2_TAG:
  20. {
  21. spf = player->player_spriteFrameCache->getSpriteFrameByName("player2_anim_02.png");
  22. break;
  23. }
  24. }
  25.  
  26. Sprite* playerSprite = Sprite::createWithSpriteFrame(spf);
  27. playerSprite->setPosition( center.width+20,(winSize.height/2+50)+j*50);
  28. addChild(playerSprite);
  29. j++;
  30. }
  31. }

//添加角色购买的彩票
  1. void PopupLayer::addPlayersLottery()
  2. {
  3. for(int i=1;i<=30;i++)
  4. {
  5. if(this->getChildByTag(1000+i) != NULL)
  6. {
  7. this->removeChildByTag(1000+i);
  8. }
  9. }
  10.  
  11. Size winSize = Director::getInstance()->getWinSize();
  12. Size size = this->getContentSize();
  13. Size center =(winSize-size)/2;
  14. int j=0;
  15. for(auto it=players_vector.begin();it!=players_vector.end();it++)
  16. {
  17. RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);
  18. playerLotteryVector.clear();
  19. for(int i=0;i < player->lottery_vector.size();i++)
  20. {
  21. LabelTTF* labelLotteryNumber = LabelTTF::create(String::createWithFormat("%i",player->lottery_vector.at(i))->getCString(),"",15);
  22. labelLotteryNumber->setPosition(ccp( center.width+20+(i+1)*20,(winSize.height/2+30)+j*50));
  23. labelLotteryNumber->setColor(Color3B(255,100,100));
  24. labelLotteryNumber->setTag(1000+player->lottery_vector.at(i));
  25. playerLotteryVector.pushBack(labelLotteryNumber);
  26. }
  27.  
  28. for(int i=0;i < playerLotteryVector.size();i++)
  29. {
  30. addChild(playerLotteryVector.at(i));
  31. }
  32. j++;
  33.  
  34. }
  35. }

//开始摇奖动画
  1. void PopupLayer::runPublishAnmi()
  2. {
  3. scheduleOnce(schedule_selector( PopupLayer::realRunPublishAnmi),3.0f);
  4. }

//开始真正摇奖
  1. void PopupLayer::realRunPublishAnmi(float dt)
  2. {
  3. lp->runAction(Sequence::create(lp->getNormal_anmi(),CallFunc::create([this]()
  4. {
  5. int lott = rand()%(30)+1;
  6. //四秒后 让开奖画面消失
  7. scheduleOnce(schedule_selector( PopupLayer::dismissFromParent),4.0f);
  8. Sprite* ball = Sprite::create("images/orange_ball.png");
  9. ball->setPosition(lp->getPosition()-lp->getContentSize()/2 + ccp(0,13));
  10. ball->setAnchorPoint(ccp(0,0));
  11. addChild(ball);
  12. LabelTTF* ltf = LabelTTF::create(String::createWithFormat("%02d",lott)->getCString(),20);
  13. ltf->setPosition(ball->getPosition()+ccp(5,6));
  14. ltf->setAnchorPoint(ccp(0,0));
  15. addChild(ltf);
  16. Size winSize = Director::getInstance()->getWinSize();
  17. Size center =(winSize)/2;
  18. int j=0;
  19. //判断角色是否中奖
  20. for(auto it=players_vector.begin();it!=players_vector.end();it++)
  21. {
  22. RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);
  23. //player->lottery_vector.push_back(8);
  24.  
  25. for(int i=0;i < player->lottery_vector.size();i++)
  26. {
  27. if(player->lottery_vector.at(i) == lott)
  28. {
  29. player->setMoney(player->getMoney()+LOTTERY_WIN_MONEY);
  30.  
  31. ParticleSystem* lotteryWinParticle = ParticleSystemQuad::create("images/lottery_win.plist");
  32. lotteryWinParticle->retain();
  33. ParticleBatchNode *batch = ParticleBatchNode::createWithTexture(lotteryWinParticle->getTexture());
  34. batch->addChild(lotteryWinParticle);
  35. addChild(batch);
  36.  
  37. lotteryWinParticle->setPosition( center.width+20,(winSize.height/2+50)+j*50 );
  38. lotteryWinParticle->release();
  39. lotteryWinParticle->setAutoRemoveOnFinish(true);
  40.  
  41. }
  42. }
  43. player->lottery_vector.clear();
  44. j++;
  45. }
  46. }
  47. ),NULL));
  48. }

//发送对话框消失消息,并设置为不可见
  1. void PopupLayer::dismissFromParent(float dt)
  2. {
  3. NotificationCenter::getInstance()->postNotification(MSG_DIMISS_DIALOG,String::createWithFormat("%d",MSG_DIMISS_DIALOG_PUBLISH_LOTTERY_TAG));
  4. this->setVisible(false);
  5. }

4、GameBaseScene收到该消息后,更新资金,并显示Go按钮
  1. case MSG_DIMISS_DIALOG_PUBLISH_LOTTERY_TAG:
  2. {
  3. //this->removeChildByTag(100);
  4. for(auto it=players_vector.begin();it!=players_vector.end();it++)
  5. {
  6. RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);
  7. refreshMoneyLabel(player,0);
  8. }
  9. showGoButton();
  10. break;
  11. }



点击下载

未完待续................

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