获取Sprite的实际Rect

[来源] 达内    [编辑] 达内   [时间]2012-09-08

判断点击是否点击在了一个精灵上, 其实就是判断一个点是否在一个矩形内

判断点击是否点击在了一个精灵上, 其实就是判断一个点是否在一个矩形内。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">cocos2d-x的2.0.2版本可以使用CCRect的函数

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); " class="p1">bool CCRect::containsPoint(const  CCPoint& point) const

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); " class="p1">来判断。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); " class="p1">找出Sprite的Rect很重要了,简单搜索了下,发现网上普遍没有考虑Sprite的AnchorPoint, 所以导致判断出错。

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
 1
 CCSprite *sprite = CCSprite::create("test.png
"); 
 2

     
 3     CCSize s = sprite->getContentSize(); 
 4     CCPoint p = sprite->getPosition(); 
 5     CCPoint ap = sprite->getAnchorPoint(); 


 6
     
 7     CCRect rect = CCRectMake( 
 8                             p.x - ap.x * s.width , 
 9                              p.y - ap.y * s.height, 


10                              s.width, s.height);
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">这样就能获取到Sprite真实的Rect

资源下载