★ TouchArcade needs your help. Click here to support us on Patreon.

What's the Best Approach?

10-23-2010, 08:57 AM
#1
Joined: Oct 2010
Posts: 7
What's the Best Approach?

I'm making a collision detection system, and I want to know what the best approach is. When my main character is dead (hp = 0), should I make a separate method that ends the game, or should I do it in that method in my collision detection? The thing is that the endGame method I made is in another class than the collision detection. I could obviously move it to the same class, but I don't know if that's preferred or not, or if I should just do all if it in the same method.

I appreciate all replies!
10-23-2010, 01:41 PM
#2
Joined: Oct 2010
Posts: 7
Ok, I decided to just include it all in that method. The only thing is that I noticed I get an error on this line.
Code:
		CGRect bunnyRect = CGRectMake(bunny.position.x - (bunny.contentSize.width/2), bunny.position.y - (bunny.contentSize.height/2), bunny.contentSize.width, bunny.contentSize.height);
		if (CGRectIntersectsRect(projectileRect, bunnyRect)) { //<---this line has the error "Incompatible type for argument 1 of 'CGRectIntersectsRect'
projectileRect is a rect declared in another class.

Thanks!

10-27-2010, 03:02 PM
#3
Joined: Oct 2010
Posts: 7
Does anyone have a solution? I'm not sure what's wrong.
10-27-2010, 03:14 PM
#4
Joined: Mar 2009
Posts: 659
what's the error?
10-27-2010, 03:27 PM
#5
Joined: Mar 2009
Location: Oslo, Norway
Posts: 731
Quote:
Originally Posted by Joethemonkey101 View Post
Ok, I decided to just include it all in that method. The only thing is that I noticed I get an error on this line.
Code:
		CGRect bunnyRect = CGRectMake(bunny.position.x - (bunny.contentSize.width/2), bunny.position.y - (bunny.contentSize.height/2), bunny.contentSize.width, bunny.contentSize.height);
		if (CGRectIntersectsRect(projectileRect, bunnyRect)) { //<---this line has the error "Incompatible type for argument 1 of 'CGRectIntersectsRect'
projectileRect is a rect declared in another class.

Thanks!
It seems obvious from the error message that the problem is the projectileRect but it's more or less impossible to figure this out with just two lines of code...
10-29-2010, 01:56 PM
#6
Joined: Oct 2010
Posts: 7
Sorry, here's the code containing projectileRect.

Code:
		projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2), projectile.contentSize.width, projectile.contentSize.height);
		moleToDelete = [[NSMutableArray alloc] init];
		for (NormalMole *mole in moleArray) {
			moleRect = CGRectMake(mole.position.x - (mole.contentSize.width/2), mole.position.y - (mole.contentSize.height/2), mole.contentSize.width, mole.contentSize.height);
			if (CGRectIntersectsRect(projectileRect, moleRect)) {

Last edited by Joethemonkey101; 10-29-2010 at 09:31 PM. Reason: Adjusted my code; defined projectileRect and moleRect in the header file.
10-31-2010, 02:57 PM
#7
Joined: Oct 2010
Posts: 7
Ok, I finally got it all sorted out by putting all that code in the same class. The if statement, collision code, and everything else. Thanks for all your help!