Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠
来源:程序员人生 发布时间:2015-01-26 09:13:49 阅读次数:2787次
该部份同前面处理逻辑基本1样,道路上随机出现闪烁的珍珠,在捡到珍珠后,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(⑵00,⑵00));
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));
}
代码比较简单,后面随彩票代码1起发布
未完待续.................
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠