【问题标题】:problem with the counter of a collision between two images两个图像之间碰撞计数器的问题
【发布时间】:2011-09-28 20:35:55
【问题描述】:

这是我的代码:

-(void)collision {

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

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


- (void)viewDidLoad {
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];

    count=0;    
    scale=1;
}

使用此代码,我的计数增加了不止一个,有时是 34,有时是 74 或 70,为什么?

【问题讨论】:

  • 为什么是count==0?是错误吗?试试count=0。并且也使用++something而不是something++,它更快。

标签: iphone xcode timer uiimageview collision-detection


【解决方案1】:

阻止接收器发射 并要求将其从 它的运行循环。

 - (void)invalidate

class reference

helpful question

【讨论】:

  • 为什么无效,我不想停止我的计时器?
  • @bernard langue 好的,我不是 iOS 多线程方面的专家。但。您不停止计时器,并且无法立即删除视图。和。随着图像被删除,没有充分的理由每秒调用选择器一百次。我的建议是仅在需要使用计时器时才启动计时器,然后立即停止。我相信也可以使用几个计时器。
  • 如何做到“仅在需要使用时启动计时器并在使用后立即停止”
  • [NSTimer scheduledTimerWithTimeInterval:0.01 target:self 选择器:@selector(collision) userInfo:nil repeats:YES];启动计时器并 - (void)invalidate 停止它,不是吗?开始/停止点取决于您的应用程序行为,如果您需要让计时器继续工作,您可以尝试在 移除视图之前使用计数器设置标签文本,希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多