2011-10-03 122 views
1

我已經旋轉精靈90.我檢查旋轉精靈的觸摸位置如下:如何檢測上旋轉精靈觸摸在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"); 

什麼是這個代碼中的錯誤?我沒有得到「嗨」。如何檢測我們是否點擊該旋轉的精靈?

回答

0

ccsprite的position屬性給出了中心座標而不是左上角的位置。

你可以得到矩形了這樣

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

我不知道,如果你需要減少或增加寬度的一半,但我認爲你可以做一個小[R & d就可以了。

+0

我已經給火柴的定位點作爲matchstick.anchorPoint = ccp(0,0); – Mythili

+0

是否將接收點轉換爲GL點? – Robin

+0

如果不是,則使用[[ccdirector shareddirector] convertToGLPoint:location],我不確定函數的名稱是否正確。 – Robin

1

這裏是我已經添加到Kobold2D遊戲引擎兩個CCNode擴展(類)方法:

-(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]; 
} 

測試點是否在一個精靈(或標籤或其他節點),然後是簡單如:

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