cocos2dx 3.0 微信打飞机--005

来源:互联网 发布:linux ls命令终端退出 编辑:程序博客网 时间:2024/05/20 18:44

cocos2dx 3.0 微信打飞机--005

所有微信打飞机系列学习教程来自     http://blog.csdn.net/column/details/jackyairplane.html   所以游戏里面的介绍,资源什么的请看原博客,戳链接就可以啊。


飞机发射子弹要注意的几点是:

1.子弹的渲染效率

2.子弹的初始位置和飞行效果

3.子弹的回收

4.子弹层提供的接口

本文先讲解子弹的渲染效率问题。看两个很典型的例子,将1000个icon图精灵加入游戏。(左下角三个值从上到下分别是精灵数,渲染每帧所需要的时间,帧数)


1.普通渲染

(1)示例

[cpp] view plaincopy
  1. for(int i = 0;i < 1000;++i){  
  2.  int x = arc4random()%960;  
  3.  int y = arc4random()%640;  
  4.  CCSprite* testIcon = CCSprite::create("Icon.png");  
  5.  testIcon->setPosition( ccp(x, y) );  
  6.  this->addChild(testIcon);  
  7. }  
(2)效果



2.批次渲染

(1)示例

[cpp] view plaincopy
  1. CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("Icon.png", 1000);  
  2. batchNode->setPosition(CCPointZero);  
  3. this->addChild(batchNode);  
  4.    
  5. for(int i = 0;i < 1000;++i){  
  6.  int x = arc4random()%960;  
  7.  int y = arc4random()%640;  
  8.  CCSprite* testIcon = CCSprite::createWithTexture(batchNode->getTexture());  
  9.  testIcon->setPosition( ccp(x, y) );  
  10.  batchNode->addChild(testIcon);  
  11. }  
(2)效果图



3.渲染机制

从左下角的渲染次数(第一行)和渲染FPS(第三行),我们就可以看出,普通渲染需要进行1000次,而批次渲染只要1次就可以完成。差别在哪?CCSpriteBatchNode这个精灵批次渲染类。

普通渲染机制:

[cpp] view plaincopy
  1. 准备,渲染,清除。准备,渲染,清除。...准备,渲染,清除。100次啊100次!!!  
批次渲染机制:

[cpp] view plaincopy
  1. 准备,渲染,渲染....渲染,清除。是不是快多了?  
但必须注意的是,使用CCSpriteBatchNode,所有的精灵必须是同一张图,也不能指定精灵的深度,所有精灵必须在同一渲染层。


4.子弹的添加

子弹的添加其实就是使用CCSpriteBatchNode的最佳例子。


#ifndef __PlayAir__BulletLayer__#define __PlayAir__BulletLayer__#include <iostream>#include "cocos2d.h"class BulletLayer : public cocos2d::Layer{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();        // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();        // implement the "static create()" method manually    CREATE_FUNC(BulletLayer);        void AddBullet(float dt);        cocos2d::SpriteBatchNode*  bulletBatchNode;};#endif /* defined(__PlayAir__BulletLayer__) */

#include "BulletLayer.h"USING_NS_CC;Scene* BulletLayer::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = BulletLayer::create();        // add layer as a child to scene    scene->addChild(layer);        // return the scene    return scene;}// on "init" you need to initialize your instancebool BulletLayer::init(){    bool bRet = false;    do    {        CC_BREAK_IF(!Layer::init());                bulletBatchNode = SpriteBatchNode::create("ui/shoot.png"); //bulletBatchNode为CCSpriteBatchNode类型成员变量                this->addChild(bulletBatchNode);        //        this->schedule(schedule_selector(BulletLayer::AddBullet),0.01f);        bRet=true;    } while (0);    return bRet;}void BulletLayer::AddBullet(float dt){    Sprite* bullet=Sprite::create("bullet1.png");    bulletBatchNode->addChild(bullet);//这里子弹要添加到bulletBatchNode中,效果如下左图    //this->addChild(bullet);换成这句渲染批次和FPS,如下右图}

我们这里调用

[cpp] view plaincopy
  1. this->schedule(schedule_selector(BulletLayer::AddBullet),0.01f);  

看一下效果,因为间隔时间0.01s,所以子弹看起来是柱状的,好丑。。。看一下,渲染批次和FPS的比较:一个是4(有其他精灵),另一个则是181。如果你发现子弹只是在左下角而且精灵数不对,淡定。。。这里子弹已经做了回收处理,而且子弹的初始位置和移动都设置好了。


今天没有出来子弹啊 什么情况  一会再调。




0 0
原创粉丝点击