Hi, i have been searching this forum without finding a question to this problem.
I am developing a game, and hopefully different enemies will have different amount of lives.
With my code so far, the ones that are supposed to have 2 lives, have randomly 1 or 2 lives,
the ones that should have 3 lives have randomly 1,2 or 3 lives,
and i can't make it right for some reason. Please take a look at the methods and see if you can figure
out what is wrong. Thx =)
This is my create method:
-(id) makeSpaceRockX: (cpSpace *)space x:(float) x y:(float)y hitted:(int) hitted
{
RockSprite = [[Sprite spriteWithFile:@"EvilMonkey2.png"] retain];
hit = hitted;
cpVect verts[] = {
cpv(-35,-35),
cpv(-35, 35),
cpv(35, 35),
cpv(35,-35),
};
RockBody = cpBodyNew(200.0f, INFINITY);
RockBody->p = cpv(x, y);
RockBody->v = cpv(0, 0);
cpSpaceAddBody(space, RockBody);
rockShape = cpPolyShapeNew(RockBody, 4, verts, cpvzero);
rockShape->e = 0.9f; rockShape->u = 0.9f;
rockShape->data = RockSprite;
rockShape->collision_type = 4;
cpSpaceAddShape(space, rockShape);
ready = YES;
return self;
}
The two different collision methods, one removing the bullet, and one removing the bullet and the falling object:
static int bulletCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
GameNode *game = (GameNode*) data;
[game createExplosionX:b->body->p.x + 10 y:b->body->p.y + 10];
a->body->p = cpv(-800,-800);
b->body->p = cpv(-800,-800);
return 0;
}
static int bulletCollision2(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
GameNode *game = (GameNode*) data;
[game createExplosionX:b->body->p.x + 10 y:b->body->p.y + 10];
b->body->p = cpv(-800,-800);
return 0;
}
This is how i initialize them:
for(int i = 0; i < 3; i++) {
Rock2 *r = [[Rock2 alloc] makeSpaceRockX: space x:160 y:550 hitted:0];
[self addChild: r.getSprite];
[rocks2 addObject: r];
[r release];
}
These are my collision setup, 1 is the bullet, 3 and 4 is the falling object depending on what collision to use:
cpSpaceAddCollisionPairFunc(space, 4, 1, &bulletCollision2, self);
cpSpaceAddCollisionPairFunc(space, 3, 1, &bulletCollision, self);
And this is my update-method, that should decide how many lives each object should have:
-(void) updateRocks2
{
if(rock2go) {
for(Rock2 *r in rocks2) {
if([r getHit] == 4) {
[r setHit:1];
r.getShape->collision_type = 4;
}
if([r getHit] == 0) {
r.getShape->collision_type = 4;
}
if([r getHit] < 2) {
r.getShape->collision_type = 4;
}
if([r getHit] == 2) {
r.getShape->collision_type = 3;
}
if([r getHit] > 3) {
r.getShape->collision_type = 4;
[r setHit:1];
}
for(Bullet *b in bullets) {
if((([b getY] <= [r getY] + 40) && ([b getY] >= [r getY]-55)) &&
(([b getX] <= [r getX] + 25) && ([b getX] >= [r getX]-25))) {
[r setHit:[r getHit]+1];
}
}
if([r getY] <= -600) {
if(liv >0) score += 10;
[r resetPosition];
[r setReady: YES];
break;
}
else if([r getY] <= 0) {
if(liv >0) score -= 20;
[r resetPosition];
[r setReady: YES];
break;
}
}
}
}