您的位置:首页 > 移动开发 > Cocos引擎

02 Sprite 图片精灵

2014-08-14 11:29 155 查看
这是C++里面的接口

static Sprite* create(const std::string& filename);
static Sprite* create(const std::string& filename, const Rect& rect);
static Sprite* createWithTexture(Texture2D *texture);
static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);
static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);


1、直接创建

local backImg = cc.Sprite:create("image/0.png");
--cc.rect(float,float,float,float) 表示图片的指定范围,即从图片的指定矩形区域裁剪(原点为图片左上角)
--local backImg = cc.Sprite:create("image/0.png",cc.rect(0,0,480,320));
backImg:setPosition(480,320);
self:addChild(backImg);


2、纹理创建

--纹理:纹理就是一块内存,这块内存中存放的是按照指定的像素格式填充的图片像素信息
--CCTexture2D:纹理,即图片加载入内存后供CPU和GPU操作的贴图对象。
--CCTextureCache:纹理管理器,负责加载图片并对生成的纹理进行管理。通过“字典”来进行快速的查询。

--创建:
--纹理管理器实例化对象,加载图片,返回图像   如果纹理里面已经有这张图像,则不再次加载,而是直接返回图像
--var cache = cc.TextureCache.getInstance().addImage("res/1001.png");--3.x版本被遗弃方法

local cache = cc.Director:getInstance():getTextureCache():addImage("image/0.png");
local img = cc.Sprite:createWithTexture(cache);
img:setPosition(480,320);
self:addChild(img);


3、精灵帧创建  CCSpriteFrameCache:精灵帧缓存

--①
--createWithSpriteFrameName	根据帧缓存中一帧的名称创建
local cache = cc.SpriteFrameCache:getInstance();
cache:addSpriteFrames("image/ball.plist");
local img = cc.Sprite:createWithSpriteFrameName("11.png");
img:setPosition(480,320);
self:addChild(img);

--②
--createWithSpriteFrame		根据精灵帧创建
--getSpriteFrame  返回的是一个 CCSpriteFrame(精灵帧),即是一张图
--在cocos2dx中,getSpriteFrame的原本接口是 spriteFrameByName
local cache = cc.SpriteFrameCache:getInstance();
cache:addSpriteFrames("image/ball.plist");
local frame = cache:getSpriteFrame("11.png");
local img = cc.Sprite:createWithSpriteFrame(frame);
img:setPosition(480,320);
self:addChild(img);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Cocos2d-x Lua Sprite 图片