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 07-18-2011, 10:22 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Question Using an Array for collision detection

I have a game where a bird files to a number of perches,
I need to detect a collision with the perch
Rather then using a load of if statements, I would like to refer to them in an array.
if (CGRectIntersectsRect(bird.frame, perch1.frame)) { }
if (CGRectIntersectsRect(bird.frame, perch2.frame)) { }
if (CGRectIntersectsRect(bird.frame, perch3.frame)) { } and so on……

NSMutableArray* percharray = [NSMutableArray array];
[percharray addObjecterch1]; // Smilie is a Colon ?
[percharray addObjecterch2];
[percharray addObjecterch3];
... etc

for (int i = 0; i < num_perches; i++) {
if (CGRectIntersectsRect(bird.frame, percharray[i].frame)) { code her }
}

But I get error

‘struct NSMutbleArray’ has no member named ‘frame’

Any help please
kev1953 is offline   Reply With Quote
Old 07-18-2011, 10:23 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

smilie doesnt exist if you use code tags
Meredi86 is offline   Reply With Quote
Old 07-18-2011, 10:36 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You access objects in an NSArray with objectAtIndex:
array[i] is for C arrays.

Code:
for (int i = 0; i < num_perches; i++) {
CGRect perchFrame = [(UIView *)[percharray objectAtIndex:i] frame];
if (CGRectIntersectsRect(bird.frame, perchFrame)) { code her }
}
harrytheshark is offline   Reply With Quote
Old 07-18-2011, 10:58 AM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Smile

harrytheshark, Many thanks for your post, works great
kev1953 is offline   Reply With Quote
Old 07-19-2011, 12:31 AM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Question More help please on NSMutableArray

How do I declare my NSMutableArray to be Global.
and how to use the array to set new centers for the perches
kev1953 is offline   Reply With Quote
Old 07-19-2011, 05:43 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Declare it in the header file.

Can you be more specific about the second question?
harrytheshark is offline   Reply With Quote
Old 07-19-2011, 07:44 AM   #7 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Question

Declare it in the header file. Sorry if I am a total dunce. is this correct

in header file NSMutableArray *percharray;
then @property (nonatomic, retain) NSMutableArray *parcharray;

In M file in viewDidLoad
Code:
[percharray addObject:perch1];
etc
This runs OK but is not detecting a collision?


second question sorted

Code:
CGPoint perchCenter = [(UIView *)[percharray objectAtIndex:p] center];
perchCenter = newPosition;
kev1953 is offline   Reply With Quote
Old 07-19-2011, 07:47 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You need to initialise the array.
Code:
- (void)viewDidLoad {
    NSMutableArray * array = [[NSMutableArray alloc] init];
    [self setPercharray:array];
    [array release];
}
You then have to release it in dealloc.

For the second part, just use setCenter: on the view.
harrytheshark is offline   Reply With Quote
Old 07-19-2011, 08:18 AM   #9 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Default

Many thanks again.

Just one last one as it still relates to the array
When a bird is shot, I wish to rotate it 180 so it hangs upsidedown.
(the birds are also in an array)
Code:
CGAffineTransform birdTransform = [(UIView *)[birdarray objectAtIndex:p] transform];
CGAffineTransform birdRotate = CGAffineTransformMakeRotation(degreesToRadians(180));
birdTransform = birdRotate;
This seems ok but does not work?
kev1953 is offline   Reply With Quote
Old 07-19-2011, 08:20 AM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You have to actually set the transform.
Code:
[(UIView *)[birdarray objectAtIndex:p] setTransform:birdRotate];
harrytheshark is offline   Reply With Quote
Old 07-19-2011, 08:32 AM   #11 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 26
kev1953 is on a distinguished road
Default

Many Many Thanks for your help
kev1953 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, collision detection

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: 410
14 members and 396 guests
blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, Mah6447, n00b, nyoe, pungs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

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