2013-04-22 80 views
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; 
} 

当我点击两次弓时,我的箭头重叠在另一个上面。CCSprite重叠在另一个上

有帮助吗? enter image description here

+0

我读HTTP(如果有任何修正语法错误)://计算器.com/questions/6578/understanding-reference-counting-with-cocoa-and-objective-c as reference.still我无法解决我的问题。 – 2013-04-22 12:55:14

+0

我的项目中只有一个名称为_nextProjectile的箭头。 和我使用runAction中的finishShoot方法释放它。 – 2013-04-22 13:06:19

回答

0

您需要删除以前的箭头,然后释放下一个箭头。我认为你正在制作一个射击游戏,在触摸屏幕时释放箭头。我建议去掉最后一支箭它的动作完成时

+0

只要释放它,它就不会从场景中移除。你需要添加[self removeChild:_nextProjectile cleanUp:YES]; – 2013-04-22 13:09:20

+0

我写了[self removeChild:_nextProjectile cleanup:YES];后 [imgArrow runAction:[CCSequence动作: [CCRotateTo actionWithDuration:rotateDuration角:cocosAngle], [CCCallFunc actionWithTarget:自选择器:@selector(finishShoot)], 零]]; 它仍然没有工作:( – 2013-04-22 13:23:05

+0

我在finishShoot方法中添加箭头,因为我想只添加射出投射物的箭头,而不是每个人。 – 2013-04-22 13:33:54

0

加入我们已经删除箭头之前试试下面的代码,在里面,

-(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 

} 
+0

我写了[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; 但仍然结果是一样的。 – 2013-04-23 05:38:55

+0

,因为我已经给_nextProjectile分配了标签.. 我已经删除了另一个标签..现在当我第一次发射箭头时,它会移动到正确的方向..但它并没有第二次移动。 – 2013-04-23 05:45:54

+0

你的意思是孩子没有被移除,确保你没有添加来自其他地方,就像在触摸开始或移动或任何连锁调用的方法。 – rptwsthi 2013-04-23 05:51:26