cocos2dx3.5 EventDispatcher

前端之家收集整理的这篇文章主要介绍了cocos2dx3.5 EventDispatcher前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

单点触摸:EventListenerTouchOneByOne

// Make sprite1 touchable

auto containerForSprite1 = Node::create();
auto sprite1 = Sprite::create("Images/CyanSquare.png");
sprite1->setPosition(origin+Vec2(size.width/2,size.height/2) + Vec2(-80,80));
containerForSprite1->addChild(sprite1);
addChild(containerForSprite1,10);

auto sprite2 = Sprite::create("Images/MagentaSquare.png");
sprite2->setPosition(origin+Vec2(size.width/2,size.height/2));
addChild(sprite2,20);

auto sprite3 = Sprite::create("Images/YellowSquare.png");
sprite3->setPosition(Vec2(0,0));
sprite2->addChild(sprite3,1);

创建监听器
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);

listener1->onTouchBegan = [](Touch* touch,Event* event){ C++11最新的功能std::function
auto target = static_cast<Sprite*>(event->getCurrentTarget());获得订阅者本身

Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0,s.width,s.height);

if (rect.containsPoint(locationInNode))
{
log("sprite began... x = %f,y = %f",locationInNode.x,locationInNode.y);
target->setOpacity(180);
return true;
}
return false;
};

listener1->onTouchMoved = [](Touch* touch,Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());//getLocation() - getPrevIoUsLocation()
};

listener1->onTouchEnded = [=](Touch* touch,Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
target->setOpacity(255);
if (target == sprite2)
{
containerForSprite1->setLocalZOrder(100);
}
else if(target == sprite1)
{
containerForSprite1->setLocalZOrder(0);
}
};

auto_eventDispatcher=Director::getInstence()->getDispatcher();
注册监听器:
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener1,sprite1);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener1->clone(),sprite2);克隆监听器
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener1->clone(),sprite3);

清除监听:

auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners",[this](Ref* sender){
auto senderItem = static_cast<MenuItemFont*>(sender);
senderItem->setString("Only Next item could be clicked");

_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);

自定义事件:

auto listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){
std::string str("Custom event 1 received,");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
str += " times";
statusLabel->setString(str.c_str());
});

_eventDispatcher->addEventListenerWithFixedPriority(_listener,1);

auto sendItem = MenuItemFont::create("Send Custom Event 1",[=](Ref* sender){
static int count = 0;
++count;
char* buf = new char[10];
sprintf(buf,"%d",count);
EventCustom event("game_custom_event1");
event.setUserData(buf);
_eventDispatcher->dispatchEvent(&event);
CC_SAFE_DELETE_ARRAY(buf);
});

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