【转】获取Sprite的实际Rect

Posted on 2013-08-03 17:02  Flex/AS Programmer  阅读(304)  评论(0编辑  收藏  举报

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

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

bool CCRect::containsPoint(const CCPoint& point) const来判断。

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

CCSprite *sprite = CCSprite::create("test.png");
    CCSize contentSize = sprite->getContentSize();
    CCPoint position = sprite->getPosition();
    CCPoint anchorPoint = sprite->getAnchorPoint();
    CCRect rect = CCRectMake(position.x - anchorPoint.x * contentSize.width , position.y - anchorPoint.y * contentSize.height,contentSize.width, contentSize.height);

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

Copyright © 2024 Flex/AS Programmer
Powered by .NET 8.0 on Kubernetes