【问题标题】:problem with collision of two images两个图像碰撞的问题
【发布时间】:2011-05-08 08:14:25
【问题描述】:

这是我的问题: 我有两个图像:flakeImage 和 ViewToRotate。我想要的是,如果 flakeImage 触及 ViewToRotate,ViewToRotate.alpha=0.5;但是当 FlakeImage 出现在屏幕上时 ViewToRotate.alpha=0.5;不碰它。我认为这是我的观点有问题,因为我有:

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

这里是代码:

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:7 ];

// set the postion where flake will move to
flakeView.center = viewToRotate.center;

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

请问我该如何解决? 如果有人可以帮助我,那就太好了。

【问题讨论】:

  • 在动画结束时将 alpha 设置为 0.5 就足够了吗?还是需要真正的碰撞代码?
  • 我需要真正的碰撞代码但是当图像(flakeView)出现在屏幕上而不碰撞另一个时是.alpha=0.5;
  • 嗯,我仍然不确定您真正需要什么。所以你有一片片,也许是一片雪片?它从一个随机高度(startY)开始下降,或者很好地到达 viewToRotate 所在的位置,对吧?那么,当动画的目标是让薄片处于另一个视图的相同位置时,为什么需要碰撞检测呢?到目前为止,它是关于位置的,alpha 与它有什么关系?当薄片到达最终目的地时,它应该变成半透明吗?请帮我理解。
  • 不,因为 viewToRotate 是一个男人的头,我想要的是薄片接触嘴巴,因为动画使薄片到达头部的中心,直到它才停止已经到达中心。我想要的只是:如果碰撞 viewToRotate 和剥落(做某事)。我该怎么做。请帮帮我
  • @arvin - 这不是 Stack Overflow 的工作方式。如果您对原始问题没有得到很好的答复,请对其进行编辑以使问题更清晰。转发问题是不可接受的,无论这对您来说多么紧急。

标签: iphone cocoa-touch animation uiimageview collision-detection


【解决方案1】:

我写过http://itunes.apple.com/us/app/balls/id372269039?mt=8,对此我有一点二年级经验。如果您检查该应用程序,您会看到一些相同的问题。这个话题是一个相当深的兔子洞。当我启动那个应用程序时,我什至不知道如何编写一个像样的游戏循环。您首先需要它,因为您需要进行精确的时间步长计算。 AFA 碰撞,您分别更新模型和视图,因此如果您更新模型和对象重叠,您需要备份它们直到它们不发生碰撞,然后使用结果更新您的视图。如果你打算有很多碰撞对象,你可能会使用 UIViews 碰壁。更复杂的是,如果您的对象是圆形的,那么 CGRectIntersectsRect 将无法正常工作。这使数学有点复杂,但还不错。使用我的应用程序,我发现很难让物理看起来逼真。问题变成了球 A 和 B 重叠,所以你备份它们,然后它们现在与其他球相交,等等。This 链接是一个很好的起点,但是那里有很多代码示例“几乎”工作。

【讨论】:

    【解决方案2】:

    CGRect 有一个交集函数。 UIViews 的框架是 CGRects。

    if (CGRectIntersectsRect(view1.frame, view2.frame) == 1)
      NSLog(@"The views intersect");
    else
      NSLog(@"The views do not intersect");
    

    我预见的问题是,如果矩形有很多空白,它们会在实际接触之前看起来相交

    其次,您应该切换到阻止动画。强烈推荐

    UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];
    
    // use the random() function to randomize up our flake attributes
    int startY = round(random() % 320);
    
    // set the flake start position
    flakeView.center = CGPointMake(490, startY);
    flakeView.alpha = 1;
    
    // put the flake in our main view
    [self.view addSubview:flakeView];
    
    [UIView animateWithDuration:.7
                     animations:^ {
                             // set the postion where flake will move to
                             flakeView.center = viewToRotate.center;
                      };
    

    都是凭记忆做的,不知道有没有错误。

    圆形碰撞:

    a^2 + b^2

    if(pow(view2.frame.origin.x - view1.frame.origin.x, 2) + 
        pow(view2.frame.origin.y - view1.frame.origin.y, 2) < 
        pow(view1.frame.size.width/2, 2))
    {
         //collision
    }
    else
    {
         //no collision
    }
    

    再次,全部凭记忆,自行检查错误

    【讨论】:

    • 这正是我的问题“他们会在实际接触之前看起来相交”我该如何解决这个问题?
    • 您必须为您的对象定义自定义碰撞检测。因此,您需要圆形碰撞而不是矩形碰撞。检查编辑
    • 首先什么是 pow 它是一个变量?第二个而不是 //do I have to write CGIntesectsRect... 或者例如执行 view2.alpha =0;
    • pow 是指数数学的数学函数。 pow(x, y) = x^y。所以 pow(2, 3) = 2^3 = 8。你想保持视野吗?如果您仍然需要视图可交互,那么您可以使用 view2.alpha = 0;。如果您不再需要该视图,您可以隐藏它(稍后使用它:[view2 setHidden:YES];)或释放它(完成它,不再需要它:[view2 removeFromSuperview];) .
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多