sprite基本知识、[缩放,旋转,透明度,XY翻转,颜色设置,z值改变,换图]
1 CCSprite* sprite=[CCSprite spriteWithFile:@"Icon.png"];
2 [self addChild:sprite z:1 tag:100]; //z 值默认为0;
3 sprite.scale=0.8f;// 缩放;
4 sprite.rotation=90; // 旋转80度;
5 sprite.opacity=25; //透明度为25 [范围为0-255];
6 sprite.position=ccp(100, 100); //设置锚点坐标;
7 sprite.flipX=YES;
8 sprite.flipY=YES; //x,y翻转;
9 sprite.color=ccc3(255, 255, 0); //颜色设置;
10
1
2 CCSprite* backGround=[CCSprite spriteWithFile:@"背景.png"];
3 [self addChild:backGround];
4 backGround.position=ccp(200, 200);
5 CCSprite* windows=[CCSprite spriteWithFile:@"窗.png"]; //背景图盖窗;
6 [self addChild:windows z:1];
7 windows.position=ccp(250, 250);
8
9 [self reorderChild:backGround z:1]; //通过改变z值,窗盖背景;
10 [self reorderChild:windows z:0];
1 CCSprite*sprite1 =[CCSprite spriteWithFile:@"Icon.png"];
2 sprite1.position=ccp(400,150);
3 [self addChild:sprite1];
4
5 CCSprite*sprite2 =[CCSprite spriteWithFile:@"Icon.png"]; //没换图前;
6 sprite2.position=ccp(400,250);
7 [self addChild:sprite2];
8
9 CCTexture2D * texture =[[CCTextureCache sharedTextureCache] addImage: @"同学.png"]; //换图后
10 [sprite1 setTexture:texture]; //换图,[原精灵尺寸有多大,跟换的贴图就有多大]
11
12 CCSprite *back = [CCSprite spriteWithFile:@"同学.png"];
13 [sprite1 setDisplayFrame:[CCSpriteFrame frameWithTexture:back.texture rect:back.textureRect]]; //此换图方法,换图的sprite是多大,被跟换后的sprite就是多大;
![](https://pic002.cnblogs.com/images/2012/380035/2012031721045343.png)
1 //或者事先添加帧缓存;
2 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"btn_star_start.plist"];
3 //-----没有换贴图前
4 CCSprite*sprite4 =[CCSprite spriteWithFile:@"Icon.png"];
5 sprite4.position=ccp(50,50);
6 [self addChild:sprite4];
7 //-----换贴图后
8 CCSprite*sprite3 =[CCSprite spriteWithFile:@"Icon.png"];
9 sprite3.position=ccp(150,50);
10 [self addChild:sprite3];
11 //更换帧贴图
12 //从帧缓存中取出"main.png"
13 CCSpriteFrame* frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"main.png"];
14 [sprite3 setDisplayFrame:frame2];