获取Sprite的实际Rect

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

cocos2d-x的2.0.2版本可以使用CCRect的函数

bool CCRect::containsPoint(const CCPoint& point) const

来判断。

找出Sprite的Rect很重要了,简单搜索了下,发现网上普遍没有考虑Sprite的AnchorPoint, 所以导致判断出错。

 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);

这样就能获取到Sprite真实的Rect

 

新浪微博:http://weibo.com/young40

posted on 2012-09-03 00:07  young40  阅读(1460)  评论(0编辑  收藏  举报

导航