我希望我的按钮发出短促的声音,并在每次点击后随机切换两个位置之一。当我尝试下面的代码时程序停止工作。在插入systemSound ID代码之前,代码工作正常。
- (IBAction)CountRedClicks:(id)sender{
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
RedCounter = RedCounter + 1;
RandNumber = 1 + arc4random() % 2;
if (RandNumber == 1) {
CGRect frame = RedButton.frame;
frame.origin.x = 128;
frame.origin.y = 458;
RedButton.frame = frame;
}
if (RandNumber == 1) {
CGRect frame = GreenButton.frame;
frame.origin.x = 422;
frame.origin.y = 458;
GreenButton.frame = frame;
}
if (RandNumber == 2) {
CGRect frame = RedButton.frame;
frame.origin.x = 422;
frame.origin.y = 458;
RedButton.frame = frame;
}
if (RandNumber == 2) {
CGRect frame = GreenButton.frame;
frame.origin.x = 128;
frame.origin.y = 458;
GreenButton.frame = frame;
}
if (RedCounter == 10) {
RedCounter = 0;
PointsCounter = PointsCounter + 1;
PointsLabel.text = [NSString stringWithFormat:@"%i", PointsCounter];
RedButton.hidden = true;
GreenButton.hidden = true;
PointsLabel.hidden = false;
ResetButton.hidden = false;
if (PointsCounter == 3){
[PointsLabel setText:@"GREAT JOB!"];
ResetButton.hidden = true;
}
}
}