coccs2d-x代码积累(一)判断点击的是哪个精灵

本文介绍了一个用于Cocos2d-x游戏开发中精灵点击检测的方法。通过定义一个特殊的矩形区域来判断用户的触摸事件是否击中了某个精灵,并提供了一段具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[i][b]本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: http://www.firedragonpzy.com.cn/index.php/archives/90[/b][/i]


CCRect atlasRect(CCSprite* sprite)
{
CCRect rc = sprite->getTextureRect();
return CCRectMake(-rc.size.width/2, -rc.size.height/2, rc.size.width, rc.size.height);
}


CCSprite* Prop::selectSpriteForTouch(CCTouch* pTouch)
{
CCSprite* sprite = NULL;
CCMutableArray<CCSprite*>::CCMutableArrayIterator it;
for (it = allProps->begin(); it != allProps->end(); it++)
{
sprite = *it;
CCPoint point = sprite->convertTouchToNodeSpaceAR(pTouch);
if (CCRect::CCRectContainsPoint(atlasRect(sprite),point))
{
return sprite;
}
}

return NULL;
}

//获取并处理点击的精灵
void Prop::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
{
CCSprite* sprite = selectSpriteForTouch(pTouch);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值