2011-09-22 161 views
0
-(void)moveTheImage{ 
for (NSUInteger i = 0; i < [views count]; i++) { 
    image = [views objectAtIndex:i]; 
    X = [[XArray objectAtIndex:i] floatValue]; 
    Y = [[YArray objectAtIndex:i] floatValue]; 
image.center=CGPointMake(image.center.x + X, image.center.y + Y); 


    if(!intersectFlag) 
    {  

     if(CGRectIntersectsRect(image.frame,centre.frame))  
     { 
      intersectFlag = YES;  
      label.text= [NSString stringWithFormat:@"%d", count]; 

      ++count; 
     } 


    } 
    else 
    { 
     if(!CGRectIntersectsRect(image.frame,centre.frame)) 
     { 
      intersectFlag = NO; 
     } 
    } 


} 

我希望每次“图像”与“中心”的计数增加1相交,但是当有碰撞计数增长非常快,直到“图像”不触摸“中央”。更准确地说,“图像”移动,然后通过“中心”,计数增加,当“图像”不与“中心”接触时,计数器停止。请问我该如何解决此问题?对不起我的英语我是法国人:/与UIImageViews碰撞的问题

+0

这里缺少细节。首先:如何(以及多久)叫做'moveTheImage'?而且,'count'在哪里被声明和初始化?没有这个,我个人帮不了你很多... – Zaphod

+0

moveTheImage和计数是在我的viewDidLoad中声明,但问题来自我的数组视图我的事 –

+0

你的描述行为似乎对我来说是正确的,因为你为每个绘制的帧调用'moveTheImage',比如说,在30fps的情况下,如果运动图像需要3秒来移动'centre',那么它将被计数90次。您应该使用一个'NSMutableSet'并在其中存储碰撞物体,并删除那些不是。或者您可以向表示碰撞状态的对象添加一个属性,并对它们进行计数。 – Zaphod

回答

0

正如我在最后一条评论中所说的,我认为你的计数问题来自帧频。

因此,您可以使用一个NSMutableSet,它将包含所有正在碰撞的对象。

在该界面中,你声明你的新集:

NSMutableSet * collisionObjects; 

在init方法:

collisionObjects = [[NSMutableSet alloc] init]; 

在的dealloc:

[collisionObjects release]; 

而且你的方法变为:

-(void)moveTheImage{ 
    for (NSUInteger i = 0; i < [views count]; i++) { 
     image = [views objectAtIndex:i]; 
     X = [[XArray objectAtIndex:i] floatValue]; 
     Y = [[YArray objectAtIndex:i] floatValue]; 
     image.center=CGPointMake(image.center.x + X, image.center.y + Y); 

     if (CGRectIntersectsRect(image.frame,centre.frame)) { 
      // If the object is in collision, we add it to our set 
      // If it was already in it, it does nothing 
      [collisionObjects addObject:image]; 
     } 
     else { 
      // If the object is not in collision we remove it, 
      // just in case it was in collision last time. 
      // If it was not, it does nothing. 
      [collisionObjects removeObject:image]; 
     } 
    } 

    // The intersectFlag is updated : YES if the set is empty, NO otherwise. 
    intersectFlag = [collisionObjects count] > 0; 
    // We display the count of objects in collision 
    label.text= [NSString stringWithFormat:@"%d", [collisionObjects count]]; 
} 

这样,您总是可以使用[collisionObjects count]来计算碰撞对象的数量。