Project 01 PlantAndZomb

代码分享:http://pan.baidu.com/s/1qWoLMiw

准备:

1 如何实现一张图片上的多个图片连续显示

       A》使用事件计时器

       B》调用

              CGImage RefsubImage =CGImageCreateWithImageInRect(zombImage.CGImage, CGRectMake(self.count++%8*w, 0,w, zombImage.size.height));

  self.image = [UIImage imageWithCGImage:subImage];

   CGImageRelease(subImage);

2计算视图中的子视图的个数

int count= self.view.subviews.count

3 判断点击的位置时候位于某图片上:

CGRectContainsPoint(图片.fram,点击位置)

判断两个矩形是否有交点

CGRectContainsRect(图片.fram,点击位置)

 

 

[植物大战僵尸Day01]

1>创建植物类:

植物类有一个父类:父类中要实现的方法有:在初始化的时候创建事件计时器

                在计时器的方法中实现根据事件不同不断的切换同一张图片的多个图片实现动画效果

               [公开一个属性用于接受图片]

       其他植物类:继承父类:并在出事化的时候传入照片

 

2>僵尸类:

僵尸类有一个父类:父类中要实现的方法:在初始化的时候创建一个事件计时器

        在计时器的方法中实现根据不同的时间切换同一张图片上的多个照片形成动画效果

        并且在该方法中实现僵尸的移动     僵尸移动出屏幕时进行判断移除僵尸防止内存的泄露  (结束计数器   移除僵尸)

 [公开一个用于接受照片的属性  和一个用于改变位置的属性]   

       其他僵尸类:继承父类:并在初始化的方法中传入图片和一个用于改变速度的数;

 

3>编写主函数:

       a.拖拽不同的植物在响应的位置上种植植物:

       调用touch的方法began   move  end    cencell(意外终端的方法)

       touch……began…方法中:

       1.获得touch的位置:

              UITouch*t=[touches anyObject];

              CGPointp=[t locationInView:self.view];

       2.对所有的植物进行遍历  根据点击的点的位置对象植物的位置进行确定生成什么样的植物

       for(int i=0; i<self.plantImageViews.count; i++) {

       UIImageView *plantIV=self.plantImageViews[i];

        if(CGRectContainsPoint(plantIV.frame, p)) {

           switch (i) {

               case 0:

                   self.dragPlant=[[subFlower alloc]initWithFrame:plantIV.frame];

                   break;

               case 1:

                   self.dragPlant=[[pea alloc]initWithFrame:plantIV.frame];

                   

                   break;

               case 2:

                    self.dragPlant=[[icePeaalloc]initWithFrame:plantIV.frame];

                   

                   break;

               case 3:

                   self.dragPlant=[[Nut alloc]initWithFrame:plantIV.frame];

                   

                    break;

                   

               default:

                   break;

            }

           [self.view addSubview:self.dragPlant];

        }

    }

}

  touchMoved..方法中

       获得点击的点把创建的植物的中心点移动倒该触摸的位置

    touchEnd…方法中

       获得触摸到的位置

       判断是否生成了植物

       遍历所有的可以种植植物的位置

              根据遍历出的位置判断该位置是否已经有植物(判断该视图的subview.count=1)手触摸的         位置在该遍历出的位置内

              如果成立则在该视图内添加该植物,并将该植物的中心点移动倒该视图的中心点

       如果该位置不是可以种植植物的位置那么则将植物移除

              if[self.dragPlant.superViewisEqual:self.view]{

              [self.dragPlantremove_FromSuperview];

          }

       并将该植物赋值为空避免种下后还可以移动

  touchCancell方法中

       移除该植物  (正在拖拽过程中如果有电话接入则取消该次拖拽)

                    

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

    UITouch*t=[touches anyObject];

    CGPointp=[t locationInView:self.view];

    for (int i=0; i<self.plantIVs.count; i++) {

        self.dragplantIV=self.plantIVs[i];//begien的时候拖住的图片在种子处

        if (CGRectContainsPoint(self.dragplantIV.frame,p)) {

        switch(i) {

            case0:

                self.dragplantIV=[[sunfloweralloc]init];

                break;

             case1:

                self.dragplantIV=[[peaalloc]init];

                break;

                case2:

                self.dragplantIV=[[icepeaalloc]init];

                break;

            default:

                self.dragplantIV=[[nutalloc]init];

                break;

        }

        [self.view addSubview:self.dragplantIV];

    }

  }

}

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{

    UITouch*t=[touches anyObject];

    CGPointp=[t locationInView:self.view];

    self.dragplantIV.center=p;//移动的时候拖动的图片的中心随着touch拖动的点移动

}

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{

    UITouch*t=[touches anyObject];

    CGPointp=[t locationInView:self.view];

    if (self.dragplantIV){

        for(UIView *box inself.boxes){

            if(CGRectContainsPoint(box.frame, p)&&box.subviews.count==0) {//p点在box内并且这个box区域内没有放东西,就把这个拖着的图片放在这个box里面

                self.dragplantIV.center=CGPointMake(box.frame.size.width/2, box.frame.size.height/2);//box中心点是它本身这个view的中心,而拖住的图片的中心是相对于self.view这个控制器的

                [box addSubview:self.dragplantIV];

            }

            if([self.dragplantIV.superview isEqual:self.view]) {//如果把图片拖到了box之外的区域,则移除

                [self.dragplantIV removeFromSuperview];

            }

            self.dragplantIV=nil;//随着拖动最后的摆放好图片之后,把本次touch拖动的图片需要清空,不在控制

        }

    }

}

-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event{

    [self.dragplantIV removeFromSuperview];//程序运行的时候,不小心拖出了通知栏或者来电话等影响程序运行的时候,这种意外中断的情况移除正在拖拽并且没有放到坑内的图片

}

 

Day02

分析:

1、植物类的共性:sunFloewr可以产生阳光、pea可以发射子弹、icePea可以发射子弹,所以可以把它们的共性放在plant的父类方法中,因为pea可以发射子弹,icePea也可以发射子弹,只是子弹的图片不同,因此可以把子弹的图片放在peaicePea继承。

 

2、设置每种植物耗费的金币数,当金币数达不到某种植物要求的值时不能拖拽添加植物。

处理CDtouchend中添加方法,拖拽的植物到坑中之后,应对的某种植物栏添加遮罩实现方法:给这张图片添加一个动画  改变alpha的值

   UIImageView*plantIV = self.plantIVs[self.dragPlant.tag];

                plantIV.alpha = .3;

                [UIViewanimateWithDuration:5animations:^{

                    plantIV.alpha = .8;

                } completion:^(BOOL finished) {

                    plantIV.alpha = 1;

                }];

3、添加僵尸,让僵尸从一定的行数走过来

 

4、设置僵尸的血量检索子弹和僵尸的碰撞,根据僵尸的血量的减少移除僵尸,icepeape发的子弹僵尸速度要减少。

 

5、设置植物的血量,在僵尸类中写一个eat方法用于僵尸吃掉植物,根据植物的血量减少,在控制器中设置检索植物与僵尸的碰撞,当僵尸的中心点含在植物所在的坑内的时候,僵尸停止行走,速度变为0

 

6、添加一个铲子用于铲掉植物,为storyboard中的铲子imageview添加一个UIPanGesturerecognasizer手势,为这个手势连接方法到控制器添加铲子,实现拖住铲子到有植物的box内可以移除植物,松手铲子消失;

利用pan手势的三个状态实现:

-(IBAction)panAction:(UIPanGestureRecognizer *)sender {

    CGPoint p = [senderlocationInView:self.view];

   

    switch ((int)sender.state) {

        case UIGestureRecognizerStateBegan:

            self.shovelIV = [[UIImageViewalloc]initWithFrame:sender.view.frame];

            self.shovelIV.image = [UIImageimageNamed:@"shovel.png"];

            [self.viewaddSubview:self.shovelIV];

            break;           

        case UIGestureRecognizerStateChanged:

            self.shovelIV.center = p;

            break;

   case UIGestureRecognizerStateEnded:

            for (UIView *box in self.boxes) {

                if (CGRectContainsPoint(box.frame,p)) {

                    if(box.subviews.count>0) {

                        Plant *plant =[box.subviews lastObject];

                        [plantremoveFromSuperview];

                        [self.plantsremoveObject:plant];

                        [plant.animationTimer invalidate];

                        [plant.fierTimerinvalidate];  

                    }                   

                }

                  [self.shovelIVremoveFromSuperview];

            }

            break;

    } 

}

 

posted on 2015-03-13 23:00  蝈蝈512  阅读(183)  评论(0编辑  收藏  举报

导航