【iOS-Cocos2d游戏开发之二十】贴图更换

转载自 【黑米GameDev街区】 原文链接: http://www.himigame.com/iphone-cocos2d/516.html

 

原文标题为【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)以及利用CCSprite与CCLayerColor制作简单遮盖层!

原文过于简单,这里摘取其中自己不熟悉的一部分与大家分享。

 

2.如何更换已创建的精灵贴图?

    在上面我介绍了精灵的创建一般有两种:一种是利用帧缓存,另一种是直接索引贴图ID进行创建;所以呢更换精灵贴图也一般分为两种方法;

首先介绍第一种直接利用新建贴图进行更换:

 实例代码如下:

//-----没有换贴图前
CCSprite*sprite =[CCSprite spriteWithFile:@ "Icon.png" ];
sprite.position=ccp(150,150);
[self addChild:sprite];
//-----换贴图后
CCSprite*sprite2 =[CCSprite spriteWithFile:@ "Icon.png" ];
sprite2.position=ccp(350,150);
[self addChild:sprite2];
//更换贴图
CCTexture2D * texture =[[CCTextureCache sharedTextureCache] addImage: @ "Default.png" ];
//新建贴图
[sprite2 setTexture:texture];


运行效果如下:

【iOS-Cocos2d游戏开发之二十】贴图更换_第1张图片

 第二种利用帧替换:

//加载帧缓存,这个testpngs.plist保存了Icon和111两张图,-hd表示高清版本iphone4
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@ "testpngs-hd.plist" ];
//-----没有换贴图前
CCSprite*sprite =[CCSprite spriteWithSpriteFrameName:@ "Icon.png" ];
sprite.position=ccp(150,150);
[self addChild:sprite];
//-----换贴图后
CCSprite*sprite2 =[CCSprite spriteWithSpriteFrameName:@ "Icon.png" ];
sprite2.position=ccp(350,150);
[self addChild:sprite2];
//更换帧贴图
//从帧缓存中取出111.png
CCSpriteFrame* frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@ "111.png" ];
[sprite2 setDisplayFrame:frame2];
 

运行效果如下:

 

你可能感兴趣的:(游戏,iPhone)