在介绍了前面的几篇后,对于源代码会在下一篇中上传,须要源代码的能够在下一篇中看到下载链接,开源才是王道,分享才干成长。
这篇继续介绍下游戏中的子弹层的渲染。也就是BulletSprite的介绍。
对于子弹层的渲染有两种机制。一种是直接从缓存中进行精灵创建,创建后的精灵直接加入并使用,另外一种机制为将创建的精灵加入到SpriteBatchNode中,这样进行渲染效率更高。对于这样渲染的机制我在这里略微提一下,
普通的渲染机制为:准备,渲染,清除。准备,渲染,清除。...
批次渲染机制为:准备,渲染,渲染....渲染。清除
看以看到上面两种差别。批次渲染中仅仅进行一次准备一次清除,要节省好多硬件消耗,这是游戏优化的一个重要指标。
对与文件里的两种渲染机制我都进行了实现,终于执行时我用的还是批次渲染,能够考到终于的编译为BulletSprite2.cpp,而不是BulletSprite.cpp。
好啦,说了这么多。该上代码了。
看一下BulletSprite.h头文件的内容:
<span style="font-size:18px;"><strong>#include "cocos2d.h"
USING_NS_CC;
class BulletSprite : public cocos2d::Layer
{
public:
BulletSprite();
~BulletSprite();
virtual bool init();
CREATE_FUNC(BulletSprite);
Animation* f_createAnimate(int count,int fps);//创建子弹执行的动画
void removeBullet( Node* pNode );//移除超出屏幕可视范围的子弹或者碰撞后的子弹清除
void ShootBullet(float dt);//发射子弹,在当中进行子弹的渲染和子弹的飞行动作
public:
Vector <Sprite *>vecBullet;//子弹容器
SpriteBatchNode* bulletBatchNode;//批次渲染节点
};</strong></span>
函数都有具体说明,不做过多解释,以下看BulletSprite2.cpp的内容:
<span style="font-size:18px;"><strong>#include "BulletSprite.h"
#include "PlaneLayer.h"
BulletSprite::BulletSprite() {
}
BulletSprite::~BulletSprite() {
}
bool BulletSprite::init() {
bool bRet = false;
do {
CC_BREAK_IF(!Layer::init());
Texture2D* texture = TextureCache::sharedTextureCache()->textureForKey("bullet.png");
//创建BatchNode节点
bulletBatchNode = SpriteBatchNode::createWithTexture(texture);
this->addChild(bulletBatchNode);
//每隔0.2S调用一次发射子弹函数
this->schedule(schedule_selector(BulletSprite::ShootBullet),0.2f);
bRet = true;
} while (0);
return bRet;
}
void BulletSprite::ShootBullet(float dt) {
Size winSize = Director::getInstance()->getWinSize();
auto PlanePos = PlaneLayer::sharedPlane->getChildByTag(AIRPLANE)->getPosition();
//从缓存中创建子弹
auto spritebullet = Sprite::createWithSpriteFrameName("bullet_1.png");
//将创建好的子弹加入到BatchNode中进行批次渲染
bulletBatchNode->addChild(spritebullet);
//将创建好的子弹加入到容器
vecBullet.pushBack(spritebullet);
Point bulletPos = (Point(PlanePos.x,
PlanePos.y
+ PlaneLayer::sharedPlane->getChildByTag(AIRPLANE)->getContentSize().height
/ 2 + 20));
spritebullet->setPosition(bulletPos);
spritebullet->setScale(0.4f);
float flyLen = winSize.height - PlanePos.y;
float flyVelocity = 320 / 1;//执行速度,能够自己控制。每秒所走的像素
float realFlyDuration = flyLen / flyVelocity;//实际飞行的时间
//子弹执行的距离和时间,从飞机处開始执行到屏幕顶端
auto actionMove = MoveTo::create(realFlyDuration,
Point(bulletPos.x, winSize.height));
//子弹执行完动作后进行函数回调。调用移除子弹函数
auto actionDone = CallFuncN::create(
CC_CALLBACK_1(BulletSprite::removeBullet, this));
Sequence* sequence = Sequence::create(actionMove, actionDone, NULL);
spritebullet->runAction(sequence);
}
/**
* 移除子弹。将子弹从容器中移除。同一时候也从SpriteBatchNode中移除
*/
void BulletSprite::removeBullet(Node* pNode) {
if (NULL == pNode) {
return;
}
Sprite* bullet = (Sprite*) pNode;
this->bulletBatchNode->removeChild(bullet, true);
vecBullet.eraseObject(bullet);
}
/**
* 从缓存中获取子弹动绘图片后创建子弹动画。终于返回创建好的动画
*/
Animation* BulletSprite::f_createAnimate(int count, int fps) {
char buff[16];
Animation *panimation = Animation::create();
panimation->setDelayPerUnit(1.0 / fps);
for (int id = 1; id <= count; id++) {
sprintf(buff, "bullet_%d.png", id);
panimation->addSpriteFrame(
SpriteFrameCache::getInstance()->getSpriteFrameByName(buff));
}
return panimation;
}
</strong></span>
上面函数中凝视的都比較具体,关键要注意批次渲染机制的运用,这是游戏优化的一大提高点。
介绍了这么多还没有開始介绍游戏主要逻辑层,我都等不及了,在下一篇中会进行游戏源代码的上传。须要的能够看下一篇的介绍。
这篇就介绍到这里,下一篇也是最后一篇。介绍游戏主要逻辑层的实现。