Hi everyone, I'm new here, this is my first time using xcode. Hope anyone can help me here.
Purpose of code: I have a ball and a box (box1). When the ball collides with box1, the box1 disappears, when it collides it also plays a sound. But the problem is when the ball collides with box1, the sound keeps looping, and when the box1 disappeared, I move the ball to the box1 empty position, it still loops the sound. I just want to play the sound once.
The portion of code goes here:
Thank you.
Purpose of code: I have a ball and a box (box1). When the ball collides with box1, the box1 disappears, when it collides it also plays a sound. But the problem is when the ball collides with box1, the sound keeps looping, and when the box1 disappeared, I move the ball to the box1 empty position, it still loops the sound. I just want to play the sound once.
The portion of code goes here:
Code:
#import "accelerometerViewController.h"
#import <AVFoundation/AVFoundation.h>
@implementation accelerometerViewController
@synthesize ball;
@synthesize box1;
@synthesize box2;
-(void)checkcollision{
if (CGRectIntersectsRect(box1.frame, ball.frame)){
[box1 removeFromSuperview];
NSLog(@"green!");
//insert sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"trink_s" ofType:@"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error: NULL];
[theAudio play];
}
if (CGRectIntersectsRect(ball.frame, box2.frame)){
[box2 removeFromSuperview];
NSLog(@"red!");
}
//insert sound2
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
valueX = acceleration.x*30.0;
valueY = acceleration.y*30.0;
int newX = (int)(ball.center.x + valueX);
if (newX > 320-BALL_RADIUS)
newX = 320-BALL_RADIUS;
if (newX < 0+BALL_RADIUS)
newX = 0+BALL_RADIUS;
int newY = (int)(ball.center.y - valueY);
if (newY > 460-BALL_RADIUS)
newY = 460-BALL_RADIUS;
if (newY < 0+BALL_RADIUS)
newY = 0+BALL_RADIUS;
CGPoint newCenter = CGPointMake(newX, newY);
ball.center = newCenter;
[self checkcollision];
}
- (void)viewDidLoad {
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
[[UIAccelerometer sharedAccelerometer] setDelegate:self]; [super viewDidLoad];
}
Thank you.
Last edited: