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.)
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.
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?
> [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];
}
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.
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.
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
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.
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