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

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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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

05-02 01:10