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

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

概述每隔N个回合,彩票开奖一次,每期开奖奖金固定5万,暂不累积。摇奖效果一般,以后考虑用物理引擎实现 1、定义彩票开奖类 bool LotteryPublish::init(){ addItemSpriteFrameCache(); SpriteFrame* spf; spf = itemSpriteFrameCache->getSpriteFrameByName("publish_ly01. 每隔N个回合,彩票开奖一次,每期开奖奖金固定5万,暂不累积。摇奖效果一般,以后考虑用物理引擎实现


1、定义彩票开奖类

bool LotteryPublish::init(){	addItemSpriteFrameCache();	SpriteFrame* spf;	spf = itemSpriteFrameCache->getSpriteFrameByname("publish_ly01.png");	Sprite::initWithSpriteFrame(spf);	setItemAnimate();	return true;}voID LotteryPublish::addItemSpriteFrameCache(){	itemSpriteFrameCache = SpriteFrameCache::getInstance();	itemSpriteFrameCache->addSpriteFramesWithfile("images/publish_ly.pList","images/publish_ly.png");	memset(name,20);	for (int i=1; i<=21; i++) 	{		sprintf(name,"publish_ly%02d.png",i);		item_anim_vector.pushBack(itemSpriteFrameCache->getSpriteFrameByname(name));	}}//开奖动画voID LotteryPublish::setItemAnimate(){	if(!AnimationCache::getInstance()->getAnimation("publish_ly_animation"))	{		AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(item_anim_vector,0.1f),"publish_ly_animation");	}	 normal_anmi = Animate::create(AnimationCache::getInstance()->getAnimation("publish_ly_animation"));	 normal_anmi->retain();}

2、创建开奖画面的对话框。
voID GameBaseScene::initPopPublishLottery(){	popDialogLottery = PopupLayer::create(DIALOG_BG);		popDialogLottery->setContentSize(CCSizeMake(Dialog_Size_WIDth,Dialog_Size_Height+180)); 	popDialogLottery->setTitle(LanguageString::getInstance()->getLanguageString(PUBliSH_LottERY)->getCString());	popDialogLottery->setContentText("",20,60,250);	popDialogLottery->setPopType(LottERY_PUBliSH);//开奖类型的对话框	popDialogLottery->setPlayerVector(players_vector);//传入角色容器,开奖画面会根据这个显示角色购买的彩票号码	popDialogLottery->setTag(100);	this->addChild(popDialogLottery);		popDialogLottery->setVisible(false);	}在显示Go按钮之前,根据回合数显示开奖界面voID GameBaseScene::receivednotificationOMsg(Object* data){.............	case MSG_GO_SHOW_TAG:		{			//便于测试,每一回合结束都显示开奖画面			if(gameRoundCount !=0 && gameRoundCount%1 == 0)			{				//前面角色买地等,会播放动画,所以这里延迟一下,显示开奖画面				scheduleOnce(schedule_selector( GameBaseScene::popPublishLottery),2.0f);							}else			{				showGobutton();			}			break;		}............}//把开奖画面显示出来,并播放摇奖动画voID GameBaseScene::popPublishLottery(float dt){	popDialogLottery->setVisible(true);		//开奖画面中添加角色购买的彩票号码	popDialogLottery->addplayersLottery();	//播放摇奖动画	popDialogLottery->runPublishAnmi();}

3、在PopupLayer.h中添加开奖对话框枚举LottERY_PUBliSH
enum POP_TYPE{	norMAL,LottERY,LottERY_PUBliSH,STOCK,};//当对话框进入后调用setPublishLotteryContext,在对话框中添加开奖画面voID PopupLayer::onEnter(){......	case LottERY_PUBliSH:		{			setPublishLotteryContext(contentSize);			break;		}.....}//在对话框中添加开奖画面voID PopupLayer::setPublishLotteryContext(Size size){	Size winSize = Director::getInstance()->getWinSize();	lp = LotteryPublish::create();	addChild(lp);	lp->setposition((winSize)/2);	addplayersInfo(size);}

//添加角色图标
voID PopupLayer::addplayersInfo(Size size){	Size winSize = Director::getInstance()->getWinSize();	Size center =(winSize-size)/2;	int j=0;	for(auto it=players_vector.begin();it!=players_vector.end();it++)	{		RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);		SpriteFrame* spf;		int tag = player->getTag();		switch(tag)		{		case PLAYER_1_TAG:			{				spf = player->player_spriteFrameCache->getSpriteFrameByname("player1_anim_01.png");				break;			}		case PLAYER_2_TAG:			{				spf = player->player_spriteFrameCache->getSpriteFrameByname("player2_anim_02.png");				break;			}		}		Sprite* playerSprite = Sprite::createWithSpriteFrame(spf);		playerSprite->setposition( center.wIDth+20,(winSize.height/2+50)+j*50);		addChild(playerSprite);		j++;	}	}

//添加角色购买的彩票
voID PopupLayer::addplayersLottery(){	for(int i=1;i<=30;i++)	{		if(this->getChildByTag(1000+i) != NulL)		{			this->removeChildByTag(1000+i);		}			}	Size winSize = Director::getInstance()->getWinSize();	Size size = this->getContentSize();	Size center =(winSize-size)/2;	int j=0;	for(auto it=players_vector.begin();it!=players_vector.end();it++)	{		RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);				playerLotteryVector.clear();		for(int i=0;i < player->lottery_vector.size();i++)		{						LabelTTF* labelLotteryNumber = LabelTTF::create(String::createWithFormat("%i",player->lottery_vector.at(i))->getCString(),"",15);			labelLotteryNumber->setposition(ccp( center.wIDth+20+(i+1)*20,(winSize.height/2+30)+j*50));			labelLotteryNumber->setcolor(color3B(255,100,100));			labelLotteryNumber->setTag(1000+player->lottery_vector.at(i));			playerLotteryVector.pushBack(labelLotteryNumber);		}		for(int i=0;i < playerLotteryVector.size();i++)		{			addChild(playerLotteryVector.at(i));			}		j++;	}}

//开始摇奖动画
voID PopupLayer::runPublishAnmi(){	scheduleOnce(schedule_selector( PopupLayer::realRunPublishAnmi),3.0f);}

//开始真正摇奖
voID PopupLayer::realRunPublishAnmi(float dt){lp->runAction(Sequence::create(lp->getnormal_anmi(),CallFunc::create([this]()		{			int lott = rand()%(30)+1;			//四秒后 让开奖画面消失			scheduleOnce(schedule_selector( PopupLayer::dismissFromParent),4.0f);						Sprite* ball = Sprite::create("images/orange_ball.png");			ball->setposition(lp->getposition()-lp->getContentSize()/2 + ccp(0,13));			ball->setAnchorPoint(ccp(0,0));			addChild(ball);			LabelTTF* ltf = LabelTTF::create(String::createWithFormat("%02d",lott)->getCString(),20);			ltf->setposition(ball->getposition()+ccp(5,6));			ltf->setAnchorPoint(ccp(0,0));			addChild(ltf);			Size winSize = Director::getInstance()->getWinSize();			Size center =(winSize)/2;			int j=0;			//判断角色是否中奖			for(auto it=players_vector.begin();it!=players_vector.end();it++)			{				RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);				//player->lottery_vector.push_back(8);				for(int i=0;i < player->lottery_vector.size();i++)				{					if(player->lottery_vector.at(i) == lott)					{						player->setMoney(player->getMoney()+LottERY_WIN_MONEY);						ParticleSystem* lotteryWinParticle = ParticleSystemQuad::create("images/lottery_win.pList");						lotteryWinParticle->retain();						ParticleBatchNode *batch = ParticleBatchNode::createWithTexture(lotteryWinParticle->getTexture());						batch->addChild(lotteryWinParticle);							addChild(batch);						lotteryWinParticle->setposition( center.wIDth+20,(winSize.height/2+50)+j*50 );																lotteryWinParticle->release();						lotteryWinParticle->setautoRemoveOnFinish(true); 					}									}				player->lottery_vector.clear();				j++;			}					}	),NulL));}

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

4、GameBaseScene收到该消息后,更新资金,并显示Go按钮
	case MSG_DIMISS_DIALOG_PUBliSH_LottERY_TAG:		{			//this->removeChildByTag(100);			for(auto it=players_vector.begin();it!=players_vector.end();it++)			{				RicherPlayer* player = dynamic_cast<RicherPlayer*>(*it);				refreshMoneyLabel(player,0);			}			showGobutton();			break;		}



点击下载

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

总结

以上是内存溢出为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存