02 Sprite 图片精灵

来源:互联网 发布:2016it行业发展前景 编辑:程序博客网 时间:2024/06/11 11:07

这是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的原本接口是 spriteFrameByNamelocal 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);

0 0
原创粉丝点击