Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-27-2011, 06:22 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 35
Kenenth is on a distinguished road
Default How can I detect if two images from different NSMutableArrays collide?

I'm having a bit of difficultys with detecting collision from two seperate NSMutableArrays. I have figured out that you cant just nest a for loop inside of another, because I need to make changes(delete both of the items from each array if they collide). It works sometimes but most of the times it crashes. this is my current attempt:

Code:
		for (int objectCounter1=[object1Array count]-1; objectCounter1>= 0; objectCounter1--){
			for (int objectCounter2=[object2Array count]-1; objectCounter2>= 0; objectCounter2--){
				UIImageView *Object1 = [object1Array objectAtIndex:objectCounter1];
				UIImageView *Object2 = [object2Array objectAtIndex:objectCounter2];
				if( CGRectIntersectsRect(Object1.frame, Object2.frame)) {
					[Object1 removeFromSuperview];
					[object1Array removeObjectAtIndex:objectCounter1];
					Object 1 = nil;
					[Object2 removeFromSuperview];
					[object2Array removeObjectAtIndex:objectCounter2];
					Object2= nil;
					score = score+50;
                                        }
                         }
            }
This does not work. It crashes every few times, saying something along the lines of [NSMutableArray objectAtIndex:] index 2 beyond bounds (0....1).
This is very complicating explaining why it doesnt work, but here you go.

say if you start with 3 items in each array. item one at index 0, item 2 at index 1, and item 3 at index 2. If you loop through the array and item 2 from both arrays collide. Then they both get deleted at the index they were found. But, the for loop is still going, and It still thinks there is an Index where both of the items were deleted, so it tries to run the loop attempting to get objectatindex:2, even though an Item has just been deleted from each array, so that means only 2 objects left in each array(the bound being 0...1) .

is there any way I can accomplish this task without getting the crash and it working fully functional? Thanks a million in advance.
Kenenth is offline   Reply With Quote
Old 04-27-2011, 08:47 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

It's a big NO NO to try and remove objects from an array while you are iterating over it. This is causing your index out of bounds problems.

A better approach would be to create two temporary arrays. When you detect a collision, then add the objects to their respective temp array (object1 goes into object1 temp array, object2 goes into object 2 temp array). AFTER youf nested for loops, you can then iterate over each of the items in those arrays and do something like this:

Code:
for( UIImageView* obj1 in tempArray1 )
{
   [obj1 removeFromSuperview];
   [object1Array removeObject: obj1];
    score+=50;
}

//do the same thing for tempArray2 and remove those objects from object2Array
smithdale87 is offline   Reply With Quote
Old 04-27-2011, 09:15 AM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Iterating over the array backwards is fine; it's my preferred way of doing this. Your only problem is the nested loops; if your object1[n] hits something in the middle of the object2 array you remove both items; *but* you keep looping the object2 array and checking it against the now-dead object1[n]. You need to break out of the inner loop with a "break" statement. Try this:

Code:
for (int objectCounter1=[object1Array count]-1; objectCounter1>= 0; objectCounter1--){
	for (int objectCounter2=[object2Array count]-1; objectCounter2>= 0; objectCounter2--){
		UIImageView *Object1 = [object1Array objectAtIndex:objectCounter1];
		UIImageView *Object2 = [object2Array objectAtIndex:objectCounter2];
		if( CGRectIntersectsRect(Object1.frame, Object2.frame)) {
			[Object1 removeFromSuperview];
			[object1Array removeObjectAtIndex:objectCounter1];
			Object 1 = nil;
			[Object2 removeFromSuperview];
			[object2Array removeObjectAtIndex:objectCounter2];
			Object2= nil;
			score = score+50;

			//smasher change
			break;
		}
	}
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-27-2011, 09:27 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 35
Kenenth is on a distinguished road
Default

Thank you so much. smithdale, its a good idea, but for smashers all i have to write is "break;".This has been troubling me for about a week. Now my app is 100% working . Again Thanks!
Kenenth is offline   Reply With Quote
Reply

Bookmarks

Tags
array, collision detection, crash, nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 346
12 members and 334 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, Nobbsy, oztemel, pbart, PlutoPrime, samdanielblr, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,897
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0