用来作为以后复习使用。

 1 #include "ScenceScend.h"

 CCScene* ScenceScend::scene()
{
CCScene* s = CCScene::create();
ScenceScend* layer = ScenceScend::create();
s->addChild(layer);
return s; }
bool ScenceScend::init()
{
CCLayer::init();
//1.通过文件直接创建精灵
//CCSprite* sprite = CCSprite::create();
//CCSprite* sprite = CCSprite::create(filename,CCRect);
//CCSprite* sprite = CCSprite::create(filename); //2.通过纹理去创建精灵 //CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("filename");
//CCSprite* sprite = CCSprite::createWithTexture(texture);
//CCSprite* sprite = CCSprite::createWithTexture(texture,CCRect);也可取图片局部
//addChild(sprite);
//3通过精灵帧来创建
/*
CCSize size = CCDirector::sharedDirector()->getWinSize(); CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png");
CCSize size2 = texture->getContentSize();
CCSpriteFrame* frame =
CCSpriteFrame::createWithTexture(texture, CCRectMake(size2.height / 4, size2.width / 4, size2.width / 2, size2.width / 2));
CCSprite* sprite = CCSprite::createWithSpriteFrame(frame);
sprite->setPosition(size.width/2,size.height/2);
addChild(sprite);
*/ //4.通过精灵帧缓存 去创建精灵 CCSize size = CCDirector::getInstance()->getWinSize();
//CCTexture2D 贴图类
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png");
CCSize size2 = texture->getContentSize();
CCSpriteFrame* frame =
CCSpriteFrame::createWithTexture(texture, CCRectMake(size2.height / , size2.width / , size2.width / , size2.width / ));
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(frame,"HXL");
CCSprite* sprite = CCSprite::createWithSpriteFrameName("HXL");
sprite->setPosition(size.width / , size.height / );
addChild(sprite); /*
//调用父类的初始化。因此这里是 子类方法覆盖父类了。
CCLayer::init();
CCLabelTTF *label = CCLabelTTF::create("hxltom","Arial",36);
label->setPosition(CCPoint(100,200));
addChild(label);
*/ return true;
}

三个特殊的层。

 #include "example2.h"

 CCScene* example2::scene()
{
CCScene* s = CCScene::create();
example2* layer = example2::create();
s->addChild(layer);
return s; }
bool example2::init()
{
//CCLayerColor::init();
CCLayerColor::initWithColor(ccc4(,,,));
//CCLayerColor::init();默认也是黑色 //三个特殊的层。
/*
1.CCLayerColor 可改变layer的背景,设置层的大小
2.CCLayerGradient 有渐变效果的层
3.CCLayerMultiplex CCLayerColor::create(ccColor4B());
ccColor4B()这个类封装了颜色,里面有4个参数 r g b o o代表透明度
CCLayerColor::create(ccc4(255,0,0,255)); 红,绿,蓝,透明度 CCLayerGradient
*/ #if 0
//CCLayerColor !!!!!!!!!!!!
CCSize size = CCDirector::getInstance()->getWinSize();
CCLayerColor* layer = CCLayerColor::create(
ccc4(,,,),size.width/,size.height/
);
addChild(layer);
#endif #if 0
//CCLayerGradient 有渐变效果的层,透明度也可以渐变,渐变方向(第三个参数)
//渐变默认从上往下。cp(0,1)从底下往上渐变 cp(1,1)左下角到右上角
//CCLayerGradient* layer = CCLayerGradient::create(ccc4(255,0,0,255),ccc4(255,0,255,255));
//一开始设置渐变方向
CCLayerGradient* layer = CCLayerGradient::create(
ccc4(, , , ), ccc4(, , , ),
ccp(,)
);
/*
//生成之后改变渐变方向 (两种方式等价的)
CCLayerGradient* layer = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255));
layer->setVector(ccp(0,1));
addChild(layer);
*/ #endif //CCLayerMultiplex可以包含好几个层,但是只是显示其中一个
CCSize size = CCDirector::getInstance()->getWinSize();
CCLayerColor* layer1 = CCLayerColor::create(ccc4(,,,),size.width/,size.height/);
CCLayerGradient* layer2 = CCLayerGradient::create(ccc4(, , , ), ccc4(, , , ));
CCLayerMultiplex* layer = CCLayerMultiplex::create(layer1, layer2, NULL); layer->switchTo();//设置显示第一个
addChild(layer); setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne);
return true;
}
05-07 15:42