0

so I have a label and I would like that when an image collide with another image it increase of 10 . score = score +10; but instead of this it increase of 40 I don't know why . so here is my code :

-(id) init
{
    if( (self=[super init] )) {

        [self schedule:@selector(update:)];

        score = 0;

        scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",score] fontName:@"PUSAB___.TTF" fontSize:15 ];
        scoreLabel.position=ccp(450,30);
        [self addChild:scoreLabel];
    }
}

- (void)update:(ccTime)dt {
    if (CGRectIntersectsRect(mangeurRect, targetRect)) {
        [targetsToDelete addObject:target];     
        score=score + 10; ;// Not really, but your score changes somehow...
        [scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
    }                       
}

sorry for my english I'm french :/

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
greg rock
  • 239
  • 6
  • 14

3 Answers3

1

I think that there is no frame you are getting in the CGRectIntersectsRect. You should Try the Below code

- (void)update:(ccTime)dt {

if (CGRectIntersectsRect([mangeurRect frame], [targetRect frame])) {
    [targetsToDelete addObject:target];     
    score += 10;
    [scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}                       

}

OR

- (void)update:(ccTime)dt {

if (CGRectIntersectsRect([mangeurRect boundingBox], [targetRect boundingBox])) {
    [targetsToDelete addObject:target];     
    score += 10; 
    [scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}                       

}

By this you would probably get the score increased whenever the images collide with each other.

mattblessed
  • 772
  • 1
  • 12
  • 27
Marine
  • 1,097
  • 7
  • 19
0

You should probably check whether your target is not already in targets to delete as follows, in case the process that actually empties the targetsToDelete structure is somehow delayed for a few ticks:

- (void)update:(ccTime)dt {
    if ([targetsToDelete containsObject:target]) return;    // already scored.
    if (CGRectIntersectsRect(mangeurRect, targetRect)) {
        [targetsToDelete addObject:target];     
        score=score + 10; ;// Not really, but your score changes somehow...
        [scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
    }                       
}

the above assumes that targetsToDelete is an NSMutableArray.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
0

Your update method is not complete. Try this:

- (void)update:(ccTime)dt {
    if (CGRectIntersectsRect(mangeurRect, targetRect)) {
        [targetsToDelete addObject:target];     
        score=score + 10; ;// Not really, but your score changes somehow...
        [scoreLabel setString: [NSString stringWithFormat:@"%d",score]];

        //do something with your targetsToDelete array.. 
        for (CCSprite *target in targetsToDelete) {
         //[_targets removeObject:target]; //uncomment this line, if you have saved your targets in a _targets array
         [self removeChild:target cleanup:YES];                                 
        }
    }                       
}

Ref: http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial

Saikat
  • 2,613
  • 1
  • 22
  • 14