各位大大,这个问题我实在无法自己解决了,求帮忙看看到底是什么问题
【萌新一个】
我在场景中的一层上使用了一个RenderTexture当做画板来画画,这个RenderTexture是全屏的。同时我想在这个场景中加载几个按钮。我用Cocos UI编辑器导出了.json文件,几个按钮作为单独一层也同时加载到这个场景中。
问题来了,无论我怎么设置两个层的Zorder都无法让RenderTexture响应触摸事件,而button一直都可以响应触摸。
我还尝试设置过事件穿透,以及设置EventListener的优先顺序(把RenderTexture设置为-130的优先度)都无果。难道RenderTexture就不能和Menu放在一个场景了吗?有什么好的解决办法?非常感谢!
下面是代码,使用的是Cocos2d-x 3.10版本:
这是RenderTexture层的init函数
bool SnowLayer::init()
{
if (!Layer::init())
{
return false;
}
this->setTouchEnabled(true);
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
auto listen = EventListenerTouchAllAtOnce::create();
listen->onTouchesBegan = CC_CALLBACK_2(SnowLayer::onTouchesBegan, this);
listen->onTouchesMoved = CC_CALLBACK_2(SnowLayer::onTouchesMoved, this);
eventDispatcher->addEventListenerWithSceneGraphPriority(listen,this);
//创建干净的画板
pRt = RenderTexture::create(visibleSize.width,visibleSize.height);//锚点在中间
CCAssert(pRt, "RenderTexture is invalid");
pRt->setPosition(visibleSize.width / 2, visibleSize.height / 2);
this->addChild(pRt);
//创建遮罩图片
Sprite *pMask = CCSprite::create("mask1.png");
CCAssert(pMask, "mask sprite is invalid");
pMask->getTexture()->setAliasTexParameters();
masksize = pMask->getContentSize();
pMask->setPosition(visibleSize.width / 2, visibleSize.height / 2);//锚点在中间
Point origin = pMask->getPosition();
//创建被遮罩图片
Sprite *pFlower = CCSprite::create("passive.png");
CCAssert(pFlower, "Flower sprite is invalid");
pFlower->setPosition(visibleSize.width / 2, visibleSize.height / 2);
//this->addChild(pFlower);
//先设置好 遮罩精灵 和 被遮罩精灵 在被渲染的时候采用什么样的颜色混合法则
BlendFunc maskBlend = { GL_ONE, GL_ZERO };
BlendFunc flowerBlend = { GL_DST_ALPHA, GL_ZERO };
pMask->setBlendFunc(maskBlend);
pFlower->setBlendFunc(flowerBlend);
// 创建一个橡皮擦
pEraser = Sprite::create("eraser.png");
pEraser->retain();
//开始把各种精灵渲染到画板上
pRt->begin();
pMask->visit();
pFlower->visit();
pRt->end();
return true;
}
这是UI层
void ChooseChapLayer::createOprUI()
{
auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("ChosseChapUi_1.ExportJson");
this->addChild(UI);
Button* Button_Decora = (Button*)Helper::seekWidgetByName(UI, "Button_1");
Button_Decora->addTouchEventListener(this, toucheventselector(ChooseChapLayer::JumptoDecora));
Button* Button_Snow = (Button*)Helper::seekWidgetByName(UI, "Button_2");
Button_Snow->addTouchEventListener(this, toucheventselector(ChooseChapLayer::JumptoSnow));
Button* Button_Tradition = (Button*)Helper::seekWidgetByName(UI, "Button_3");
Button_Tradition->addTouchEventListener(this, toucheventselector(ChooseChapLayer::JumptoTradition));
}
谢谢各位!这问题困扰我很久了。