cocos2d-x 3.x 不规则点击区域

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.x 不规则点击区域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

网上打部分不规则点击教程都是针对cocos2d-2.x的,因为那时draw()函数里会立即绘制。3.x不一样了,一边如此我们也可以让他以2.x的工作方式来绘制。直接贴代码



void HelloWorld::testPixel() { auto s = Director::getInstance()->getWinSize(); auto sp = Sprite::create("HelloWorld.png"); sp->setPosition(s / 2); addChild(sp); Size size = sp->getContentSize(); auto m_rt = RenderTexture::create(size.width,size.height,Texture2D::PixelFormat::RGBA8888); // 绘画开始 m_rt->begin(); // 绘制 sp->visit(); // 结束绘制 m_rt->end(); Director::getInstance()->getRenderer()->render(); //加这句 // 通过画布拿到这张画布上每个像素点的信息,封装到CCImage中 pImage = m_rt->newImage(); auto listen = EventListenerTouchOneByOne::create(); listen->setSwallowTouches(false); listen->onTouchBegan = [&](Touch* touch,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget()); if(target->getBoundingBox().containsPoint(touch->getLocation())) { //获取像素数据 Color4B color4B; Vec2 nodePos = target->convertTouchToNodeSpace(touch); unsigned int x = nodePos.x; unsigned int y = target->getContentSize().height - nodePos.y; unsigned char* data_ = pImage->getData(); unsigned int *pixel = (unsigned int *)data_; pixel = pixel + (y * (int)target->getContentSize().width) * 1 + x * 1; //R通道 color4B.r = *pixel & 0xff; //G通道 color4B.g = (*pixel >> 8) & 0xff; //B通过 color4B.b = (*pixel >> 16) & 0xff; //Alpha通道,我们有用的就是Alpha color4B.a = (*pixel >> 24) & 0xff; if(color4B.a == 0) { log("tou ming.............."); } else { log("bu tou ming.............."); } } return true; }; _eventDispatcher->addEventListenerWithSceneGraPHPriority(listen,sp); }

原文链接:https://www.f2er.com/cocos2dx/341338.html

猜你在找的Cocos2d-x相关文章