标签:
创建精灵类的6种方法:
1、最常用的方法
//获取屏幕大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite *sp1 = CCSprite::create("icon.png");
sp1->setPosition(size.width*0.2, size.height*0.7);
this->addChild(sp1);
2、
CCSprite *sp2 = CCSprite::create("icon.png", CCRect(28, 28, 29, 29));
sp2->setPosition(ccp(size.width*0.4, size.height*0.7));
this->addChild(sp2);
3、
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("icon.png");//创建2D纹理
CCSprite *sp3 = CCSprite::createWithTexture(texture);
sp3 -> setPosition(ccp(size.width*0.8, size.height*0.7));
this->addChild(sp3);
4、
CCSprite *sp4 = CCSprite::createWithTexture(texture, CCRect(0, 0, 40, 40));
sp4->setPosition(ccp(size.width*0.4, size.height*0.8));
this->addChild(sp4);
5、
//利用缓存帧来创建精灵对象,帧动画中比较常用
CCSpriteFrame *frame = CCSpriteFrame::create("icon.png", CCRect(0, 0, 57, 57));
CCSprite *sp5 = CCSprite::createWithSpriteFrame(frame);
sp5->setPosition(ccp(size.width*0.3, size.height*0.3));
this->addChild(sp5);
6、
//另外一种利用缓存帧来创建精灵对象,
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.plist");
CCSprite *sp6 = CCSprite::createWithSpriteFrameName("icon.png");
sp6->setPosition(ccp(size.width*0.7, size.height*0.3));
this->addChild(sp6);
【sprite常用函数】
setScale(float fScale) 缩放
setScaleX(float fScaleX) 在X轴上缩放
setScaleY(float fScaleY) 在Y轴上缩放
setRotation(float fRotation) 进行旋转,正数是顺时针旋转,负数为逆时针
setRotationX(float fRotationX)
setRotationY(float fRotationY)
setSkewX(float sx) 倾斜
setSkewY(float sy)同上
setAnchorpoin(const CCPoint &anchor);
setSkewX(bool bVisible) 可见度;
setColor(const ccColor3B &color3)
setOpacity() 透明度;
setDisplayFrame(CCSpriteFrame *pNewFrame)
sp5 -> setTexture(CCTextureCache::sharedTextureCache()->addImage("icon.png")) 更改图片
标签:
原文地址:http://www.cnblogs.com/linguoqiu/p/4815042.html