2011-10-03 3 views
1

J'ai tourné sprite à 90. J'ai vérifié l'emplacement contact de l'image-objet pivotée comme suit:comment détecter le toucher sprite dans pivotée cocos2d

matchsprite.rotation=90; 

CGRect r=CGRectMake(matchstick.position.x, matchstick.position.y, matchstick.contentSize.height,matchstick.contentSize.width); 
if(CGRectContainsPoint(r, location))  
    NSLog(@"Hii"); 

Quelle est l'erreur dans ce code? Je n'ai pas eu "Hii". Comment détecter si nous tapons ce sprite pivoté ou pas?

Répondre

0

La propriété position de ccsprite vous donne la coordonnée centrale et non la position du coin supérieur gauche.

vous pouvez obtenir le rect fait comme celui-ci

CGRect r=CGRectMake(matchstick.position.x - matchstick.contentSize.width/2, matchstick.position.y + matchstick.contentSize.height/2, matchstick.contentSize.height, matchstick.contentSize.width); 

Je ne sais pas si vous devez soustraire ou ajouter la moitié de la largeur, mais je pense que vous pouvez faire un peu de R & D sur elle.

+0

J'ai donné le point d'ancrage à matchstick comme matchstick.anchorPoint = ccp (0,0); – Mythili

+0

avez-vous converti le point reçu en point GL. – Robin

+0

sinon utilisez [[ccdirector shareddirector] convertToGLPoint: location], je ne sais pas si le nom de la fonction est correct. – Robin

1

Voici les deux méthodes extension CCNode (catégorie) J'ai ajouté au moteur de jeu Kobold2D:

-(BOOL) containsPoint:(CGPoint)point 
{ 
    CGRect bbox = CGRectMake(0, 0, contentSize_.width, contentSize_.height); 
    CGPoint locationInNodeSpace = [self convertToNodeSpace:point]; 
    return CGRectContainsPoint(bbox, locationInNodeSpace); 
} 

-(BOOL) containsTouch:(UITouch*)touch 
{ 
    CCDirector* director = [CCDirector sharedDirector]; 
    CGPoint locationGL = [director convertToGL:[touch locationInView:director.openGLView]]; 
    return [self containsPoint:locationGL]; 
} 

test si un point est sur une image-objet (ou l'étiquette ou tout autre nœud), puis est comme simple comme:

UITouch* uiTouch = [touches anyObject]; 
if ([aSprite containsTouch:uiTouch]) 
{ 
    // do something 
}