大家早上好,我就接着,昨天的写了;
玩过植物大战僵尸的朋友都应该知道太阳光照射产生金币的过程,我把它叫做太阳因子,我就这样命名了,怎么滴!
好吧,太阳因子会在地图上方随机位置产生;我设计它并要求它完成两个直线运动实现系统的自动拾取; 说到自动拾取我就想到我那次玩植物大战僵尸的时候,突然冒出一句:是否要系统自动帮您拾取太阳光照,当时感觉这真他妈搞科技,好人性化;呵呵;其实不过是简单的流程控制罢了!
好,下面我们来建一个层:(首先来说下这个层得作用:产生并实现太阳因子的各个功能,包括太阳因子的线性运动和回收);
SunCellLayer.h :
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
#include "SunCellSprite.h"
class SunCellLayer :public cocos2d::CCLayer
{
public:
SunCellLayer(void);
~SunCellLayer(void);
CREATE_FUNC(SunCellLayer);
bool virtual init();
SunCellSprite* _sunCellSprite; //太阳因子精灵
void initSunCell(float dt);//定时产生太阳因子
cocos2d::CCSpriteBatchNode* _sunBatchNode;//太阳因子的精灵批处理节点
cocos2d::CCSpriteFrameCache* _sunCache;//太阳因子的精灵框帧缓存
void SunCellMoveWay();//声明太阳因子的运动路线;
void removeSunCell(CCNode* pSend);//太阳因子的回收
};
在.cpp中我们先不去写上面方法的具体实现过程;
#include "SunCellLayer.h"
#include "GameLayer.h"
USING_NS_CC;
SunCellLayer::SunCellLayer(void)
{
this->_sunCache =CCSpriteFrameCache::sharedSpriteFrameCache();//创建精良框帧缓存
this->_sunCache->addSpriteFramesWithFile("suncell.plist");//包含精灵碎图的文件
this->_sunCache->retain(); //保存缓存资源
this->_sunBatchNode=CCSpriteBatchNode::create("suncell.pvr.ccz");//创建太阳因子的批处理节点;
this->_sunBatchNode->retain();
this->addChild(this->_sunBatchNode);//把精灵批处理节点加到太阳因子层
this->_sunCellSprite =NULL;
}
SunCellLayer::~SunCellLayer(void)
{
this->_sunCache->release();
this->_sunBatchNode->release();
}
bool SunCellLayer::init()
{
if(!CCLayer::init())
{
return false;
}
return true;
}
void SunCellLayer::initSunCell(float dt)
{
}
void SunCellLayer::SunCellMoveWay()
{
}
void SunCellLayer::removeSunCell(CCNode* pSend)
{
}
这是 SunCellLayer类的基本结构;
下面我们要创建一个太阳因子的精灵类;然后把它加到太阳因子层里面去
SunCellSprite.h
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\sprite_nodes\ccsprite.h"
#include "cocos2d.h"
class SunCellSprite :public cocos2d::CCSprite
{
public:
SunCellSprite(void);
~SunCellSprite(void);
CREATE_FUNC(SunCellSprite);
virtual bool init();
};
SunCellSprite.cpp
#include "SunCellSprite.h"
USING_NS_CC;
SunCellSprite::SunCellSprite(void)
{
}
SunCellSprite::~SunCellSprite(void)
{
}
bool SunCellSprite::init()
{
if(!CCSprite::initWithSpriteFrameName("Sun_1.png"))
{
return false;
}
int i;
CCArray* sunArray = CCArray::create();//创建一个数组用于存放太阳因子的帧
sunArray->retain();
//下面是太阳因子的动画实现过程
for(i=1;i<23;i++)
{
CCSpriteFrame* sunFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("Sun_%d.png",i)->getCString());
sunArray->addObject(sunFrames);
}
CCAnimation* sunAnimation=CCAnimation::createWithSpriteFrames(sunArray,0.2f);
this->runAction(CCRepeatForever::create(CCAnimate::create(sunAnimation)));
return true;
}
现在我们要把太阳因子精灵加到太阳因子层里面去;
在上面的SunCellLayer.h中我们已经声明过了,大家回头看下;
下面我们就来填充SunCellLayer.cpp哪些空的函数体,我们要求太阳因子每隔一小段时间产生一次。所以在上面的
void SunCellLayer::initSunCell(float dt)函数中加入以下代码:
void SunCellLayer::initSunCell(float dt)
{
this->_sunCellSprite = SunCellSprite::create();//产生代糖因子
this->_sunBatchNode->addChild(this->_sunCellSprite);//把太阳因子加入到精灵批处理节点中去
CCSize winsize = CCDirector::sharedDirector()->getWinSize();//获取窗口的大小
//设定太阳因子产生的随机位子
this->_sunCellSprite->setPosition(ccp(3*winsize.width/4 * rand()/RAND_MAX + 1*winsize.width/5,winsize.height+this->_sunCellSprite->getContentSize().height));
this->SunCellMoveWay();//执行上面提到的两个线性运动,并在金币产生的位置回收太阳因子
}
我们要求太阳因子做两个线性运动后回收;所以在上面的void SunCellLayer::SunCellMoveWay()中加入以下代码:
void SunCellLayer::SunCellMoveWay()
{
//这个函数代表太阳因子的具体运动过程
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCFiniteTimeAction* sunCellMove1 = CCMoveTo::create(4.0f,ccp(this->_sunCellSprite->getPosition().x,1* winSize.height/4 *rand()/RAND_MAX + 2*winSize.height/5));
CCFiniteTimeAction* sunCellMove2 = CCMoveTo::create(0.5f,ccp(2*winSize.width/7, 8*winSize.height/9));
this->_sunCellSprite->runAction(CCSequence::create(sunCellMove1,sunCellMove2,CCCallFuncN::create(this,callfuncN_selector(SunCellLayer::removeSunCell)),NULL));
}
在上面的void SunCellLayer::removeSunCell(CCNode* pSend)加入以下代码:
void SunCellLayer::removeSunCell(CCNode* pSend)
{
CCSprite* sprite = (CCSprite*) pSend;
this->_sunBatchNode->removeChild(sprite,true);//从精灵批处理节点中回收太阳因子
((GameLayer*)this->getParent())->_dollarDisplayLayer->_dollar = ((GameLayer*)this->getParent())->_dollarDisplayLayer->_dollar +25;//这句很重要,我的得意之处
//利用C++多态实现金币的自动递增;
}
在SunCellLayer.cpp的init()中调用定时器:
schedule(schedule_selector(SunCellLayer::initSunCell),9.0f);//太阳因子的定时器
最后把我们的太阳因子层加到主游戏层里面去:
在GameLayer.h中加入以下代码:
#include "SunCellLayer.h"
SunCellLayer* _sunCellLayer;
void initSunCellLayer();
在GameLayer.cpp的构造函数中:(这点很重要,一不小心就有可能造成内存泄露)
this->_sunCellLayer=NULL;
在void GameLayer::initSunCellLayer()中定义初始化太阳因子层:
//初始化太阳因子层
void GameLayer::initSunCellLayer()
{
this->_sunCellLayer = SunCellLayer::create();
this->addChild(this->_sunCellLayer);
}
在GameLayer.cpp中的init()方法中加入以下代码:
this->initSunCellLayer();
这样太阳因子部分就完成了。。貌似有点多啊,先生们!