Andy--清风

总有一种东西能让你一直为他奋斗终身, 永不孤独寂寞
随笔 - 30, 文章 - 1, 评论 - 107, 阅读 - 91576
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

在上一们讲了精灵表的一些知和使用Zwoptex对图片进行了整合和生成plist文件。这一节我们应用上一节的工具重新生成一对精灵表,对精灵进行操作。

1.建立工程


建立完工程后,如下:


2.载入文件


分别载入以上4个精灵,利用zwoptex生成的plistpng文件如下:



接下来把生成的plistpng导入resources文件中。

3.编程

HelloWorld中的.m文件的注释掉里面其他的代码,重写如下:


代码解释如下:

复制代码
CGSize s = [[CCDirector sharedDirector] winSize];//获取屏幕的尺寸

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprite.plist"];//载入我们生成的plist文件到CCSpriteFrameCache的缓冲文件中

//获取精灵表中的第一张图片加载在场景中,并设置他的位置
sprite1 = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sprite_01.png"]];
sprite1.position = ccp( s.width/2-80, s.height/2);
[self addChild:sprite1];

//获取精灵表中的第二张图片加载在场景中,并设置他的位置
sprite2 = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sprite_03.png"]];
sprite2.position = ccp( s.width/2+80, s.height/2);
[self addChild:sprite2];

//根据设定的时间更新页面。调用updateSprite方法
[self schedule:@selector(updateSprite:) interval:0.3f];
复制代码

当调用updateSprite方法实现小鸟眨眼睛的更新。

代码的解释如下:

复制代码
static int sprite1FrameIndex = 0;//标志位置为0
static int sprite2FrameIndex = 4;//标志位置为4
-(void) updateSprite:(ccTime)dt
{

if (++ sprite1FrameIndex > 2) {
sprite1FrameIndex = 1;//进行循环。使精灵1不断执行相同的过程
}
if (++ sprite2FrameIndex > 4) {
sprite2FrameIndex = 3;//进行循环。使精灵2不断执行相同的过程
}
//更新的时候显示其他的精灵图片。
[sprite1 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"sprite_%02d.png",sprite1FrameIndex]]];
[sprite2 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"sprite_%02d.png",sprite2FrameIndex]]];
}
复制代码


4.运行如下图


5.总结

的重点在于如何上一生成的文件取并做成画效果示,其中,CCSpriteFrameCachesetDisplayFrame两个方法较为重要,可以重点掌握一下,以便能用在日常的游发项目中。

源码在这里下载:http://download.csdn.net/detail/qiaoshe/3707533

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示