第六章精灵
来源:互联网 发布:算法分析的主要方面是 编辑:程序博客网 时间:2024/05/16 10:43
Cocos2d-x学习笔记
Sprite精灵类
Sprite是Node的子类,Sprite直接继承于Node类。
创建Sprite精灵对象
创建精灵对象有很多方式,其中常用的函数如下:
static Sprite * create ()
:创建一个精灵对象,纹理等属性需要在创建之后设置。static Sprite * create ( const std::string & filename )
:指定图片创建精灵。static Sprite * create ( const std::string & filename, const Rect & rect )
:指定图片和裁剪区域来创建精灵。static Sprite * creataWithTexture ( Texture2D * texture )
:指定纹理创建精灵。static Sprite * createWithTexture ( Textrue2D * texture, const Rect & rect, bool rotated = false )
:指定纹理和裁剪的矩形区域来创建精灵,第三个参数是否旋转纹理,默认不旋转。static Sprite * createWithSpriteFrame ( SpriteFrame * pSpriteFrame )
:通过一个精灵帧对象创建另一个精灵对象。static Sprite * createWithSpriteFrameName ( const std::stirng & spriteFrameName )
:通过指定帧缓存中精灵帧名创建精灵对象。
实例
HelloWorld.h文件
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback // implement the "static create()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp文件
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add you code auto background = Sprite::create("background.png"); background->setAnchorPoint(Vec2::ZERO);//背景设置锚点 this->addChild(background, 0);//第二个参数表示Z轴上的顺序 auto tree1 = Sprite::create("tree1.png", Rect(604, 38, 302, 295));//左上角点坐标+(宽,高) 注:UI坐标系 tree1->setPosition(Vec2(200, 230));//注意这里是GL坐标系 this->addChild(tree1, 0); //通过纹理缓存TextureCache创建纹理Texture2D对象,通过Director的getTextureCache()函数可以获得TextureCache实例,TextureCache的addImage()函数可以创建Texture2D对象。 Texture2D * cache = Director::getInstance()->getTextureCache()->addImage("tree1.png"); auto tree2 = Sprite::create(); tree2->setTexture(cache); tree2->setTextureRect(Rect(73, 72, 182, 270)); tree2->setPosition(Vec2(500, 200)); this->addChild(tree2, 0); return true;}
精灵的性能优化
游戏是一种很耗费资源的应用,特别是在移动设备中。精灵的性能优化可以使用精灵表和缓存。
使用纹理图集
纹理图集(Texture Atlas)也称为精灵表(Sprite Sheet),它是把许多晓得精灵图片组合成一张大图里面,其优点如下所述:
- 减少文件读取次数,读取一张图片比读取一堆小文件要快。
- 减少OpenGl ES调用并且加速渲染。
- 减少内存消耗。
- Cocos2d-x全面支持Zwoptex和TexturePacker,所以创建和使用纹理图集是很容易的。
使用精灵帧缓存
精灵帧缓存是缓存的一种,缓存有如下几种:
- 纹理缓存(TextureCache):使用纹理缓存可以创建纹理对象。
- 精灵帧缓存(SpriteFrameCache):能够从精灵表中创建精灵帧缓存,然后再从精灵帧缓存中获取精灵对象,反复使用精灵对象,使用精灵帧缓存可以节省内存消耗。
- 动画缓存(AnimationCache):动画缓存主要用于精灵动画,精灵动画中的每一帧是从动画缓存中获取得到的。
精灵帧缓存主要介绍精灵帧缓存(SpriteFrameCache),要使用精灵帧缓存涉及到类有:SpriteFrame和SpriteFrameCache
SpriteFrameCache 使用实例:
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("SpriteSheet.plist");auto mountain1 = Sprite::createWithSpriteFrameName("mountain1.png");
移除精灵帧缓存
void removeSpriteFrameByName( const std::string & name )
:指定具体的精灵帧名移除。void removeSpriteFrames()
:指定移除精灵缓存。void removeSpriteFramesFormFile(const std::stirng & plist)
:指定具体的坐标文件移除精灵帧。void removeUnusedSpriteFrames()
:移除没有使用的精灵帧。
实例
HelloWorld.h文件
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback virtual void onExit(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp文件
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// auto background = Sprite::create("background.png"); background->setAnchorPoint(Vec2::ZERO); this->addChild(background, 0); SpriteFrameCache * frameCache = SpriteFrameCache::getInstance(); frameCache->addSpriteFramesWithFile("SpirteSheet.plist"); auto mountain1 = Sprite::createWithSpriteFrameName("mountain1.png"); mountain1->setAnchorPoint(Vec2::ZERO); mountain1->setPosition(Vec2(-200, 80)); this->addChild(mountain1, 0); SpriteFrame * heroSpriteFrame = frameCache->getSpriteFrameByName("hero1.png"); Sprite *hero1 = Sprite::createWithSpriteFrame(heroSpriteFrame); hero1->setPosition(Vec2(800, 200)); this->addChild(hero1, 0); return true;}void HelloWorld::onExit(){ Layer::onExit(); SpriteFrameCache::getInstance()->removeSpriteFrames();}
0 0
- 第六章精灵
- 第三章:精灵
- 第六章第六题
- 精灵
- 精灵
- 精灵
- 精灵
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- 第六章
- javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String
- 洛谷 P1125 笨小猴
- easyui datagrid加载数据的两种方式
- python数据分析之(2)numpy
- 函数传参问题(C/C++)
- 第六章精灵
- HDU-1754-I hate it (线段树RMQ)
- 一个简单的jqery实现小说的文字大小、颜色及背景
- stm32 起步 gpio操作
- python
- 35、(知识篇)SpringMVC12 SpringMVC 超链接国际化
- java 企业网站源码模版 有前后台 springmvc SSM 生成静态化
- Visual Odometry(视觉里程计): StereoScan Dense 3d Reconstruction in Real-time
- pat 1009. Product of Polynomials (25)