Sunday, 8 September 2013

strange sprite's behaviour before time?

strange sprite's behaviour before time?

I have a problem with a game i'm making, it is a simple shoot'em game
where monsters come from the right of the screen and move to the left and
you have to shoot them. I wrote a method that detects collision of
monsters with arrows(knife in code), a method to calculate the score, and
a method to kill all monsters instantly with a press of a button. The
problem is sometimes when i shoot the arrows disappear before they hit a
monster and the score is increased??!!
//detect collision
- (void)update:(ccTime)dt
{
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
for (knife in knives)
{
monstersToDelete = [[NSMutableArray alloc] init];
for (monster in monsters) {
if (CGRectIntersectsRect(knife.boundingBox, monster.boundingBox))
{
s++;
[monstersToDelete addObject:monster];
[projectilesToDelete addObject:knife];
}
}
for (monster in monstersToDelete)
{
[monsters removeObject:monster];
[self removeChild:monster cleanup:YES];
}
if (monstersToDelete.count > 0)
{
[projectilesToDelete addObject:knife];
}
[monstersToDelete release];
}
for (knife in projectilesToDelete)
{
[knives removeObject:knife];
[self removeChild:knife cleanup:YES];
}
[projectilesToDelete release];
}
//count the score
-(void)setScore:(ccTime)dt
{
s= s + skillKill;
[self removeChild:tempScoreOnScreen];
[self removeChild:scoreOnScreen];
score = [NSString stringWithFormat:@"%i",s];
skillKill = 0;
scoreOnScreen = [CCLabelTTF labelWithString:score
fontName:@"Arial" fontSize:20];
scoreOnScreen.color = ccBLACK;
scoreOnScreen.position = ccp(450,300);
[self addChild:scoreOnScreen];
}
\\kill monsters instantly
-(void) killAll
{
for(monster in nukeKills)
{
[self removeChild:monster cleanup:YES];
NSLog(@"%i",[nukeKills count]);
//number of monsters on the screen
skillKill= [nukeKills count];
}
[nukeKills removeAllObjects];
}
note that i add all monsters created by ccsprite in nsmutablearray
monsters automatically and arrows (knife) in nsmutablearray knives also.
i just dont know why that problem of arrows disappearing happens any help
is appreciated

No comments:

Post a Comment