首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠

Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠

该部分同前面处理逻辑基本一样,道路上随机出现闪烁的珍珠,在捡到珍珠后,toast显示捡到珍珠资金增加。

1、编写Item_ball类,该类存储了珍珠相关动画

bool Item_ball::init()
{
	addItemSpriteFrameCache();
	SpriteFrame* spf;
	spf = itemSpriteFrameCache->getSpriteFrameByName("ball_01.png");

	Sprite::initWithSpriteFrame(spf);
	setItemAnimate();
	return true;
}

void Item_ball::addItemSpriteFrameCache()
{
	itemSpriteFrameCache = SpriteFrameCache::getInstance();
	itemSpriteFrameCache->addSpriteFramesWithFile("images/ball.plist","images/ball.png");

	memset(name, 0, 20);

	for (int i=1; i<=3; i++) 
	{
		sprintf(name, "ball_%02d.png",i);
		item_anim_vector.pushBack(itemSpriteFrameCache->getSpriteFrameByName(name));
	}

}

void Item_ball::setItemAnimate()
{
	if(!AnimationCache::getInstance()->getAnimation("ball_animation"))
	{
		AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(item_anim_vector,0.4f),"ball_animation");
	}
	 normal_anmi = Animate::create(AnimationCache::getInstance()->getAnimation("ball_animation"));
	 normal_anmi->retain();
}

2、GameBaseScene的initItemSprite方法添加了item_ball对象
void GameBaseScene::initItemSprite()
{
	item_crab = (Item_crab*)Item::create(ITEM_CRAB);
	addChild(item_crab);
	item_crab->runAction(RepeatForever::create(item_crab->getNormal_anmi()));
	item_crab->setVisible(false);

    item_ball = (Item_ball*)Item::create(ITEM_BALL);
	addChild(item_ball);
	item_ball->runAction(RepeatForever::create(item_ball->getNormal_anmi()));
	item_ball->setVisible(false);
	.................
}	

3、updateBlockWaySprites方法更新珍珠的位置

void GameBaseScene::updateBlockWaySprites(float dt)
{
	................

	int _rand2 = rand()%(wayLayerPass_vector.size()); 
	Vec2 position2 = wayLayerPass_vector.at(_rand2);
	item_ball->setVisible(true);
	item_ball->setPosition(position2);
	item_ball->setAnchorPoint(ccp(0,0));

}

4、
当停留位置是珍珠时,toast显示信息,显示完毕后调用sendMSGDealAroundLand2方法,继续处理上下左右相邻地块信息
void GameBaseScene::doBlockWayEvent(RicherPlayer* player)
{
.............
else if(player->getBoundingBox().intersectsRect(item_ball->getBoundingBox()))
	{
		item_ball->setPosition(ccp(-200,-200));
		int money =  10000;
		refreshMoneyLabel(player,money);
		CocosToast::createToast(this,String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(PICK_BALL)->getCString(),money)->getCString(),TOAST_SHOW_TIME,player->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGDealAroundLand2);
	}
...........
}

void GameBaseScene::sendMSGDealAroundLand2()
{
	NotificationCenter::getInstance()->postNotification(MSG_AROUND_LAND,String::createWithFormat("%d",MSG_AROUND_LAND_TAG));
}


技术分享


代码比较简单,后面随彩票代码一起发布


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

Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠