僵尸移动效果

来源:互联网 发布:java messagedigest 编辑:程序博客网 时间:2024/05/16 04:42

从本文开始,将以“植物大战僵尸”游戏为参考,练习UIView的使用。

首先从僵尸的移动效果开始。僵尸图片如下所示:

“僵尸”作为一个特殊的种类,我们需要先帮它们创建“模型”。把僵尸们的共有特征封装起来,编写一个接口:

#import <UIKit/UIKit.h>@interface TRZomb : UIImageView //继承自UIImageView控件@property (nonatomic)int count;//僵尸的序号,用于选择图片中的僵尸(由8只僵尸连成的图)@property (nonatomic, strong)UIImage *zombImage;//僵尸图片@property (nonatomic)float speed;//僵尸速度@end
#import "TRZomb.h"@implementation TRZomb/* * 初始化方法 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        //每隔0.05s就调用一次animation方法        [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animation:) userInfo:nil repeats:YES];    }    return self;}-(void)animation:(NSTimer *)timer{    //从已有的图片上获取僵尸图片    CGImageRef subImage = CGImageCreateWithImageInRect(self.zombImage.CGImage, CGRectMake(self.count++%8*self.zombImage.size.width/8, 0, self.zombImage.size.width/8, self.zombImage.size.height));    self.image = [UIImage imageWithCGImage:subImage];//僵尸的外貌被赋值    CGImageRelease(subImage);//释放图片资源,参数为空时不报错    //僵尸中心点的位置,y轴不变,x轴每次调用改方法就减去僵尸移动过的距离(速度:self.speed/0.05)    self.center = CGPointMake(self.center.x-self.speed, self.center.y);    //当僵尸移出到左边界时,取消定时器    if (self.center.x <= -30) {        [timer invalidate];//让timer停止        [self removeFromSuperview];    }}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/- (void)dealloc{    NSLog(@"僵尸死掉了");}@end

“僵尸”先生的接口已经完成了,下面可以开发第一台机器来制造最弱的僵尸杂兵了(也就是第一幅图的僵尸),炮灰制造者

#import "TRZomb.h"@interface TRZombA : TRZomb //毫无疑问的继承僵尸特征@end
#import "TRZombA.h"@implementation TRZombA/* * 初始化方法 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.zombImage = [UIImage imageNamed:@"zomb_0.png"];//僵尸图片        self.speed = .5;//僵尸速度    }    return self;}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end

OK,其余的僵尸也是用类似的制造机器。僵尸大军准备集结了

#import "TRViewController.h"#import "TRZombA.h"#import "TRZombB.h"#import "TRZombC.h"#import "TRZombD.h"@interface TRViewController ()@property (nonatomic)int count;@end@implementation TRViewController- (void)viewDidLoad{    [super viewDidLoad];    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(addZomb) userInfo:nil repeats:YES];}/* * 制造僵尸流水线 */-(void)addZomb{    int random = arc4random()%4;//随机僵尸生成指令    //在Switch Case里面如果需要创建新变量需要加大括号    TRZomb *zomb = nil;    switch (random) {        case 0:            zomb = [[TRZombA alloc]init];//制造炮灰僵尸            break;        case 1:             zomb = [[TRZombB alloc]init];//制造民工僵尸            break;        case 2:             zomb = [[TRZombC alloc]init];//制造铁桶僵尸            break;        case 3:             zomb = [[TRZombD alloc]init];//制造橄榄球僵尸            break;    }    //量产僵尸宽60,高120,从坐标x:568,y:0~259处发起攻击    zomb.frame = CGRectMake(568,arc4random()%260, 60, 120);    [self.view addSubview:zomb];//把僵尸投入战场    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0