获取计时器错误

时间:2014-07-02 20:23:43

标签: ios objective-c

在随机位置(spawnGood)创建图像后,如果该对象与用户控制的图像(userToken)发生碰撞,则NSTimer应该开始使用“棒”方法。如果移动userToken,则该方法应该将spawnGood移动到与userToken相同的位置(例如,将其自身粘贴到userToken)。但是,当两个图像发生碰撞时,我收到“无法识别的选择器发送到实例”错误。它可能是计时器或方法的问题,所以有人能够建议解决这个问题吗?在此先感谢:)

下面的代码产生了所描述的错误:

-(void)spawn{

    spawn = [NSTimer scheduledTimerWithTimeInterval:2.0f
                                             target:self
                                           selector:@selector(spawnMechanism)
                                           userInfo:nil
                                            repeats:YES];
}



-(void)stick:(UIImageView *)spawnGood{

    [UIView animateWithDuration:3.0
                          delay: 0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         spawnGood.center=CGPointMake(userToken.center.x, userToken.center.y);

                     }
                     completion:^(BOOL finished){
                         NSLog(@"complete");
                     }];



}




-(void)spawnMechanism{
    timeSinceLastSpawn++;


    if(timeSinceLastSpawn >= 3)
    {
        if( arc4random() % 10 < 7 ){


            UIImageView *spawnGood;

            spawnGood=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random() % 700, -100, 70,70)];
            UIImage *image;
            image=[UIImage imageNamed:@"friendly-01"];
            [spawnGood setImage:image];
            [array addObject:spawnGood];
            [self.view addSubview:spawnGood];

            NSLog(@"spawnGood spawned");
            [UIView animateWithDuration:2.0
                                  delay: 0.0
                                options: UIViewAnimationOptionCurveLinear
                             animations:^{
                                 CGRect frame1 =[spawnGood frame];

                                 frame1.origin.y =frame1.origin.y+950;
                                 [spawnGood setFrame:frame1];

                             }
                             completion:^(BOOL finished){

                                 if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
                                     NSLog(@"Good Collision");
                                     stick=[NSTimer scheduledTimerWithTimeInterval:rate
                                                                            target:stick
                                                                          selector:@selector(stick:)
                                                                          userInfo:nil
                                                                           repeats:YES];














                                 }else{
                                     [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
                                         CGRect frame1 =[spawnGood frame];

                                         frame1.origin.y =frame1.origin.y+950;
                                         [spawnGood setFrame:frame1];
                                     }
                                                      completion:nil];

                                     };
                             }



             ];


        }










            }





        }

1 个答案:

答案 0 :(得分:1)

您有两个错误:

  1. 此行中的目标

                                stick=[NSTimer scheduledTimerWithTimeInterval:rate
                                                                        target:stick
                                                                      selector:@selector(stick:)
                                                                      userInfo:nil
                                                                       repeats:YES];
    

    应该是自我&#39;而不是&#39;坚持&#39; (可能是NSTimer *类变量?)

  2. 棒的参数应该是NSTimer *,而不是图像视图。

  3. 你应该美化你的代码并添加更多的上下文。