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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2011, 06:35 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 12
Zanzibar7 is on a distinguished road
Default Need help with looping through an array

I'm trying to make a game where you move your player around the screen and collect gold bars. I made an array that i'm going to keep all of my gold bars in, and I want to loop through that array and check for a collision between the player and each gold bar. In my code right now I only have one gold bar but later on I plan on making several different levels that use different numbers of gold bars, so that's why I want a mutable array.

In the header file I have this:

Code:
@interface Game1ViewController : UIViewController {
UIImageView *goldBar;
NSMutableArray *goldBars;
}
@property (nonatomic, retain) IBOutlet UIImageView *goldBar;
@property (nonatomic, retain) NSMutableArray *goldBars;
In the implementation file I have this:

Code:
@implementation Game1ViewController
@synthesize goldBar;
@synthesize goldBars;
- (void)viewDidLoad {
	[goldBars addObject:goldBar];
}
- (void)gameStatePLayNormal {
	// collision between player and each gold bar
	for (UIImageView *image in goldBars) {
		if (CGRectIntersectsRect(player.frame, image.frame)) {
			score++;
			[goldBars removeObject:image];
		}
	}
}
- (void)viewDidUnload {
	self.goldBar = nil;
}
- (void)dealloc {
	[goldBar release];
	[goldBars release];
}
That's all the code relevant to my question. I don't receive any errors when I run it, but it doesn't do anything in the for loop. I think there is something I have to do in the viewDidLoad method to initialize the array but i don't know exactly what I need to do. Is there a better way of doing this or am I on the right track, and what do I need to do to get my for-loop working? Thanks in advance for any help.

Last edited by Zanzibar7; 11-08-2011 at 09:58 PM.
Zanzibar7 is offline   Reply With Quote
Old 11-08-2011, 11:29 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

You need to initialize your array.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 11-09-2011, 01:24 AM   #3 (permalink)
Registered Member
 
SundialSoft's Avatar
 
Join Date: Oct 2010
Location: Scotland
Posts: 176
SundialSoft is on a distinguished road
Default

Quote:
Originally Posted by Zanzibar7 View Post
I'm trying to make a game where you move your player around the screen and collect gold bars. I made an array that i'm going to keep all of my gold bars in, and I want to loop through that array and check for a collision between the player and each gold bar. In my code right now I only have one gold bar but later on I plan on making several different levels that use different numbers of gold bars, so that's why I want a mutable array.

In the header file I have this:

Code:
@interface Game1ViewController : UIViewController {
UIImageView *goldBar;
NSMutableArray *goldBars;
}
@property (nonatomic, retain) IBOutlet UIImageView *goldBar;
@property (nonatomic, retain) NSMutableArray *goldBars;
In the implementation file I have this:

Code:
@implementation Game1ViewController
@synthesize goldBar;
@synthesize goldBars;
- (void)viewDidLoad {
	[goldBars addObject:goldBar];
}
- (void)gameStatePLayNormal {
	// collision between player and each gold bar
	for (UIImageView *image in goldBars) {
		if (CGRectIntersectsRect(player.frame, image.frame)) {
			score++;
			[goldBars removeObject:image];
		}
	}
}
- (void)viewDidUnload {
	self.goldBar = nil;
}
- (void)dealloc {
	[goldBar release];
	[goldBars release];
}
That's all the code relevant to my question. I don't receive any errors when I run it, but it doesn't do anything in the for loop. I think there is something I have to do in the viewDidLoad method to initialize the array but i don't know exactly what I need to do. Is there a better way of doing this or am I on the right track, and what do I need to do to get my for-loop working? Thanks in advance for any help.
As stated you need an alloc/init statement:
Code:
       goldBars = [[NSMutableArray alloc] init];
or you could initWithObjects and add your initial bars that way.

Have you decided how to write the game yet? 2D or 3D. Just asking because there's a nice demo for Cocos3D using the accelerometer to move around 'catching' blocks so may be similar to what you plan to do.
SundialSoft is offline   Reply With Quote
Old 11-09-2011, 01:11 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 12
Zanzibar7 is on a distinguished road
Default Thanks

Thanks, I got it to work now. I knew I was missing something simple like like that, but I couldn't figure out how to write it.
Zanzibar7 is offline   Reply With Quote
Old 11-09-2011, 02:51 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 12
Zanzibar7 is on a distinguished road
Default

Quote:
Originally Posted by SundialSoft View Post
Have you decided how to write the game yet? 2D or 3D. Just asking because there's a nice demo for Cocos3D using the accelerometer to move around 'catching' blocks so may be similar to what you plan to do.
It's a 2d game and I know I should probably be using cocos2d to make it, and I might recreate it later using cocos2d, but for now I'm going to stick with the regular frameworks. I'm not really in the mood right now to learn how to use it.
Zanzibar7 is offline   Reply With Quote
Old 11-17-2011, 06:27 PM   #6 (permalink)
Registered Member
 
Naughty_Ottsel's Avatar
 
Join Date: Aug 2008
Location: Gillingham, Dorset, UK
Age: 19
Posts: 218
Naughty_Ottsel is on a distinguished road
Send a message via MSN to Naughty_Ottsel
Default

The others have answered correctly however for convenience it may be helpful to remove the @synthesize goldBars and lazily instantiate the array:

Code:
-(NSMutableArray *)goldBars
{
if (!_goldBars) _goldBars = [[NSMutableArray alloc]init];
}
return _goldBars
}
Its not majorly important, but means you aren't creating the array until it is needed and that is simply done via the usual dot notation, or calling this method. It always helps to remember that unless it is in the .xib and non primitive properties are not inited through the @property/@synthesize calls.
__________________
Follow me on Twitter
Naughty_Ottsel is offline   Reply With Quote
Reply

Bookmarks

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: 376
10 members and 366 guests
apatsufas, comicool, Creativ, Dalia, dansparrow, LunarMoon, mer10, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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