Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Hi guys,

Question for Xcode:
Anyone know how to play the sound while the image/object is moving in accelerometer?

I inserted the sound inside the code that cause the ball moving, but it keeps looping the sound and the object cannot move at all for now.

Thank you.

The code in my main file:
Code:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
	
	
	valueX = acceleration.x*10.0;
	valueY = acceleration.y*10.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 < 55+BALL_RADIUS)
		newY = 55+BALL_RADIUS;
	
	
	if ([checkball isEqualToString:@"yes"])
	{	
		NSString *path = [[NSBundle mainBundle] pathForResource:@"ball_roll_sound" ofType:@"mp3"];
		AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error: NULL];
		theAudio.numberOfLoops = 1; 
		theAudio.currentTime = 0; // start at beginning
		theAudio.volume = 1.0;
		[theAudio play];
		
	CGPoint newCenter = CGPointMake(newX, newY);
	ball.center = newCenter;
	[self checkcollision];
	}
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.