2011-07-23 187 views
0

这里的柜台是我的代码:问题与冲突

-(void)detectCollision{ 

    imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y); 


    if(CGRectIntersectsRect(imageView.frame,centre.frame)){ 


    label.text= [NSString stringWithFormat:@"%d", count]; 
    ++count; 
} 

我有一个CADisplayLink(60帧)上detectCollision。 我想增加一个“计数”的每一次“imageView”与“中心”碰撞,但我的问题是,计数增量太快,每次有一个碰撞它增加近100或200,我不知道为什么。我该如何解决这个问题?

回答

0

它因为每次帧开始相交,

0123直到帧分离和超过100计数增加了CADisplayLink

所以你可以使用一个BOOL并将其设置为true时,帧第一相交

if(CGRectIntersectsRect(imageView.frame,centre.frame)) 

你的条件将是真实的。然后检查它们是否分开,并使BOOL返回false。

初始化BOOL intersectFlag在init中为false。我认为这些帧最初不会相交。

-(void)detectCollision 
{ 
    imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y); 

    if(!intersectFlag) 
    {  
     if(CGRectIntersectsRect(imageView.frame,centre.frame))  
     { 
      intersectFlag = YES;   
      label.text= [NSString stringWithFormat:@"%d", count]; 
      ++count; 
     } 
    } 
    else 
    { 
     if(!CGRectIntersectsRect(imageView.frame,centre.frame)) 
     { 
      intersectFlag = NO; 
     } 
    } 
} 
+0

没有它不工作时,计数器增加非常快,直到ImageView的不接触中心 –

+0

你初始化intersectFlag为NO在初始化 – ArunGJ

+0

没有,但我从来没有使用BOOL,我怎么能初始化intersectFlag为NO在init –

0

找出碰撞ü应用范围使用不是帧

边界 - 尺寸对象的内部

帧 - 从上海华的x,y +大小(边界)偏移

在这里,我假定ù在人像模式

-(void)detectCollision{ 

     if(CGRectContainsPoint(imageView.bounds, CGPointMake(160, 240))){ 


       label.text= [NSString stringWithFormat:@"%d", count]; 

       ++count; 

     } 


    }