2014-05-23 3 views
0

저는 cocos2d를 처음 사용하고 있으며 과일 닌자 같은 동일한 객체가 계속해서 빠져 나가는 게임을 개발 중입니다. 화면에서 손가락을 드래그하여 터치합니다. 나는 그것을 만들 때마다 스프라이트를 추가하는 NSMutableArray를 만들려고 시도했지만 넘어졌다. 그러나 그것이 사실이 아니더라도 스프라이트를 만졌다는 것을 알게된다. 스프라이트가 보이지 않는 것 같다. 내가 스프라이트에 손을 대었을 때 나는 그것을 제거했지만 아마 제거하지는 못할 것입니다.화면에 손가락을 드래그하여 배열에 포함 된 CCSprite의 터치를 감지합니다.

@interface GameScene : CCScene 
{ 
    NSMutableArray *spriteArray; 
} 

- (id)init 
{ 
    spriteArray = [[NSMutableArray alloc]init]; 
    return self; 
} 

- (void)onEnter 
{ 
    [super onEnter]; 
    [self schedule:@selector(addSprites:) interval:1.0]; 
} 


- (void)addSprites:(CCTime)dt 
{ 
    CCSprite *sprite = [CCSprite spriteWithImageNamed:@"sprite000.png"]; 

    int minX = sprite.contentSize.width/2; 
    int maxX = self.contentSize.width - sprite.contentSize.width/2; 
    int rangeX = maxX - minX; 
    int randomX = (arc4random() % rangeX) + minX; 

    sprite.position = CGPointMake(randomX, self.contentSize.height + sprite.contentSize.height); 
    [self addChild:sprite z:6]; 
    [spriteArray addObject:sprite]; 

    CCAction *actionMove = [CCActionMoveTo actionWithDuration:3.0 position:CGPointMake(randomX, -sprite.contentSize.height)]; 

    CCAction *actionRemove = [CCActionRemove action]; 
    [sprite runAction:[CCActionSequence actionWithArray:@[actionMove, actionRemove]]]; 

    if ([spriteArray count] > 50) 
    { 
     [spriteArray removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 40)]]; 
    } 
} 


-(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLoc = [touch locationInNode:self]; 

    for (CCSprite *sprite in spriteArray) 
    { 
     if (CGRectContainsPoint([sprite boundingBox], touchLoc)) 
     { 
      CCLOG(@"Touched!"); 
      CCAction *actionRemove = [CCActionRemove action]; 
      [sprite runAction:actionRemove]; 
      return; 
     } 
    } 
} 

답변

1

당신은 spriteArray에서 스프라이트를 제거되지 않습니다 다음은 내 코드입니다. 따라서 제거 된 스프라이트로 터치를 확인합니다. 시도해보십시오.

for (int i=0;i<spriteArray.count;i++) 
{ 
    //get the current sprite from the array 
    CCSprite *sprite = [spriteArray objectAtIndex:i]; 

    if (CGRectContainsPoint([sprite boundingBox], touchLoc)) 
    { 
     CCLOG(@"Touched!"); 
     CCAction *actionRemove = [CCActionRemove action]; 
     [sprite runAction:actionRemove]; 

     //remove the sprite from the array 
     [spriteArray removeObjectAtIndex:i]; 

     //decrement i to be safe if you remove the return one day 
     --i; 

     return; 
    } 
} 
+0

고마워요! 네가 옳아! 하지만 CCArray는 이제 Cocos2d의 v3에 대해 사용되지 않습니다. – swifferina

+0

여러분을 환영합니다! 지금은 사용 중단 된 것을 알았 기 때문에 내 답변에서 삭제했습니다. – robert