あなたのソースコードは少ないです。ただし、インターフェイスで宣言される整数変数を使用できます。実装では、整数変数を0に設定し(initメソッドで)、新しいバグを作成するときは、整数値をもう1つ増やし、バグスプライトタグを次のように整数値に設定します。
// Interface
@interface yourScene:CCLayer {
int bugsCount; // add new variable for counting current bugs
}
// implementation
@implementation
-(id) init {
if( (self=[super init])) {
// Your code in init
// integer value for counting current bugs
bugsCount = 0;
}
return self;
}
-(void) addNewBug {
// your code of adding bugs
CCSprite *bug = [CCSprite spriteWithFile:@"bug.png"];
bugsCount++;
bug.tag = bugsCount; // Your bug sprite tag should be equal current bugsCount value
bug.position = ccp(x,y);
[self addChild:bug];
}
ccTouchEndedメソッドでは、forループを実行して、次のように選択されたバグを検出します。
-(void) ccTouchesEnded:(NSSet*)touches withEvent:(id)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
location = [self convertToNodeSpace:location];
// Detecting bug by touch
for (i=1; i <= bugsCount; i++) {
// get bug sprite by tag (from bugsCount idea)
CCNode *bug = [self getChuldByTag:i];
if (CGRectContainsPoint(bug.boundingBox, location)) {
// add new score (now this will work)
score += 1;
NSLog(@"%i", score);
// remove bug from scene
[self removeChildByTag:1 cleanup:YES];
// decrease number of bugs
bugsCount--;
}
}
}