植物模型

来源:互联网 发布:航天五院501 知乎 编辑:程序博客网 时间:2024/05/16 18:29
僵尸模型在上一节中已经初步编写完毕了,这一节主要讲述植物模型。以向日葵,豌豆射手,寒冰射手,坚果墙为例。

植物也有一些核心特征,必然要封装成接口。

#import <UIKit/UIKit.h>#import "TRViewController.h"/* * 植物 生产器核心 */@interface TRPlant : UIImageView //继承自UIImageView@property (nonatomic)int count;//植物外形序号(用于在8个植物动作中选一个)@property (nonatomic, strong)UIImage *plantImage;//植物图片(由8个连续的植物动作拼成)@property (nonatomic) int fps;//植物动作频率@property (nonatomic, weak)TRViewController *vc;//场景控制器-(void)fire;//植物效果(生产阳光或发射子弹)-(void)changeImage;//改变动作@end
#import "TRPlant.h"@implementation TRPlant/* * 初始化方法 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    // 每隔1.0/60s调用changeImage方法    if (self) {        [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];    }    return self;}-(void)changeImage{    self.fps++;    //每0.1s(1.0/60*6)调用一次,改变动作,总体看上去就是连贯的动作    if (self.fps%6 ==0) {        //从已有的图片上获取植物动作(瞬间行为)        CGImageRef subImage = CGImageCreateWithImageInRect(self.plantImage.CGImage, CGRectMake(self.count++%8*self.plantImage.size.width/8, 0, self.plantImage.size.width/8, self.plantImage.size.height));        //赋予植物外形动作        self.image = [UIImage imageWithCGImage:subImage];        //释放图片资源,参数为空时不报错        CGImageRelease(subImage);    }}/* * 植物效果(子弹,阳光...) */-(void)fire{    }@end

OK,下面就是我们最喜欢的向日葵了,这可是战争中的后勤保障部队,毫无攻击力,因此一般是最后两派的常客

#import "TRSunFlower.h"@implementation TRSunFlower/* * 初始化方法 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.plantImage = [UIImage imageNamed:@"plant_0.png"];        self.userInteractionEnabled = YES;    }    return self;}/* * 连续产出阳光 */-(void)fire{    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(addSunAction) userInfo:nil repeats:YES];}/* * 产出阳光的方法 */-(void)addSunAction{    //阳光就是一个可以点击的按钮,宽25,高25,上下左右5~24    UIButton *sunBtn = [[UIButton alloc]initWithFrame:CGRectMake(5+arc4random()%20, 5+arc4random()%20, 25, 25)];    //给阳光按钮(普通状态)外形图片    [sunBtn setImage:[UIImage imageNamed:@"sun.png"] forState:UIControlStateNormal];    //阳光点击事件    [sunBtn addTarget:self action:@selector(clickedSun:) forControlEvents:UIControlEventTouchUpInside];    //把阳光增加到场景中    [self addSubview:sunBtn];    //3s后,如果阳光没有被点击,就消失    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sunTimeoutAction:) userInfo:sunBtn repeats:NO];}//阳光自动消失-(void)sunTimeoutAction:(NSTimer *)timer{    UIButton *sunBtn = timer.userInfo;    [sunBtn removeFromSuperview];}//点击阳光,场景图上的阳光数增加-(void)clickedSun:(UIButton *)sunBtn{    [sunBtn removeFromSuperview];    int sunCount = self.vc.sunCountLabel.text.intValue;    self.vc.sunCountLabel.text = [NSString stringWithFormat:@"%d",sunCount+25];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end
向日葵有了,当然接下去就是最基本的兵种,万众瞩目的豌豆射手

#import "TRPlant.h"/* * 豌豆射手 */@interface TRPea : TRPlant@property (nonatomic, strong)NSMutableArray *bullets;//该植物在场景中能看到的全部子弹@property (nonatomic, strong)UIImage *bulletImage;//子弹图片@end
#import "TRPea.h"@implementation TRPea/* * 初始化方法 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.plantImage = [UIImage imageNamed:@"plant_2.png"];        self.bullets = [NSMutableArray array];        self.bulletImage = [UIImage imageNamed:@"bullet_0.png"];    }    return self;}/* * 每秒钟发射一颗子弹 */-(void)fire{    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addBullet) userInfo:nil repeats:YES];}/* * 发射子弹的动作 */-(void)addBullet{    //子弹就是个移动的图片对象    UIImageView *bulletIV = [[UIImageView alloc]initWithFrame:CGRectMake(self.superview.frame.origin.x+35, self.superview.frame.origin.y+5, 15, 15)];    bulletIV.image = self.bulletImage;    [self.vc.view addSubview:bulletIV];    [self.bullets addObject:bulletIV];}-(void)changeImage{    [super changeImage];    //移动子弹的代码    for (UIImageView *bulletIV in self.bullets) {        //0.1s移动距离2,速度就是20        bulletIV.center = CGPointMake(bulletIV.center.x+2, bulletIV.center.y);        //子弹射出场景外,就没用了        if (bulletIV.center.x>=576) {            [bulletIV removeFromSuperview];            [self.bullets removeObject:bulletIV];            //forin循环过程中如果修改了数组 需要跳出循环            break;        }    }}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end

好,就先完成两种植物机器的开发,其它植物也是大同小异。

0 0