首先了解CCSpriteBatchNode的使用(参考网址:http://www.cocos2dev.com/?p=331)
关于子弹的处理参考网址:http://blog.youkuaiyun.com/jackystudio/article/details/11859449
源码下载地址:点击打开链接
关于svn的简单使用:点击打开链接
在处理子弹时,我们是不是也一颗子弹一个精灵来渲染显示呢?我们知道,子弹往往连续很多的,
如果这样处理是不是很耗资源呢?对于同样的一类子弹有没有什么办法只渲染一次来减轻对资源的
压力呢?在cocos2d中就有这么一个类是CCSpriteBatchNode。
1.使用CCSprite创建1000个Icon.png精灵到场景中,这样就会渲染1000次
for(int i = 0;i < 1000;++i){
int x = arc4random()%960;
int y = arc4random()%640;
CCSprite* testIcon = CCSprite::create("Icon.png");
testIcon->setPosition( ccp(x, y) );
this->addChild(testIcon);
}
运行如图:注意左下角最上面数字为1000.
2.当我们使用CCSpriteBatchNode时
CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("Icon.png", 1000);
batchNode->setPosition(CCPointZero);
this->addChild(batchNode);
for(int i = 0;i < 1000;++i){
int x = arc4random()%960;
int y = arc4random()%640;
CCSprite* testIcon = CCSprite::createWithTexture(batchNode->getTexture());
testIcon->setPosition( ccp(x, y) );
batchNode->addChild(testIcon);
}
运行如图:注意左下角最上面数字为1.
其实我们可以这样理解,第一种方法是每次调用Icon.png都会重新解码一次,所以得解码1000次,肯定耗资源。
而对于第二种方法则是把Icon.png解码出来的数据保存在batchNode中,需要时只需要在内存中读取该
已经解码了的数据即可,显然只解码了一次,节约了资源。
下面进入飞机大战子弹类的创建
// BulletLayer.h
#ifndef __BULLETLAYER_H__
#define __BULLETLAYER_H__
#include "cocos2d.h"
#include "PlaneLayer.h"
USING_NS_CC;
class BulletLayer : public CCLayer
{
public:
BulletLayer(void);
~BulletLayer(void);
CREATE_FUNC(BulletLayer);
virtual bool init();
void AddBullet(float dt);
void bulletMoveFinished(CCNode *pSender);
void RemoveBullet(CCSprite* bullet);
void StartShoot(float delay=0.0f);
void StopShoot(void);
public:
CCArray *m_pAllBullet;
CCSpriteBatchNode *bulletBatchNode;
};
#endif
// BulletLayer.cpp
#include "BulletLayer.h"
BulletLayer::BulletLayer(void)
{
m_pAllBullet = CCArray::create();
m_pAllBullet->retain();
}
BulletLayer::~BulletLayer(void)
{
m_pAllBullet->release();
m_pAllBullet = NULL;
}
bool BulletLayer::init(void)
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("ui/shoot.png");
bulletBatchNode = CCSpriteBatchNode::createWithTexture(texture);
this->addChild(bulletBatchNode);
bRet = true;
}while(0);
return bRet;
}
void BulletLayer::AddBullet(float dt)
{
CCSprite *bullet = CCSprite::createWithSpriteFrameName("bullet1.png");
bulletBatchNode->addChild(bullet);
CCPoint planePosition=PlaneLayer::sharedPlane->getChildByTag(AIRPLANE)->getPosition();
CCPoint bulletPosition=ccp(planePosition.x,
planePosition.y + PlaneLayer::sharedPlane->getChildByTag(AIRPLANE)->getContentSize().height/2);
bullet->setPosition(bulletPosition);
float length = CCDirector::sharedDirector()->getWinSize().height
+ bullet->getContentSize().height/2 - bulletPosition.y;
float velocity = 420/1;
float realMoveDuration = length/velocity;
CCFiniteTimeAction *actionMove = CCMoveTo::create(realMoveDuration,
ccp(bulletPosition.x, CCDirector::sharedDirector()->getWinSize().height + bullet->getContentSize().height/2));
CCFiniteTimeAction *actionDone = CCCallFuncN::create(this,callfuncN_selector(BulletLayer::bulletMoveFinished));
CCSequence *sequence = CCSequence::create(actionMove, actionDone,NULL);
bullet->runAction(sequence);
}
void BulletLayer::StartShoot(float delay)
{
this->schedule(schedule_selector(BulletLayer::AddBullet),0.20f,kCCRepeatForever,delay);
}
void BulletLayer::StopShoot(void)
{
this->unschedule(schedule_selector(BulletLayer::AddBullet));
}
void BulletLayer::bulletMoveFinished(CCNode *pSender)
{
CCSprite *bullet = (CCSprite *)pSender;
m_pAllBullet->removeObject(bullet);
this->bulletBatchNode->removeChild(bullet,true);
}
void BulletLayer::RemoveBullet(CCSprite* bullet)
{
if (bullet!=NULL)
{
this->m_pAllBullet->removeObject(bullet);
this->removeChild(bullet,true);
}
}
GameLayer.h文件中添加头文件 #include "BulletLayer.h"以及类中增加成员变量 BulletLayer *bulletLayer;
GameLayer.cpp文件中构造中增加 bulletLayer = NULL; 在init中添加
this->bulletLayer=BulletLayer::create();
this->addChild(bulletLayer);
this->bulletLayer->StartShoot();
把BulletLayer.h和BulletLayer.cpp添加进项目工程编译运行。
运行结果如下图: