cocos2d之精灵sprite的创建

CCSprite是最常用到的类。它使用图片把精灵(sprite)显示在屏幕上,精灵的创建(3种方法):


//1、直接通过CCSprite创建精灵        

CCSprite *sprit = [CCSprite spriteWithFile:@"1.jpg"];

CGSize size = [[CCDirector sharedDirector] winSize];

sprit.position = ccp(size.width, size.height);

sprit.anchorPoint = ccp(1, 1);

[self addChild:sprit];


//2、使用texture创建精灵、存到缓存中

CCTexture2D * tx = [[CCTextureCache sharedTextureCache] addImage:@"1.jpg"];

CCSprite * txSprite =[CCSprite spriteWithTexture:tx];

CGSize size = [[CCDirector sharedDirector] winSize];

txSprite.position = ccp(size.width/4,size.height/4);

[self addChild:txSprite];

//从缓存中取得图片,节省内存   

CCTexture2D * tx2 = [[CCTextureCache sharedTextureCache] textureForKey:@"1.jpg"];

CCSprite * txSprite2 =[CCSprite spriteWithTexture:tx2];

txSprite2.position = ccp(size.width*3/4,size.height/4);

[self addChild:txSprite2];


//3、从贴图集中取得图片

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Untitled_default.plist"];

CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"5.jpg"];

CGSize size = [[CCDirector sharedDirector] winSize];

sprite.position = ccp(size.width/4,size.height/4);

[self addChild:sprite];






你可能感兴趣的:(cocos2d之精灵sprite的创建)