Sprite(精灵)&& 三个特殊的层Layer
用来作为以后复习使用。
1 #include "ScenceScend.h" 2 3 CCScene* ScenceScend::scene() 4 { 5 CCScene* s = CCScene::create(); 6 ScenceScend* layer = ScenceScend::create(); 7 s->addChild(layer); 8 return s; 9 10 } 11 bool ScenceScend::init() 12 { 13 CCLayer::init(); 14 //1.通过文件直接创建精灵 15 //CCSprite* sprite = CCSprite::create(); 16 //CCSprite* sprite = CCSprite::create(filename,CCRect); 17 //CCSprite* sprite = CCSprite::create(filename); 18 19 20 //2.通过纹理去创建精灵 21 22 //CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("filename"); 23 //CCSprite* sprite = CCSprite::createWithTexture(texture); 24 //CCSprite* sprite = CCSprite::createWithTexture(texture,CCRect);也可取图片局部 25 //addChild(sprite); 26 //3通过精灵帧来创建 27 /* 28 CCSize size = CCDirector::sharedDirector()->getWinSize(); 29 30 CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png"); 31 CCSize size2 = texture->getContentSize(); 32 CCSpriteFrame* frame = 33 CCSpriteFrame::createWithTexture(texture, CCRectMake(size2.height / 4, size2.width / 4, size2.width / 2, size2.width / 2)); 34 CCSprite* sprite = CCSprite::createWithSpriteFrame(frame); 35 sprite->setPosition(size.width/2,size.height/2); 36 addChild(sprite); 37 */ 38 39 //4.通过精灵帧缓存 去创建精灵 40 41 CCSize size = CCDirector::getInstance()->getWinSize(); 42 //CCTexture2D 贴图类 43 CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png"); 44 CCSize size2 = texture->getContentSize(); 45 CCSpriteFrame* frame = 46 CCSpriteFrame::createWithTexture(texture, CCRectMake(size2.height / 4, size2.width / 4, size2.width / 2, size2.width / 2)); 47 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(frame,"HXL"); 48 CCSprite* sprite = CCSprite::createWithSpriteFrameName("HXL"); 49 sprite->setPosition(size.width / 2, size.height / 2); 50 addChild(sprite); 51 52 /* 53 //调用父类的初始化。因此这里是 子类方法覆盖父类了。 54 CCLayer::init(); 55 CCLabelTTF *label = CCLabelTTF::create("hxltom","Arial",36); 56 label->setPosition(CCPoint(100,200)); 57 addChild(label); 58 */ 59 60 return true; 61 }
三个特殊的层。
1 #include "example2.h" 2 3 CCScene* example2::scene() 4 { 5 CCScene* s = CCScene::create(); 6 example2* layer = example2::create(); 7 s->addChild(layer); 8 return s; 9 10 } 11 bool example2::init() 12 { 13 //CCLayerColor::init(); 14 CCLayerColor::initWithColor(ccc4(255,0,0,255)); 15 //CCLayerColor::init();默认也是黑色 16 17 18 //三个特殊的层。 19 /* 20 1.CCLayerColor 可改变layer的背景,设置层的大小 21 2.CCLayerGradient 有渐变效果的层 22 3.CCLayerMultiplex 23 24 CCLayerColor::create(ccColor4B()); 25 ccColor4B()这个类封装了颜色,里面有4个参数 r g b o o代表透明度 26 CCLayerColor::create(ccc4(255,0,0,255)); 红,绿,蓝,透明度 27 28 CCLayerGradient 29 */ 30 31 #if 0 32 //CCLayerColor !!!!!!!!!!!! 33 CCSize size = CCDirector::getInstance()->getWinSize(); 34 CCLayerColor* layer = CCLayerColor::create( 35 ccc4(200,200,200,255),size.width/2,size.height/2 36 ); 37 addChild(layer); 38 #endif 39 40 #if 0 41 //CCLayerGradient 有渐变效果的层,透明度也可以渐变,渐变方向(第三个参数) 42 //渐变默认从上往下。cp(0,1)从底下往上渐变 cp(1,1)左下角到右上角 43 //CCLayerGradient* layer = CCLayerGradient::create(ccc4(255,0,0,255),ccc4(255,0,255,255)); 44 //一开始设置渐变方向 45 CCLayerGradient* layer = CCLayerGradient::create( 46 ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255), 47 ccp(0,1) 48 ); 49 /* 50 //生成之后改变渐变方向 (两种方式等价的) 51 CCLayerGradient* layer = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255)); 52 layer->setVector(ccp(0,1)); 53 addChild(layer); 54 */ 55 56 #endif 57 58 //CCLayerMultiplex可以包含好几个层,但是只是显示其中一个 59 CCSize size = CCDirector::getInstance()->getWinSize(); 60 CCLayerColor* layer1 = CCLayerColor::create(ccc4(255,255,0,255),size.width/2,size.height/2); 61 CCLayerGradient* layer2 = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255)); 62 CCLayerMultiplex* layer = CCLayerMultiplex::create(layer1, layer2, NULL); 63 64 layer->switchTo(0);//设置显示第一个 65 addChild(layer); 66 67 setTouchEnabled(true); 68 setTouchMode(kCCTouchesOneByOne); 69 return true; 70 }