序言
回顾cocos2dx,之前在mac+Xcode平台学习了一遍cocos2dx,一年时间不接触cocos了,一直在搞Unity3d,现在还是就之前所学温故温故,但不再用Xcode来写,用常用的Windows+VS2012做为开发环境来回顾cocos2dx。从CCSprite精灵类开始回顾起。
CCSprite多种创建方式
创建CCSprite精灵类有好多中方式,好多构造函数,这里列举集中创建形式,包括要显示某个部分和缓存帧,2D贴图创建和普通创建等。
if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //加载精灵 //方式一 CCSprite *sprite = CCSprite::create("icon.png"); //123px*123px的图片 sprite->setPosition(ccp(size.width*0.2,size.height*0.7)); this->addChild(sprite); //方式二 //设置显示区域 CCSprite *sprite1 = CCSprite::create("icon.png",CCRect(0,0,60,60)); //只取60px*60px的图片 sprite1->setPosition(ccp(size.width*0.5,size.height*0.7)); this->addChild(sprite1); //方式三 //创建2d文理来创建精灵 CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("icon.png"); CCSprite *sprite2 = CCSprite::createWithTexture(texture); sprite2->setPosition(ccp(size.width*0.8,size.height*0.7)); this->addChild(sprite2); //方式四 //还是设置显示区域 CCSprite *sprite3 = CCSprite::createWithTexture(texture,CCRect(61.5,61.5,61.5,61.5)); sprite3->setPosition(ccp(size.width*0.2,size.height*0.3)); this->addChild(sprite3); //方式五 //缓存帧 CCSpriteFrame *frame = CCSpriteFrame::create("icon.png",CCRect(0,0,61.5,61.5)); CCSprite *sprite4 = CCSprite::createWithSpriteFrame(frame); sprite4->setPosition(ccp(size.width*0.5,size.height*0.3)); this->addChild(sprite4); //方式六 /*CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.plist"); CCSprite *sprite5 = CCSprite::createWithSpriteFrameName("icon.png"); sprite5->setPosition(ccp(size.width*0.7,size.height*0.3)); this->addChild(sprite5);*/
CCSprite缩小和放大
CCSprite->SetScale():参数介于0~无穷,就可实现精灵的放大和缩小功能。
//精灵的缩小和放大 CCSprite *spriteS= CCSprite::create("icon.png"); //123px*123px的图片 spriteS->setPosition(ccp(size.width*0.2,size.height*0.5)); spriteS->setScale(0.5f); this->addChild(spriteS); CCSprite *spriteM = CCSprite::create("icon.png"); //123px*123px的图片 spriteM->setPosition(ccp(size.width*0.7,size.height*0.5)); spriteM->setScale(1.5f); this->addChild(spriteM);
SetScaleX,SetScaleY是只针对X、Y方向的缩放
例如:sprite->setScaleX(1.5f); sprite2->setScaleY(1.5f);
CCSprite旋转
CCSprite->setRotation();参数是度数,正数代表顺时针旋转多少度,负数代表逆时针旋转多少度。//精灵的旋转 CCSprite *spriteL= CCSprite::create("icon.png"); //123px*123px的图片 spriteL->setPosition(ccp(size.width*0.3,size.height*0.5)); spriteL->setRotation(30.0f); this->addChild(spriteL); CCSprite *spriteR= CCSprite::create("icon.png"); //123px*123px的图片 spriteR->setPosition(ccp(size.width*0.8,size.height*0.5)); spriteR->setRotation(-30.0f); this->addChild(spriteR);
CCSprite倾斜
CCSprite->setSkewY();参数是度数,正数代表顺时针倾斜多少度,负数代表逆时针倾斜多少度。
//精灵的倾斜 CCSprite *spriteL= CCSprite::create("icon.png"); //123px*123px的图片 spriteL->setPosition(ccp(size.width*0.3,size.height*0.5)); spriteL->setSkewY(30.0f); this->addChild(spriteL); CCSprite *spriteR= CCSprite::create("icon.png"); //123px*123px的图片 spriteR->setPosition(ccp(size.width*0.8,size.height*0.5)); spriteR->setSkewY(-30.0f); this->addChild(spriteR);
CCSprite设置不可见和变色
//精灵可见和变色 //设置不可见 CCSprite *spriteL= CCSprite::create("icon.png"); //123px*123px的图片 spriteL->setPosition(ccp(size.width*0.3,size.height*0.5)); spriteL->setVisible(false); this->addChild(spriteL); //变成红色 CCSprite *spriteR= CCSprite::create("icon.png"); //123px*123px的图片 spriteR->setPosition(ccp(size.width*0.8,size.height*0.5)); spriteR->setColor(ccc3(255,0,0)); this->addChild(spriteR);
CCSprite设置透明度
//设置透明度 CCSprite *spriteL= CCSprite::create("icon.png"); //123px*123px的图片 spriteL->setPosition(ccp(size.width*0.3,size.height*0.5)); spriteL->setOpacity(100);//0~255 this->addChild(spriteL); CCSprite *spriteR= CCSprite::create("icon.png"); //123px*123px的图片 spriteR->setPosition(ccp(size.width*0.7,size.height*0.5)); spriteR->setOpacity(230);//0~255 this->addChild(spriteR);
CCSprite替换图片
//替换图片 CCSprite *spriteR= CCSprite::create("icon.png"); //123px*123px的图片 spriteR->setPosition(ccp(size.width*0.7,size.height*0.5)); spriteR->setOpacity(230);//0~255 this->addChild(spriteR); spriteR->setTexture(CCTextureCache::sharedTextureCache()->addImage("CloseNormal.png"));
欢迎关注我的围脖
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:375151422 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================