iOS xcode: sound problem in CGRectIntersectsRect

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
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:
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:

chown33

Moderator
Staff member
Aug 9, 2009
8,447
4,449
Pale blue comma
There's a flaw in your logic.

A collision is not the ongoing overlap of rectangles at each time period. A collision is the change from non-overlapping in one time period to overlapping in the subsequent time period.

To start a sound only when the two items first collide, start the sound only when the two rectangles first overlap.

You will need an additional variable to track changes in overlap.

You should also ask yourself how removing a view changes its frame. If you are expecting the frame to "disappear" because the view isn't on-screen, you might want to confirm what really happens after removeFromSuperview.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
There's a flaw in your logic.

A collision is not the ongoing overlap of rectangles at each time period. A collision is the change from non-overlapping in one time period to overlapping in the subsequent time period.

To start a sound only when the two items first collide, start the sound only when the two rectangles first overlap.

You will need an additional variable to track changes in overlap.

You should also ask yourself how removing a view changes its frame. If you are expecting the frame to "disappear" because the view isn't on-screen, you might want to confirm what really happens after removeFromSuperview.
hi chown33,

Thanks for the reply and advice.
Would you mind to help me to write that portion of code. Coz I'm not familiar with xcode. I was just guessing the code, and I thought the call function is written like this "[self checkcollision]", I put this outside the void, but it didn't work. I thought removeFromSuperview really removes the box?
 

chown33

Moderator
Staff member
Aug 9, 2009
8,447
4,449
Pale blue comma
If you can't code this yourself, it suggests you need to review the basics of Objective-C and the relevant frameworks (Cocoa Touch, Core Graphics, etc.). You can't write programs effectively by haphazardly pasting together pieces of other people's programs, nor by putting things in different places until it finally compiles.

If you're not following a book or tutorial, you should be. You have to crawl before you can walk. There is no shortcut that successfully avoids learning the fundamentals.
 
Last edited:

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
If you can't code this yourself, it suggests you need to review the basics of Objective-C and the relevant frameworks (Cocoa Touch, Core Graphics, etc.). You can't write programs effectively by haphazardly pasting together pieces of other people's programs, nor by putting things in different places until it finally compiles.

If you're not following a book or tutorial, you should be. You have to crawl before you can walk. There is no shortcut that successfully avoids learning the fundamentals.
I followed the book's tutorial actually. Ok, nevermine. Thanks. I'll find other alternative solutions.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
8,447
4,449
Pale blue comma
I followed the book's tutorial actually.
Which book? Be specific: title, author, edition number if any.

Does the book have a website? If so, have you looked there for a solution?

We can't read your mind. If you don't tell us what you're studying from, we have no way of knowing what you're doing.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Which book? Be specific: title, author, edition number if any.

Does the book have a website? If so, have you looked there for a solution?

We can't read your mind. If you don't tell us what you're studying from, we have no way of knowing what you're doing.
The book has a website actually, but you need to purchase. The book that I bought is from Apress publisher, title: Beginning iPhone Games Development.

The audio framework I got it from this book. The collision part I got it from Youtube video tutorial. I know that I put the "[self checkcollision]" at the wrong place, coz that function will read 30 times in updateinterval. Just that I don't know how to write "call function".

I know that I'm a bit rush here. And I haven't studied xcode before, I'm just using my flash script knowledge to do this. And the boss just asked me to use xcode and didn't give me time to start from crawling, everyday keeps asking me whether my sound problem has been solve or not. So I force to do so, sorry if I've said anything wrong here in previous. I just hope that anyone can help me here, and I will really appreciate that. Thank you.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,447
4,449
Pale blue comma
The book has a website actually, but you need to purchase. The book that I bought is from Apress publisher, title: Beginning iPhone Games Development.
That book assumes you already know Objective-C, and are already familiar with Cocoa, frameworks, and working in Xcode. It says "Beginning" in its title, but it's not a book that's appropriate for a complete beginner.


The audio framework I got it from this book.
Is there a license involved? Are you free to use that code any way you want?

The collision part I got it from Youtube video tutorial.
Which one?


I know that I put the "[self checkcollision]" at the wrong place, coz that function will read 30 times in updateinterval. Just that I don't know how to write "call function".
That suggests you don't really know Objective-C. Until you solve that problem, you won't be able to solve the sound problem. Someone else might be able to solve the sound problem by writing and testing all the code for you, then giving it to you, but that's much more work than I'm willing to do.


I know that I'm a bit rush here. And I haven't studied xcode before, I'm just using my flash script knowledge to do this. And the boss just asked me to use xcode and didn't give me time to start from crawling, everyday keeps asking me whether my sound problem has been solve or not.
I'm not sure what the real goal here is. Is to learn how to write games for iPhone, or is it to make one specific modification to a youtube tutorial's code?

If the goal is learning how to write games for iPhone, then you need to start at the beginning by learning Objective-C. That's the foundation that everything else is built on. You can't avoid it.

If the goal is to modify code from a tutorial, then someone who knows Objective-C could write that, but it doesn't really help you learn anything, because you still won't really know Objective-C. You'll just know who to ask when your boss tells you to write more Objective-C programs.

If the goal is something else, then please explain exactly what the real goal is.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Actually my goal is just simple: 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.

The project that I started was VIEW BASED APPLICATION.

when I need an accelerometer code, I went to find the code in the book as I mentioned, when I need the collision code, I went to find CGRectIntersectsRect and see how's the tutorial and use the code on my program.

No one here in office knows xcode. I'm using the forum to ask the experts.

The code has no license, I didn't use the whole program and paste it, I do it on my own like my goal mentioned as above. coz the book has nothing that I want.

Collision code I got it from here http://www.youtube.com/watch?v=hdsnxcvNb7Y

here is the header code:
Code:
#import <UIKit/UIKit.h>
#define BALL_RADIUS 25

@interface accelerometerViewController : UIViewController <UIAccelerometerDelegate> {
	IBOutlet UIImageView *ball;
	IBOutlet UIImageView *box1;
	IBOutlet UIImageView *box2;
	float valueX;
	float valueY;
	
}

-(void)checkcollision;

@property (nonatomic, retain) UIImageView *ball;
@property (nonatomic, retain) UIImageView *box1;
@property (nonatomic, retain) UIImageView *box2;

@end
the code in main file:
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, remember to import avfoundation framework and #import <AVFoundation/AVFoundation.h>
		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!");
	}
	
}
 
		

-(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];
}	

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
sorry for the troublesome...T_T
 
Last edited:

nickculbertson

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
The problem is that once the two frames intersect they continue to intersect in the subsequent movements causing the method to be called repeatedly. So the quick dirty fix would be to move one of the objects off the screen and out of sight -50, -50.

Nick
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
The problem is that once the two frames intersect they continue to intersect in the subsequent movements causing the method to be called repeatedly. So the quick dirty fix would be to move one of the objects off the screen and out of sight -50, -50.

Nick
Thanks nickculbertson, I never thought of this dirty fix :)
I just added these two lines then it works.
Code:
.......
.......
-(void)checkcollision{
	if (CGRectIntersectsRect(box1.frame, ball.frame)){
		[B][COLOR="Blue"]CGPoint newCenter = CGPointMake(-50, -50);
		box1.center = newCenter;[/COLOR][/B]
.....
.....