0
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
 {

     _nextProjectile = [[CCSprite spriteWithFile:@"arrow.png"]retain];
     _nextProjectile.position = imgArrow.position;

        [imgArrow runAction:[CCSequence actions:
                            [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                            [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)],
                            nil]];
    //Some code 

    }

- (void)finishShoot {

    // Ok to add now - we've finished rotation!

    [self addChild:_nextProjectile];
    [_projectiles addObject:_nextProjectile];

    // Release
    [_nextProjectile release];
    _nextProjectile = nil;
}

When I click on bow twice my arrow overlaps one over another.

any help ?!enter image description here

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
V.D
  • 403
  • 1
  • 6
  • 22
  • i read http://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-and-objective-c as reference.still i'm not able to resolve my problem. – V.D Apr 22 '13 at 12:55
  • There is only one arrow in my project having name _nextProjectile. and i'm releasing it using finishShoot method in runAction. – V.D Apr 22 '13 at 13:06

2 Answers2

0

You need to remove your previous arrow before releasing next arrow with touch. I think you are making a shooting game where an arrow is released as the screen is touched. I would recommend removing the last arrow when it's action is complete

Sahil Arora
  • 213
  • 3
  • 8
  • It is not removed from the scene by just releasing it. You need to add [self removeChild: _nextProjectile cleanUp:YES]; – Sahil Arora Apr 22 '13 at 13:09
  • i wrote [self removeChild:_nextProjectile cleanup:YES]; after [imgArrow runAction:[CCSequence actions: [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle], [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)], nil]]; still it didn't work :( – V.D Apr 22 '13 at 13:23
  • i'm adding arrow in finishShoot method because i want to add only that arrow which shoots the projectile.not each of them.. – V.D Apr 22 '13 at 13:33
  • Your code is confusing. Elaborate what exactly you require here – Sahil Arora Apr 22 '13 at 13:33
  • for(CCSprite *arrow in _projectiles){ [self removeChild:arrow cleanup:YES]; } Add this code at the start of your touch method. It will remove all the existing arrows and there will be only one when you add – Sahil Arora Apr 22 '13 at 13:41
  • When arrow fires from bow it will collide from projectiles which comes randomly from scene.If arrow collide from projectile it should remove from screen.or if it didn't collide it also should remove when it goes off screen.. here,if i press once on my bow it will fire and remove from screen very nicely but when i click twice on bow once my arrow is fire and then it appears on my bow. – V.D Apr 22 '13 at 13:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28665/discussion-between-v-d-and-sahil-arora) – V.D Apr 22 '13 at 13:45
  • Still confusing.. Try the code above and let me know if it suits your need. Else we will do something else (y) – Sahil Arora Apr 22 '13 at 13:45
0

Try this code, in it before adding we have removed arrow, (fix syntactical error if there any)

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
 {
 [self removeChild:[self childWithTag:101]];
 _nextProjectile = [[CCSprite spriteWithFile:@"arrow.png"]retain];
 _nextProjectile.tag = 101;
 _nextProjectile.position = imgArrow.position;

    [imgArrow runAction:[CCSequence actions:
                        [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                        [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)],
                        nil]];
//Some code 

}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • i wrote [self removeChild:_nextProjectile cleanup:YES]; [self addChild:_nextProjectile]; [self removeChildByTag:101 cleanup:YES]; _nextProjectile = [[CCSprite spriteWithFile:@"arrow.png"]retain]; _nextProjectile.tag = 101; _nextProjectile.position = imgArrow.position; but still result is same. – V.D Apr 23 '13 at 05:38
  • because i already assign tag to _nextProjectile.. i have remove another tag..now when i fire arrow for 1st time it will move into proper direction..but it didn't move for 2nd time. – V.D Apr 23 '13 at 05:45
  • You mean child is not getting removed, make sure you have not added is from some where else then, like in touches begun or move or any method that get called in chain. – rptwsthi Apr 23 '13 at 05:51
  • yes,,i have added in my finishShoot method...so,i have removed it from that method but still result is same. – V.D Apr 23 '13 at 05:59