CCSprite精灵
一.创建CCSprite常用的4种方法:
1.CCSprite *sp1 = CCSprite :: create(const char* fileName);
作用:通过一张图片生成精灵(CCSprite)类;
参数:fileName为图片的名称,图片放置在工程根目录Resources文件夹下。
2.CCSprite *sp2 = CCSprite :: create(const char *fileName, CCRect &rect);
作用:通过一张图片进行指定区域的裁剪之后,生成精灵对象;
参数一:图片名称;
参数二:矩形区域。
【说明】CCRect是等同于使用CCRectMake(flota x, float y, float width, float height)。
其中x,y,width以及height如下图所示:
需要注意的是,CCSprite对象通过上面的方法进行创建之后,精灵的锚点是经过裁剪之后的图片的锚点,创建sp1之后,再进行sp1->setAnchorPoint(ccp(0,0));之后,sp1的锚点在A,而不是在B。
3.CCSprite *sp3 = CCSprite :: createWithFrameName(const char* frameName);
作用:利用帧缓存中一帧的名称创建一个CCSprite对象;
参数:帧名称。
举例:
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrameWithFile(“weapon.plist”);
CCSprite *sp3 = CCSprite :: createWithSpriteFrameName(“weapon_f-knife.png”);
sp3-<setAnchorPoint(ccp(0,0));
sp3->setPosition(ccp(10,10));
This->addChild(sp3);
4.CCSprite *sp4 = CCSprite::createWithSpriteFrame(CCSpriteFrame *frame);
作用:利用另外一帧创建一个精灵;
参数:精灵对象。
举例:
CCSpriteFrame *frame = CCSpriteFrame :: create(“Icon.png”,CCRect(10,10,35,35));
CCSprite *sp4 = CCSprite :: createWithSpriteFrame(frame);
sp4->setAnchorPoint(ccp(0,0));
sp4->setPosition(ccp(10,10));
this->addChild(sp4);
二.CCSprite常用的函数
CCSprite * sp = CCSprite :: create(“Icon.png”);
1.sp->setPosition(CCPoint :: & var)
作用:设置精灵的坐标;
参数:CCPonit类型,可是使用宏ccp(float x, float y)快捷创建。
2.sp->setRotation(float fRotation)
作用:设置旋转角度;
参数:float类型,旋转角度的范围是[0,360]。
3.sp->setScale(float scale)
作用:设置缩放比;
参数:缩放比例,当缩放值在(0,1)之间为缩小,范围大于1为放大。
说明:setScaleX(float scaleX)只对X轴进行缩放,setScaleY(float scaleY)只对Y轴进行缩放。
4.sp->setFlipX(bool bFlipX)
作用:设置X轴镜像翻转。
参数:bool类型,bFlipX = true进行X轴镜像翻转。
说明:setFlipY(bool bFlipY)用来设置对精灵进行Y轴镜像操作。
5.sp->setOpacity(GLubyte var)
作用:设置透明度;
参数:GLubyte类型,透明度值的范围在[0, 255]。var = 0为全透明,var = 255为不透明。
说明:sp->setOpacity(127) 设置sp精灵为半透明。
6.sp->setVisible(bool visible)
作用:设置是否可见;
参数:bool类型,visible = false为不可见,默认为true。
7.sp->setAnchorPoint(CCPoint & anchor)
作用:设置锚点;
参数:默认的锚点在精灵的中心。
8.sp->setColor(const ccColor3B &var)
作用:设置颜色;
参数:ccColor3B类型等用于使用宏ccc3(const GLubyte r, const GLubyte g, const GLubyte b)进行快捷创建。
9.sp->setTexture(CCTexture2D *texture)
作用:设置贴图;
参数:CCTexture类型。
10.sp->setTextureRect(const CCRect &rect)
作用:设置贴图的大小;
参数:CCRect类型等同于使用CCRectMake(float x, float y, float width, float height)进行创建。