Cocos2d-x游戏开发之设置精灵的触摸范围

在Cocos2d-x游戏开发中,为了精细化处理触摸事件,文章介绍了如何设置精灵的特定区域响应触摸。通过调整精灵的boundingBox,实现只让精灵的一部分接受触摸事件,解决了精灵重叠时触摸判断的问题。示例代码展示了如何在触摸开始时检查触摸点是否在预设的矩形区域内,从而控制精灵的行为。

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

触摸事件是游戏中必不可少的一部分,但是有时候我们不想使整个精灵都可以接受触摸响应的,所以我们要设精灵的一部分响应。

同样先给个效果图

  1.   

这样当我们点击两个精灵重叠的部分时就不好判断由那个精灵接收触摸事件因此我们要设置精灵的一部分接受触摸事件

同样我们先初始化两个精灵

  1. bool HelloWorld::init()  
  2. {  
  3.     //////////////////////////////  
  4.     // 1. super init first  
  5.     if ( !CCLayer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.   
  10.     /////////////////////////////  
  11.     // 2. add a menu item with "X" image, which is clicked to quit the program  
  12.     //    you may modify it.  
  13.   
  14.     // add a "close" icon to exit the progress. it's an autorelease object  
  15.     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  16.                                         "CloseNormal.png",  
  17.                                         "CloseSelected.png",  
  18.                                         this,  
  19.                                         menu_selector(HelloWorld::menuCloseCallback) );  
  20.     pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );  
  21.   
  22.     // create menu, it's an autorelease object  
  23.     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  24.     pMenu->setPosition( CCPointZero );  
  25.     this->addChild(pMenu, 1);  
  26.   
  27.     /////////////////////////////  
  28.     // 3. add your codes below...  
  29.   
  30.     // add a label shows "Hello World"  
  31.     // create and initialize a label  
  32.     CCLabelTTF* pLabel = CCLabelTTF::create("设置精灵的触摸范围""Thonburi", 34);  
  33.     plabel1 = CCLabelTTF::create("你触摸了精灵重叠区域","Thonburi",34);  
  34.     // ask director the window size  
  35.     size = CCDirector::sharedDirector()->getWinSize();  
  36.   
  37.     // position the label on the center of the screen  
  38.     pLabel->setPosition( ccp(size.width / 2, size.height - 20) );  
  39.   
  40.     // add the label as a child to this layer  
  41.     this->addChild(pLabel, 1);  
  42.   
  43.     // add "HelloWorld" splash screen"  
  44.     pSprite = CCSprite::create("Icon-72.png");  
  45.     pSprite1 = CCSprite::create("Icon-72.png");  
  46.   
  47.     // position the sprite on the center of the screen  
  48.     pSprite->setPosition(ccp(size.width/2, size.height/2) );  
  49.     pSprite1->setPosition(ccp(size.width/2-50, size.height/2-50) );  
  50.     plabel1->setPosition(ccp(size.width/2, size.height/2-120));  
  51.     plabel1->setVisible(false);  
  52.     // add the sprite as a child to this layer  
  53.     this->addChild(pSprite, 0);  
  54.     this->addChild(pSprite1,1);  
  55.     this->addChild(plabel1);  
  56.     this->setTouchEnabled(true);  
  57.     return true;  
  58. }  

接下来我们给精灵的一小部分设置为可触摸

  1. void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){  
  2.     CCTouch *touch = (CCTouch *)pTouches->anyObject();  
  3.     CCPoint beginLoc = touch->locationInView();  
  4.     beginLoc = CCDirector::sharedDirector()->convertToGL(beginLoc);  
  5.       
  6.     CCRect rect = pSprite1->boundingBox();          //得到精灵的矩形框  
  7.     int x = rect.origin.x;                          //得到矩形框的左下角x坐标  
  8.     int y = rect.origin.y;                          //得到矩形框的左下角x坐标  
  9.     int w = rect.size.width;                        //得到矩形框的宽  
  10.     int h = rect.size.height;                       //得到矩形框的高  
  11.       
  12.     rect = CCRect(210, 130, 20, 20);                //重新设置精灵的矩形框x坐标为210,Y坐标为130,宽w为20,高h为20      
  13.    CCLog("%d==%d==%d==%d",x,y,w,h);  
  14.     if(CCRect::CCRectContainsPoint(rect, beginLoc)){  
  15.        plabel1->setVisible(true);  
  16.     }  
  17. }  

这样就可以设置我们想要的触摸区域了 不会再为精灵重叠由那个精灵响应触摸事件而烦恼了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值