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 01-31-2011, 07:13 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
jackosx is on a distinguished road
Default Help needed in creating multiple enemies

I am new to programming in general, and am trying to create multiple enemies. What is the easiest way to do this? Should I create an enemy class?

Last edited by jackosx; 02-01-2011 at 10:18 AM.
jackosx is offline   Reply With Quote
Old 02-04-2011, 03:17 PM   #2 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

Quote:
Originally Posted by jackosx View Post
I am new to programming in general, and am trying to create multiple enemies. What is the easiest way to do this? Should I create an enemy class?
Have an enemy class and then just create instances of that class to your hearts content, then make the proper connections in interface Builder (at least that's how I do it.)
missing_no is offline   Reply With Quote
Old 02-05-2011, 10:38 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

Yes, you should probably create an Enemy class; then you can write a method that creates a bunch enemies and adds them to an array. That makes it easy to loop through the array and move each object, check for collisions, etc.

Search this board for "Random Rocks" for some code samples.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-06-2011, 10:55 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
jackosx is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Yes, you should probably create an Enemy class; then you can write a method that creates a bunch enemies and adds them to an array. That makes it easy to loop through the array and move each object, check for collisions, etc.

Search this board for "Random Rocks" for some code samples.
I read that many times, and it was extremely useful and so far I have this
Code:
	if (therearegrunts == TRUE) {
		for (id Grunt  in enemies) {
	[Grunt setCenter:CGPointMake([Grunt center].x + 1, [Grunt center].y)];
			if (CGRectIntersectsRect([Grunt frame], you.frame)) {
				[Grunt removeFromSuperview];
				[enemies removeObject: Grunt]; //this line causes it to crash
			}
	}
Moving works, and removing them when they collide works, but I end up with an array that is a couple thousand enemies long, how do remove only the enemy that it is hit from the array?
jackosx is offline   Reply With Quote
Old 02-06-2011, 11:35 AM   #5 (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

> [enemies removeObject: Grunt]; //this line causes it to crash

You can't remove an object from an array while you're looping through it with fast enumeration; fast enumeration means this:

Code:
for (id Grunt  in enemies) {
    if(something)
         [enemies removeObject:grunt]; //this line causes it to crash
}
You can fix this by using a regular for loop

Code:
for (int i=0; i< [enemies count]; i++) {
     id grunt = [enemies objectAtIndex:i];

    if(something)
         [enemies removeObject:grunt];
}
That code has a subtle bug that skips an object in the array every time you delete a grunt; plus it calls [enemies count] every iteration. You can do better by going backwards:

Code:
for (int i=[enemies count]-1; i>=0 i--) {
     id grunt = [enemies objectAtIndex:i];

    if(something)
         [enemies removeObject:grunt];
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-06-2011, 02:24 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
jackosx is on a distinguished road
Default

Thanks, it works great! Is their a way to add a health value to the enemies, so they don't disappear as soon as they are touched? In the grunt.h I made an integer called health, but grunt.health doesn't work in my viewcontroller.
jackosx is offline   Reply With Quote
Old 02-06-2011, 04:18 PM   #7 (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

Quote:
Originally Posted by jackosx View Post
Thanks, it works great! Is their a way to add a health value to the enemies, so they don't disappear as soon as they are touched? In the grunt.h I made an integer called health, but grunt.health doesn't work in my viewcontroller.
You need to look up "properties" - setting grunt.health won't work until you create a "property" for that variable.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-06-2011, 04:51 PM   #8 (permalink)
Dr. Gerbil
 
1337Skittles's Avatar
 
Join Date: Aug 2010
Location: Kentucky
Posts: 129
1337Skittles is on a distinguished road
Default

I might have done it the long way but here's how I did it in my game. for each enemy you have in your array declare an integer value and set it to zero. for example if you have 5 enemies put:

int enemyHealth1 = 0;
int enemyHealth2 = 0;
int enemyHealth3 = 0;
int enemyHealth4 = 0;
int enemyHealth5 = 0;

and then when your enemy collides with something I put something like

if(theEnemyThatColided == yourEnemyArray object at index:0)
{
enemyHealth1++;
}
else if(theEnemyThatColided == yourEnemyArray object at index:1)
{
enemyHealth2++;
}

and so on..

then you'll want to set a health limit like

if(enemyLife1 == 10||enemyLife2 == 10)
{
//make it explode or something idk
}

I'm just going off of the top of my head, I can't remember exactly how I did it without looking and my laptop is nowhere near me
1337Skittles is offline   Reply With Quote
Old 02-06-2011, 06:54 PM   #9 (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

I'd stick with arrays - that's going to be a lot of typing by the time you have 20 enemies.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-21-2011, 07:59 AM   #10 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
jackosx is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
You need to look up "properties" - setting grunt.health won't work until you create a "property" for that variable.
I made a health property like this @property int health; and I synthesized it, but I still get request for something not in structure or union
jackosx is offline   Reply With Quote
Old 02-21-2011, 10:45 AM   #11 (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

Quote:
Originally Posted by jackosx View Post
I made a health property like this @property int health; and I synthesized it, but I still get request for something not in structure or union
What class has the health property? Did you import the header for that class into the class where you're calling blah.health? When you try to access blah.health, is "blah" declared as a pointer to the right class?

In your code above you were using "id" for everything, which is a generic pointer to any object. The compiler won't let you used properties on an id, because it doesn't know the right class.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-21-2011, 04:11 PM   #12 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
jackosx is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
What class has the health property? Did you import the header for that class into the class where you're calling blah.health? When you try to access blah.health, is "blah" declared as a pointer to the right class?

In your code above you were using "id" for everything, which is a generic pointer to any object. The compiler won't let you used properties on an id, because it doesn't know the right class.
Thanks I used [[enemies objectAtIndex:i] health] and it works
jackosx 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: 417
10 members and 407 guests
7twenty7, chemistry, ChrisYates, gmarro, hussain1982, Retouchable, skrew88, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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