首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖

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

每隔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, 0, 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 大富翁游戏项目开发-第二十四部分 彩票开奖