单点触摸: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->on@R_301_4608@ed = [=](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);
});
以上是内存溢出为你收集整理的cocos2dx3.5 EventDispatcher全部内容,希望文章能够帮你解决cocos2dx3.5 EventDispatcher所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)