`
369540808
  • 浏览: 196477 次
文章分类
社区版块
存档分类
最新评论

cocos2d-x 植物大战僵尸(二)

 
阅读更多

结着上次的写吧;

要实现的效果是阳光因子(姑且这么叫吧);阳光因子每隔一段时间在随机游戏窗口的上方随机产生;散落到屏幕的随机位置后,通过一个回调函数收回;收回的瞬间把玩家的金币 加 $25;(就这么个过程吧);要实现效果大概就是:


先来顾虑下实现这个过程需要哪些逻辑来完成;

(1)就是要有个金币计数板;(2)时时更新也就是每一帧都更新金币数目这个可以通过update来实现;(3):产生太阳因子的过程,和他的回收过程。(4):设定定时器每隔一段时间产生太阳因子;

那就按照这个过程一一实现吧:


首先在GaneLayer 层中声明一个变量 int _allScore ; 在构造函数中初始化为 this->_allScore = 225; //这个是金币展示板得初始值;

下面要来创建金币展示板了;

在GameLayer中声明一下内容:

//7:创建金币显示板
cocos2d::CCLabelTTF* _scoreLabel;
void createScoreLabel();

在GameLayer.cpp中:

实现这方法:

void GameLayer::createScoreLabel()
{
this->_scoreLabel =CCLabelTTF::create();
CCString* scoreStr = CCString::createWithFormat("Score : $%d",this->_allScore);
this->_scoreLabel->setString(scoreStr->m_sString.c_str());
this->addChild(this->_scoreLabel);
this->_scoreLabel->setPosition(ccp(350,550));
this->_scoreLabel->setFontSize(26);
this->_scoreLabel->setColor(ccc3(0,0,0));
}

这段代码可以产生这样的效果:



接下来就是要更新金币计数板了,,只要金币数目发生了改变。。就让计数板上面跟着改变:那就要使用update来更新了;

在GameLayer层中。。声明update:

//更新金币数目;
//更新金币数目;
void update(float dt);

它的实现过程我是这样写的:

void GameLayer::update(float dt)
{
CCString* scoreStr = CCString::createWithFormat("Score : $%d",this->_allScore);
this->_scoreLabel->setString(scoreStr->m_sString.c_str()); //这里是类型转换将整形转换、成char*型

}

上面的代码意思是只要金币数发生了变化就更新金币数;


接下来是产生太阳因子和回收过程:(太阳因子可以看做一个独立精灵所以我决定给他写个精灵类)

创建:太阳因子类SunSprite 继承 PlantSpriteAstribute ,PlantSpriteAstribute继承CCSprite

#pragma once
#include "plantspriteastribute.h"
#include "cocos2d.h"
class SunSprite :public PlantSpriteAstribute
{
public:
SunSprite(void);
~SunSprite(void);


CREATE_FUNC(SunSprite);
virtual bool init();
};

在SunSprite.cpp中;实现init()方法

#include "SunSprite.h"
USING_NS_CC;
SunSprite::SunSprite(void)
{
}
SunSprite::~SunSprite(void)
{
}
bool SunSprite::init()
{
if(!PlantSpriteAstribute::initWithSpriteFrameName("Sun_1.png"))
{
return false;
}
CCArray* _plantArray =CCArray::create();
_plantArray->retain();
for(int i=1;i<22;i++)
{
CCSpriteFrame* plantFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("Sun_%d.png",i)->getCString());
_plantArray->addObject(plantFrames);
}
CCAnimation* plantAnimation =CCAnimation::createWithSpriteFrames(_plantArray,0.2f);
this->runAction(CCRepeatForever::create(CCAnimate::create(plantAnimation)));
return true;
}

上面这个过程是播放太阳因子动画的过程;


接下来我们把这个太阳因子加到游戏层里面去;

#include "SunSprite.h"

并作一下声明:

//创建阳光动画
SunSprite* _sunSprite;
void createSun(float dt);
void removeSun(CCNode* pSend);

在GameLayer的构造函数中,加入太阳因子的精灵框帧缓存,和批处理节点;

this->_cache->addSpriteFramesWithFile("plant.plist"); //植物和阳光统一放在这里面;

this->_batchNode_Plant =CCSpriteBatchNode::create("plant.pvr.ccz");
this->addChild(_batchNode_Plant);
this->_batchNode_Plant->retain();

在其析构函数中释放内存,避免内存泄露:

_batchNode_Plant->release();

下面来实现createSun方法和回收方法:

void GameLayer::createSun(float dt)
{
this->_sunSprite =SunSprite::create();
this->_sunSprite->setPosition(ccp(600*rand()/RAND_MAX + 380,600));
this->_batchNode_Plant->addChild(this->_sunSprite);
this->_sunSprite->setOpacity(180);


//产生太阳以后,要做两个运动
CCFiniteTimeAction* move1 = CCMoveTo::create(4.0f,ccp(this->_sunSprite->getPosition().x,380*rand()/RAND_MAX+100));
CCFiniteTimeAction* move2 = CCMoveTo::create(0.5f,ccp(300,550));
this->_sunSprite->runAction(CCSequence::create(move1,move2,CCCallFuncN::create(this,callfuncN_selector(GameLayer::removeSun)),NULL));
}


void GameLayer::removeSun(CCNode* pSend)
{
CCSprite* sprite =(CCSprite*)pSend;
this->_batchNode_Plant->removeChild(sprite,true);
this->_allScore=this->_allScore+25;
}


。。。因为创建太阳因子,,我们是设定定时器的,,所以要调用定时器函数;

schedule(schedule_selector(GameLayer::createSun),9.0f); //这里限定太阳因子每9s产生一次;产生后做两个线性运动,,并被系统回收掉; 回收后,让总金币加25;








分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics