2013-11-09 48 views
0

我有CCSprite這(原因我會在這裏粉飾)具有圍繞其邊緣的一些填充。我從精靈表創建精靈,並且它不斷地動畫。在這裏,我添加了一個半透明的藍色精靈,以顯示精靈的contentSize。我也以各地(兩者)繪製邊框打開CC_SPRITE_DEBUG_DRAW精靈:訪問紋理的矩形在cocos2d的精靈

enter image description here

因此,藍色方框代表的CCSpriteboundingBox/contentSize性能。質地。這是正確的,所需的功能。

但是......你可以看到,CC_SPRITE_DEBUG_DRAW能夠識別繪製質感的實際邊緣。我想訪問實際的「繪製區域」(例如,作爲CGRect)。換句話說:我希望能夠檢測如果用戶觸摸的單元上,而不是簡單的藍色框(boundingBox)。

我怎樣才能訪問該CGRect

回答

1

望着調試抽獎代碼,我發現這一點:

#if CC_SPRITE_DEBUG_DRAW == 1 
    // draw bounding box 
    CGPoint vertices[4]={ 
     ccp(_quad.tl.vertices.x,_quad.tl.vertices.y), 
     ccp(_quad.bl.vertices.x,_quad.bl.vertices.y), 
     ccp(_quad.br.vertices.x,_quad.br.vertices.y), 
     ccp(_quad.tr.vertices.x,_quad.tr.vertices.y), 
    }; 
    ccDrawPoly(vertices, 4, YES); 
#elif CC_SPRITE_DEBUG_DRAW == 2 
    // draw texture box 
    CGSize s = self.textureRect.size; 
    CGPoint offsetPix = self.offsetPosition; 
    CGPoint vertices[4] = { 
     ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y), 
     ccp(offsetPix.x+s.width,offsetPix.y+s.height), 
      ccp(offsetPix.x,offsetPix.y+s.height) 
    }; 
    ccDrawPoly(vertices, 4, YES); 
#endif // CC_SPRITE_DEBUG_DRAW 

看起來你也許可以讓你從精靈的quad財產想要什麼。或者,也許它的第二個解決方案,因爲我不知道什麼被這裏的邊框和紋理箱的Cocos2D手段。

+0

我已經花了,而與此周圍碴,至今有運氣不佳,這就是爲什麼我結束在這裏張貼希望有一個簡單的解決方案...欣賞提示,但! –

1

這是我想出的代碼,如在CCSprite我的自定義子類的函數:

// In local space 
- (CGRect)hitArea { 
    CGPoint bl = CGPointMake(MIN(_quad.tl.vertices.x, _quad.bl.vertices.x), MIN(_quad.bl.vertices.y, _quad.br.vertices.y)); 
    CGPoint tr = CGPointMake(MAX(_quad.tr.vertices.x, _quad.br.vertices.x), MAX(_quad.tl.vertices.y, _quad.tr.vertices.y)); 
    return CGRectMake(bl.x, bl.y, tr.x - bl.x, tr.y - bl.y); 
} 

// In game space, like how .boundingBox works 
- (CGRect)hitBox { 
    return CGRectApplyAffineTransform(self.hitArea, [self nodeToParentTransform]); 
}