展会信息港展会大全

用C++在cocos2d-x 3.2下完美解决Menu吞掉事件导致ScrollView等无法响应的问题
来源:互联网   发布日期:2015-09-27 14:59:23   浏览:6544次  

导读: 最近下了最新版本的cocos2dx 3.2做项目,发现一个坑爹的问题。ScrollView 的子控件上有Menu的时候,ScrollView滑动无法响应。 百度了很多资料,要么说不清楚...

最近下了最新版本的cocos2dx 3.2做项目,发现一个坑爹的问题。ScrollView 的子控件上有Menu的时候,ScrollView滑动无法响应。

百度了很多资料,要么说不清楚,要么版本很旧的不适合。

于是自己跑去看了下源码。

发现Menu里面有一句

touchListener->setSwallowTouches(true);

将true修改为false后,完全木有问题。

所以花了几分钟自己写了个继承Menu的类,

修改下方法,然后要使用Menu的地方替换为自己的DBMenu就完美解决问题了。

下面是源码头h文件:

#pragmaonce

#include cocos2d.h

USING_NS_CC;

class DBMenu:public Menu

{

public:

bool init();

/** initializes a Menu with a NSArray of MenuItem objects */

bool initWithArray(const Vector& arrayOfItems);

static DBMenu* createWithArray(const Vector& arrayOfItems);

static DBMenu* createWithItem(MenuItem* item);

/** creates a Menu with MenuItem objects */

static DBMenu* createWithItems(MenuItem *firstItem, va_list args);

static DBMenu* create(MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;

private:

};

下面是源码cpp文件:#include DBMenu.h

bool DBMenu::init()

{

return initWithArray(Vector());

}

bool DBMenu::initWithArray( const Vector& arrayOfItems )

{

if (Layer::init())

{

_enabled = true;

// menu in the center of the screen

Size s = Director::getInstance()->getWinSize();

this->ignoreAnchorPointForPosition(true);

setAnchorPoint(Vec2(0.5f, 0.5f));

this->setContentSize(s);

setPosition(Vec2(s.width/2, s.height/2));

int z=0;

for (auto& item : arrayOfItems)

{

this->addChild(item, z);

z++;

}

_selectedItem = nullptr;

_state = Menu::State::WAITING;

// enable cascade color and opacity on menus

setCascadeColorEnabled(true);

setCascadeOpacityEnabled(true);

auto touchListener = EventListenerTouchOneByOne::create();

touchListener->setSwallowTouches(false);

touchListener->onTouchBegan = CC_CALLBACK_2(DBMenu::onTouchBegan, this);

touchListener->onTouchMoved = CC_CALLBACK_2(DBMenu::onTouchMoved, this);

touchListener->onTouchEnded = CC_CALLBACK_2(DBMenu::onTouchEnded, this);

touchListener->onTouchCancelled = CC_CALLBACK_2(DBMenu::onTouchCancelled, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

return true;

}

return false;

}

DBMenu* DBMenu::createWithArray( const Vector& arrayOfItems )

{

auto ret = new DBMenu();

if (ret && ret->initWithArray(arrayOfItems))

{

ret->autorelease();

}

else

{

CC_SAFE_DELETE(ret);

}

return ret;

}

DBMenu* DBMenu::createWithItem( MenuItem* item )

{

return DBMenu::create(item, nullptr);

}

DBMenu* DBMenu::createWithItems( MenuItem *item, va_list args )

{

Vector items;

if( item )

{

items.pushBack(item);

MenuItem *i = va_arg(args, MenuItem*);

while(i)

{

items.pushBack(i);

i = va_arg(args, MenuItem*);

}

}

return DBMenu::createWithArray(items);

}

DBMenu* DBMenu::create( MenuItem* item, ... ) CC_REQUIRES_NULL_TERMINATION

{

va_list args;

va_start(args,item);

DBMenu *ret = DBMenu::createWithItems(item, args);

va_end(args);

return ret;

}

*>*>*>*>

本文原创,如转载请注明原文地址。

http://blog.csdn.net/q229827701/article/details/38901213

*>*>

赞助本站

人工智能实验室
相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港