如何创建多个imageview并获取交叉的框架。

时间:2012-06-05 18:03:49

标签: iphone ios ipad uiimageview cgrect

我正在开发游戏。下面的屏幕截图显示了我游戏的整个过程。 enter image description here

首先,当我的游戏开始时,在viewdidload中,我使用NStimer从上到下移动我的红球。当我点击我的aeroplan时,它使用NStimer冷杉的错误...首先我检查交叉点然后创建新的球

          if ((CGRectIntersectsRect(missels.frame,Ball.frame))
         {
          [self performSelector:@selector(createball) ];
          }

当我的误导击中球时,它分成两部分,就像我在屏幕截图中显示的那样。这两个新球的代码。

 -(void)createball{
  imageMove1 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];        
  UIImage *imag = [UIImage imageNamed:@"ball1.png"];
  [imageMove1 setImage:imag];
  imageMove1.tag=i;
  [self.view addSubview:imageMove1];
  [ballArray addObject:imageMove1];

  imageMove2 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];        
  UIImage *imag1 = [UIImage imageNamed:@"ball1.png"];
  [imageMove2 setImage:imag1];
  imageMove2.tag=k;
  [self.view addSubview:imageMove2];
  [ball1Array addObject:imageMove2];
  [self performSelector:@selector(moveball) ];
 }

之后我使用CAKeyframeAnimation给这些新球移动。这是代码。

-(void)moveball{
 for (int i=0; i<[ballArray count]; i++) {
 moveimg=[ballArray objectAtIndex:i];
 CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 pathAnimation.calculationMode = kCAAnimationPaced;
 pathAnimation.fillMode = kCAFillModeForwards;
 pathAnimation.removedOnCompletion = NO;
 pathAnimation.duration = 15.0;
 pathAnimation.repeatCount = 1;
 CGMutablePathRef curvedPath = CGPathCreateMutable();
 CGPathMoveToPoint(curvedPath, NULL, x+15, y);
 CGPathAddQuadCurveToPoint(curvedPath, NULL, 20, 10, 100, 330);
 pathAnimation.path = curvedPath;
 CGPathRelease(curvedPath);
 moveimg.center=CGPointMake(x, y);
 [moveimg.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
  }

for (int k=0; k<[ball1Array count]; k++) {
        move1img=[ball1Array objectAtIndex:k];
        CAKeyframeAnimation *pathAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation1.calculationMode = kCAAnimationPaced;
        pathAnimation1.fillMode = kCAFillModeForwards;
        pathAnimation1.removedOnCompletion = NO;
        pathAnimation1.duration = 15.0;
        pathAnimation1.repeatCount = 1;
        CGMutablePathRef curvedPath1 = CGPathCreateMutable();
        CGPathMoveToPoint(curvedPath1, NULL, x+40, y);
        CGPathAddQuadCurveToPoint(curvedPath1, NULL, 350, 10, 380, 330);
        pathAnimation1.path = curvedPath1;
        CGPathRelease(curvedPath1);
        move1img.center=CGPointMake(x, y);
        [move1img.layer addAnimation:pathAnimation1 forKey:@"moveTheSquare"];
     }
   }

我的这种方法只适用于我在错误和球交叉之后创造前两个球。当我的球运动之后,它不适用于交叉。所以在这场比赛中,我面临两个主要问题。

  1. 如何在我的球和错误的每个交叉点上创建两个新球。
  2. 在创建这些球之后如何在运动期间获得这些球的框架以便与错误相交。就像这样......

      if ((CGRectIntersectsRect(missels.frame,mynewballs.frame))
    

    任何人都可以提供最好的解决方案来解决这个问题。任何帮助都会提前得到帮助。

0 个答案:

没有答案