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)示例
- 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);
- }
2.批次渲染
(1)示例
- 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);
- }
3.渲染机制
从左下角的渲染次数(第一行)和渲染FPS(第三行),我们就可以看出,普通渲染需要进行1000次,而批次渲染只要1次就可以完成。差别在哪?CCSpriteBatchNode这个精灵批次渲染类。
普通渲染机制:
- 准备,渲染,清除。准备,渲染,清除。...准备,渲染,清除。100次啊100次!!!
- 准备,渲染,渲染....渲染,清除。是不是快多了?
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,如下右图}
我们这里调用
- this->schedule(schedule_selector(BulletLayer::AddBullet),0.01f);
看一下效果,因为间隔时间0.01s,所以子弹看起来是柱状的,好丑。。。看一下,渲染批次和FPS的比较:一个是4(有其他精灵),另一个则是181。如果你发现子弹只是在左下角而且精灵数不对,淡定。。。这里子弹已经做了回收处理,而且子弹的初始位置和移动都设置好了。
今天没有出来子弹啊 什么情况 一会再调。
0 0
- cocos2dx 3.0 微信打飞机--005
- cocos2dx 3.0 微信打飞机--001
- cocos2dx 3.0 微信打飞机--002
- cocos2dx 3.0 微信打飞机--003
- cocos2dx 3.0 微信打飞机--004
- cocos2dx 3.0 飞机大战
- cocos2dx+KinectV2 体感游戏之微信打飞机
- cocos2dx飞机大战Cocos2d-x飞机实现
- cocos2dx飞机大战游戏代码
- cocos2dx实现经典飞机大战
- cocos2dx飞机大战教程Cocos2d-x飞机大战运行效果
- :微信打飞机
- 微信打飞机
- cocos2dx一仿微信飞机大战游戏实战一
- cocos2dx一仿微信飞机大战游戏实战二
- cocos2dx 3.2之Lua打飞机项目
- cocos2dx打飞机游戏(一)
- 使用3.0实现微信打飞机——3.添加玩家飞机
- 字典学习-网友答疑
- poj 2955 Brackets(区间DP,经典问题)求有规律的括号的最大长度
- Linux系统内存消失与slab使用之谜
- 开源中国
- Linux系统修改编码
- cocos2dx 3.0 微信打飞机--005
- 字符串指针。
- Managing Your App's Memory
- 北上广打拼的游子,为何远离亲人,仍义无反顾?
- 金立刘总
- Hibernate OneToOne注解配置
- Android 实现ListView的弹性效果
- LaTeX新人教程,30分钟从完全陌生到基本入门
- 自考-信息资源管理