//注册触摸EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);listener->setSwallowTouches(true);_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//创建一个遮罩LayerColor *m_pLyaerColor = LayerColor::create(ccc4(0, 0, 0, 110));//创建剪裁节点ClippingNode *pClip = ClippingNode::create();pClip->setInverted(true);//是否反向,true 圆是透明,其他是黑, false 圆是黑,其他透明 addChild(pClip);//将遮罩添加到裁剪节点上pClip->addChild(m_pLyaerColor);//绘制圆区域//设置参数ccColor4F red = ccc4f(1, 0, 0, 1);float radius = 55.0f;//圆的半径const int pCount = 200;//顶点数,将圆看成是200边型float angel = 2.0f * (float)M_PI / pCount;//两个顶点与中心的夹角(弧度) Point m_vPoint[pCount]; for (int i = 0; i < pCount; i++){float radian = i * angel;//弧度m_vPoint[i].x = radius * cosf(radian);//顶点X坐标m_vPoint[i].y = radius * sinf(radian);//顶点Y坐标 }//绘制多边形//注意不要将pStencil addChildDrawNode *pStencil = DrawNode::create();pStencil->drawPolygon(m_vPoint, pCount, red, 0, red);//绘制这个多边型pStencil->setPosition(Vec2(visibleSize.width /2 , visibleSize.height /2));//将这个圆形从裁剪节点上面抠出来, Stencil是模版的意思pClip->setStencil(pStencil);
bool HelloWorld::onTouchBegan(Touch *touch, Event *event){Size size = Director::getInstance()->getWinSize();Vec2 location = touch->getLocation();//获取点击的坐标float b = abs(location.x - size.width / 2);//获取绝对值float c = abs(location.y - size.height / 2);//坐标x.y - 圆心x,y size/2是圆心.if (b <= 55.0f && c <= 55.0f){log("dianjidaole");//小于55 点击到了圆return true; }log("meiyou");return false; }