IOS开源项目--僵尸来袭
来源:互联网 发布:孔子知天命 编辑:程序博客网 时间:2024/05/23 13:06
僵尸来袭的画面大家有没有想过。那么如果如何通过OC来实现呢。闲来无事写了一个开源项目放到github共享。下面一起分析一下核心代码。网上找来4张图片。每张图片里面有8个僵尸。我们需要切图来把8个僵尸放到一个数组中,然后使用数组动画。我们需要一个僵尸类,下面有4个子类。每一种僵尸有自己的奔跑速度。这又涉及到继承和派生的概念。还需要引入定时器。
软件结构如下:
viewController中随机出僵尸
- (void)viewDidLoad { [super viewDidLoad]; self.zombs = [NSMutableArray array]; // for (NSInteger i = 0; i < 100; i++) { // Zomb *z = [[Zomb alloc]initWithFrame:CGRectMake(arc4random()%300, arc4random()%600, 30, 50)]; // [z beginAnimation]; // [self.view addSubview:z]; // } [NSTimer scheduledTimerWithTimeInterval:.1 repeats:YES block:^(NSTimer * _Nonnull timer) { int type = arc4random()%4; Zomb *zomb = nil; switch (type) { case 0: zomb = [[ZombA alloc]initWithFrame:CGRectMake(375, arc4random()%600, 30, 50)]; break; case 1: zomb = [[ZombB alloc]initWithFrame:CGRectMake(375, arc4random()%600, 30, 50)]; break; case 2: zomb = [[ZombC alloc]initWithFrame:CGRectMake(375, arc4random()%600, 30, 50)]; break; case 3: zomb = [[ZombD alloc]initWithFrame:CGRectMake(375, arc4random()%600, 30, 50)]; break; } [zomb beginAnimation]; [self.view addSubview:zomb]; [self.zombs addObject:zomb]; }];
僵尸类中,切图,并组成僵尸动画
-(void)beginAnimation{ UIImage *zombImage = [UIImage imageNamed:self.zombImage]; NSMutableArray *zombImages = [NSMutableArray array]; float w = zombImage.size.width/8; float h = zombImage.size.height; for (NSInteger i = 0; i < 8; i++) { CGImageRef subImage = CGImageCreateWithImageInRect(zombImage.CGImage, CGRectMake(i*w, 0, w, h)); UIImage *image = [UIImage imageWithCGImage:subImage]; [zombImages addObject:image]; //释放图片资源 CGImageRelease(subImage); } //开启每个僵尸的动画 self.animationImages = zombImages; self.animationDuration = 1; self.animationRepeatCount = 0; [self startAnimating];}
实现效果如下:
所有源代码以及图片请在github下载:
https://github.com/lee727n/zombie-wave
0 0
- IOS开源项目--僵尸来袭
- (大二游戏)植物大战僵尸之僵尸来袭
- ios 僵尸调试
- ios 僵尸调试
- IOS开源项目
- ios开源项目
- iOS开源项目
- ios开源项目
- iOS开源项目
- iOS开源项目
- iOS开源项目
- iOS开源项目
- ios开源项目
- ios 开源项目
- ios 开源项目
- iOS开源项目
- IOS开源项目
- iOS开源项目
- 计划写个分布式爬虫来对近十年开发的总结
- php 查看内存使用情况
- solrcloud5+jetty
- 经纬度计算两地距离
- 在 WebService 中采用 TSOAPAttachment 传输文件
- IOS开源项目--僵尸来袭
- 对于拷贝构造函数和赋值构造函数的理解
- 1
- 2
- queue容器
- EF的edmx文件
- CodeForces
- 【a标签的使用和属性】
- 用python玩点有趣的数据分析——一元线性回归分析实例