cocos2d-lua cc.Sprite api
2016-12-16 12:36
274 查看
1. 普通创建
var sprite = new cc.Sprite(“从loader加载的资源名称”);
2. 设置中点
sprite. setAnchorPoint(0~1, 0~1); // x、y的坐标
或者:
sprite.setAnchorPoint(cc.p(x, y)); // 同上
3. x/y坐标(图片中点的坐标)
sprite.setPosition(x, y);
或者
sprite.setPosition(cc.p(x, y));
4. 缩放
sprite. setScale(0.5); // 变为原本的0.5
或
sprite.setScale(0.5, 1); // x轴缩小到0.5,y轴保持不变
5. 图片翻转
sprite.flippedX = true; // X轴翻转
sprite.flippedY = true; // Y轴翻转
6. 设置透明
sprite.setOpacity (175); // 透明度 0 ~255,0是全透明
7. 颜色过滤效果
sprite.setColor(cc.color(0,255,255, 255)); // 蓝色过滤效果
8. 获取图片的box信息
var box = sprite.getBoundingBox(); // 包含x/y/width/height
9. 读取图片的某一片段
sprite.setTextureRect(cc.rect(x, y, width, height));//cc.rect的起点在左下角
// 从图片的(x,y)点开始,截取width、height部分显示
或者:
varsprite = new cc.Sprite(“res.png”, cc.rect(x,y, w, h)); // 解析同上
10. 更换图片资源
Sprite.setTexture(“更换的图片名称”); // 注意,如果设置过textureRect,可能因为图片坐标、大小变化,而看不见新的图片
或:
// 很麻烦的说~
var texture = newcc.Texture2D();
texture.initWithElement(img);//img 是cc.loader.loadImg的 img
texture.handleLoadedTexture();
sprite.setTexture(texture);
或:
// 在setTexure的源码里,有这么一种写法
// 就是不知道addImage的图片,是不是一定要先经过cc.loader的了~
var texture = cc.textureCache.addImage(“图片名称”);
sprite.setTexture(texture);
// 后边可不要~
var size = texture.getContentSize();
sprite.setTextureRect(cc.rect(0,0,size.width, size.height));
11. 重设zIndex
parent. reorderChild(sprite, zIndex); // 找到父亲,重新设置zIndex
var sprite = new cc.Sprite(“从loader加载的资源名称”);
2. 设置中点
sprite. setAnchorPoint(0~1, 0~1); // x、y的坐标
或者:
sprite.setAnchorPoint(cc.p(x, y)); // 同上
3. x/y坐标(图片中点的坐标)
sprite.setPosition(x, y);
或者
sprite.setPosition(cc.p(x, y));
4. 缩放
sprite. setScale(0.5); // 变为原本的0.5
或
sprite.setScale(0.5, 1); // x轴缩小到0.5,y轴保持不变
5. 图片翻转
sprite.flippedX = true; // X轴翻转
sprite.flippedY = true; // Y轴翻转
6. 设置透明
sprite.setOpacity (175); // 透明度 0 ~255,0是全透明
7. 颜色过滤效果
sprite.setColor(cc.color(0,255,255, 255)); // 蓝色过滤效果
8. 获取图片的box信息
var box = sprite.getBoundingBox(); // 包含x/y/width/height
9. 读取图片的某一片段
sprite.setTextureRect(cc.rect(x, y, width, height));//cc.rect的起点在左下角
// 从图片的(x,y)点开始,截取width、height部分显示
或者:
varsprite = new cc.Sprite(“res.png”, cc.rect(x,y, w, h)); // 解析同上
10. 更换图片资源
Sprite.setTexture(“更换的图片名称”); // 注意,如果设置过textureRect,可能因为图片坐标、大小变化,而看不见新的图片
或:
// 很麻烦的说~
var texture = newcc.Texture2D();
texture.initWithElement(img);//img 是cc.loader.loadImg的 img
texture.handleLoadedTexture();
sprite.setTexture(texture);
或:
// 在setTexure的源码里,有这么一种写法
// 就是不知道addImage的图片,是不是一定要先经过cc.loader的了~
var texture = cc.textureCache.addImage(“图片名称”);
sprite.setTexture(texture);
// 后边可不要~
var size = texture.getContentSize();
sprite.setTextureRect(cc.rect(0,0,size.width, size.height));
11. 重设zIndex
parent. reorderChild(sprite, zIndex); // 找到父亲,重新设置zIndex
相关文章推荐
- cocos2d lua绑定感悟---像cc.Sprite,cc.Director这些是如何识别的
- cocos2d-x-lua:API-sprite
- cocos2d-x TestLua 之 CCMenuItemLabel CCMenuItemSprite CCMenuItemFont 菜单类
- Cocos2d-js 学习(二): cc.Sprite 简单API记录
- cocos2d-x TestLua 之 CCMenuItemLabel CCMenuItemSprite CCMenuItemFont 菜单类
- cocos2d-X 节点(CCNode.h)API
- cocos2d-x + Lua的cc.Repeat动画执行次数不准,巨坑
- cocos2d-x节点(CCShaderCache.h)API
- cocos2d-x节点(CCImageCommon_cpp.h)API
- cocos2d-x节点(CCPhysicsWorld.h)API
- cocos2d-x节点(CCDouble.h)API
- Cocos2d-x CCMenuItemSprite按钮图片旋转问题
- cocos2d-x 之 CCPhysicsSprite 基于物理引擎的Sprite
- cocos2d-x 之 CCPhysicsSprite 基于物理引擎的Sprite
- cocos2d-X 节点(CCMotionStreak.h)API
- cocos2d-x CCmenuSpriteItem 用getChildByTag()获取;
- cocos2d-x节点(CCPlatformConfig.h)API
- cocos2d-x节点(CCPhysicsSetting.h)API
- COcos2d-X 节点(CCGLBufferedNode.h)API
- lua教程八----不用CCHttpClient而转用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!