按下按钮时,我试图在屏幕上移动一个对象。我无法确定代码有什么问题。每当我按下开始游戏时,应用程序崩溃,控制台显示terminating with uncaught exception of type NSException
。此外,我有2个警告,但不知道如何解决它们。
代码在这里:
-(IBAction)startGame:(id)sender{
startGame.hidden = YES;
shipMovingLeft = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(leftTap) userInfo:nil repeats:YES];
shipMovingRight = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(rightTap) userInfo:nil repeats:YES];
}
-(void)leftTap:(id)sender{
ship.center = CGPointMake(ship.center.x -1, ship.center.y);
}
-(void)rightTap:(id)sender{
ship.center = CGPointMake(ship.center.x -1, ship.center.y);
}
我收到的警告是未声明的选择器'左键点击'和未声明的选择器'右键点击'。
答案 0 :(得分:2)
将您的计时器代码更新为以下内容:
shipMovingLeft = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(leftTap:) userInfo:nil repeats:YES];
shipMovingRight = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(rightTap:) userInfo:nil repeats:YES];
}
您需要包含冒号以指示它们是该选择器的单个参数,在这种情况下它是(id)sender
。如果有两个参数,您可以使用@selector(rightTap::)
等。
为了您的信息,未声明的选择器消息实际上是一个警告,而不是错误(除非您已启用将警告视为错误),这应该提供了一个线索,表明您提供的选择器有问题。