2011-01-16 3 views
3

Je suivais le tutoriel de Ray pour faire un simple jeu iPhone (ici: http://goo.gl/fwPi), et j'ai décidé que je voulais que les ennemis soient éliminés quand ils sont touchés. Mon approche initiale consistait à générer une petite image-objet CCSprite sur l'emplacement tactile, puis à utiliser CGRectMake pour créer une boîte englobante de cette image-objet afin de détecter si l'image-objet était touchée. Tout comme Ray avec le projectile/ennemi. Mais bien sûr, ma façon de le faire ne marche pas et je ne peux pas me sortir de ce trou.Détecter si un sprite spécifique a été touché Cocos2d-iphone

Voici l'extrait de code pertinent. Toute aide est appréciée:

 
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    // Choose one of the touches to work with 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [self convertTouchToNodeSpace: touch]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    CCSprite *touchedarea = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(location.x, location.y, 2, 2)]; 
    touchedarea.tag = 2; 
    [self addChild:touchedarea]; 
    [_touchedareas addObject:touchedarea]; 

} 



- (void)update:(ccTime)dt { 

    NSMutableArray *touchedareasToDelete = [[NSMutableArray alloc] init]; 
    for (CCSprite *touchedarea in _touchedareas) { 
     CGRect touchedareaRect = CGRectMake(
              touchedarea.position.x, 
              touchedarea.position.y, 
              touchedarea.contentSize.width, 
              touchedarea.contentSize.height); 

     NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init]; 
     for (CCSprite *target in _targets) { 
      CGRect targetRect = CGRectMake(
              target.position.x - (target.contentSize.width/2), 
              target.position.y - (target.contentSize.height/2), 
              target.contentSize.width, 
              target.contentSize.height); 

      if (CGRectIntersectsRect(touchedareaRect, targetRect)) { 
       [targetsToDelete addObject:target];    
      }      
     } 

     for (CCSprite *target in targetsToDelete) { 
      [_targets removeObject:target]; 
      [self removeChild:target cleanup:YES];         
     } 

     if (targetsToDelete.count > 0) { 
      [touchedareasToDelete addObject:touchedarea]; 
     } 
     [targetsToDelete release]; 
    } 

    for (CCSprite *touchedarea in touchedareasToDelete) { 
     [_touchedareas removeObject:touchedarea]; 
     [self removeChild:touchedarea cleanup:YES]; 
    } 
    [touchedareasToDelete release]; 
} 
+0

j'ai posé une question sur les meilleures pratiques pour ceci: http://stackoverflow.com/questions/2900691/ meilleures pratiques-pour-manipuler-touche-à-un-ccsprite-avec-c ocos2d Espérons que cela aide! – donkim

Répondre

7

Cela ressemble à une façon très difficile de s'y prendre. Je n'ai pas codé longtemps moi-même, mais peut-être que ce qui suit pourrait vous aider. Disons que vous avez un ennemi appelé nsmutablearray et que vous ajoutez le nouvel objet ennemi à ce tableau quand vous en créez un. objet ennemi serait un ccnode et un ccsprite sein appelé _enemySprite puis faire toucher

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

    NSSet *allTouches = [event allTouches]; 
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; 
    //UITouch* touch = [touches anyObject]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    int arraysize = [enemies count]; 
    for (int i = 0; i < arraysize; i++) { 


     if (CGRectContainsPoint([[[enemies objectAtIndex:i] _enemySprite] boundingBox], location)) { 

      //some code to destroy ur enemy here 


     } 
    } 
    // NSLog(@"TOUCH DOWN"); 

} 

espérons que cette aide

+0

cela ne fonctionne souvent pas. Par exemple quand CCSprite appartient à CCLayer qui ne remplit pas tout l'écran et sa position n'est pas (0, 0) – Gargo

+0

Mais quand ça convertitToGL: m'a aidé – richy

2

Une autre façon de le faire est que le calcul de la distance entre la position de contact et vos sprites .. Si le toucher est assez proche à un de vos sprites, vous pouvez le tuer .. Quelque chose comme ça ..

for (CCSprite *sprite in anArrayThatCOntainsAllYourSprites) { 

    float distance = pow(sprite.position.x - location.x, 2) + pow(sprite.position.y - location.y, 2); 

    distance = sqrt(distance); 

     if (distance <= 10) { 
       sprite.dead = YES; 
     } 

} 
Questions connexes