加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:33:38 所属栏目:百科 来源:网络整理
导读:网上打部分不规则点击教程都是针对cocos2d-2.x的,因为那时draw()函数里会立即绘制。3.x不一样了,一边如此我们也可以让他以2.x的工作方式来绘制。直接贴代码了 void HelloWorld::testPixel() { auto s = Director::getInstance()-getWinSize(); auto sp =

网上打部分不规则点击教程都是针对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); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读