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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 05:34   267   0

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

同样先给个效果图

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

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

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

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

  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. CCRect rect = pSprite1->boundingBox(); //得到精灵的矩形框
  6. int x = rect.origin.x; //得到矩形框的左下角x坐标
  7. int y = rect.origin.y; //得到矩形框的左下角x坐标
  8. int w = rect.size.width; //得到矩形框的宽
  9. int h = rect.size.height; //得到矩形框的高
  10. rect = CCRect(210, 130, 20, 20); //重新设置精灵的矩形框x坐标为210,Y坐标为130,宽w为20,高h为20
  11. CCLog("%d==%d==%d==%d",x,y,w,h);
  12. if(CCRect::CCRectContainsPoint(rect, beginLoc)){
  13. plabel1->setVisible(true);
  14. }
  15. }

这样就可以设置我们想要的触摸区域了 不会再为精灵重叠由那个精灵响应触摸事件而烦恼了。
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP