本文整理汇总了C++中EventDispatcher::dispatchEvent方法的典型用法代码示例。如果您正苦于以下问题:C++ EventDispatcher::dispatchEvent方法的具体用法?C++ EventDispatcher::dispatchEvent怎么用?C++ EventDispatcher::dispatchEvent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EventDispatcher
的用法示例。
在下文中一共展示了EventDispatcher::dispatchEvent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dispatchEvent
int EventDispatcherBinder::dispatchEvent(lua_State* L)
{
StackChecker checker(L, "EventDispatcherBinder::dispatchEvent", 0);
Binder binder(L);
EventDispatcher* eventDispatcher = static_cast<EventDispatcher*>(binder.getInstance("EventDispatcher", 1));
luaL_checktype(L, 2, LUA_TTABLE);
// lua_getfield(L, 2, "type");
lua_getfield(L, 2, "getType");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
std::string event = luaL_checkstring(L, -1);
lua_pop(L, 1);
LuaEvent e = LuaEvent(LuaEvent::Type(event.c_str()));
LuaApplication *application = (LuaApplication*)luaL_getdata(L);
lua_State *mainL = application->getLuaState();
lua_pushvalue(L, 2); // push event to main thread
if (mainL != L)
lua_xmove(L, mainL, 1);
eventDispatcher->dispatchEvent(&e);
lua_pop(mainL, 1); // pop event from main thread
return 0;
}
示例2: main
int main()
{
EventDispatcher dispatcher;
int my_var = 0;
dispatcher.addHandler("event1", [&my_var](EventDispatcher& ev) -> bool {
cout << "My event1 has been launched (first handler)" << endl;
my_var++;
return true;
});
dispatcher.addHandler("event1", [&my_var](EventDispatcher& ev) -> bool {
cout << "My event1 has been launched (second handler)" << endl;
my_var++;
cout << "Trigger event2..." << endl;
ev.dispatchEvent("event2");
return false;
});
dispatcher.addHandler("event2", [&my_var](EventDispatcher& ev) -> bool {
cout << "My event2 has been launched!" << endl;
my_var++;
return true;
});
cout << "Triggering event..." << endl;
dispatcher.dispatchEvent("event1");
cout << "My var is: " << my_var << endl;
return 0;
}
示例3: fullLevelUp
void ActorHelper::fullLevelUp(unsigned int unitID)
{
int product_id = 0;
s_levelUpUnitID = unitID;
if (unitID >= ID_Actor_Start && unitID <= ID_Actor_End) {
//SdkManager::getInstance()->pay(ProductID_RoleFullLevel);
product_id = ProductID_RoleFullLevel;
}
else if (unitID >= ID_Magic_Start && unitID <= ID_Magic_End) {
//SdkManager::getInstance()->pay(ProductID_MagicFullLevel);
product_id = ProductID_MagicFullLevel;
}
else {
CCASSERT(false, "error!");
}
//test win32
#if DEBUG_ALWAYS_BUY_SUCCESS == 1
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_PayResult);
MsgData data;
data.value = product_id;
data.errorCode = Rsp_Success;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
#endif
}
示例4: unlockHeroByFragment
void ActorHelper::unlockHeroByFragment(unsigned int unitID)//解锁角色
{
UnitID needFragId = this->getActorUnlockNeedFragmentId(unitID);
const BagItem *fragItem = BagDataManager::getManager()->getOneUnit(needFragId);
const BagItem *goldItem = BagDataManager::getManager()->getOneUnit(unitID_Gold);
int needFrag = this->getUnlockHeroNeedFragmentAndGold(unitID).first;
int needGold = this->getUnlockHeroNeedFragmentAndGold(unitID).second;
if (fragItem==nullptr || fragItem->value < needFrag)
{
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_MarketBuyGoods_Rsp);
MsgData data;
//data.errorCode = Rsp_Need_UnEnough;
if(needFragId == unitID_Actor2Fragment ) {
data.errorCode = Rsp_Suipian_UnEnough;
} else {
data.errorCode = Rsp_Need_UnEnough;
}
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
return;
}
else if (goldItem==nullptr || goldItem->value < needGold)
{
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_MarketBuyGoods_Rsp);
MsgData data;
data.errorCode = Rsp_Gold_UnEnough;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
return;
}
BagDataManager::getManager()->changeOneUnitCount(fragItem->id, -needFrag);
BagDataManager::getManager()->changeOneUnitCount(goldItem->id, -needGold);
BagDataManager::getManager()->changeOneUnitCount(unitID, 1);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_MarketBuyGoods_Rsp);
MsgData data;
data.errorCode = Rsp_Success;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
示例5: testCallbacks
static void testCallbacks() {
EventDispatcher * dispatcher;
char * sender1 = "1", * sender2 = "2";
int callback1Context = 0, callback2Context = 0;
int incrementation = 2;
bool handled;
Atom event1Atom, event2Atom;
dispatcher = EventDispatcher_create(sender1);
event1Atom = Atom_fromString("event1");
event2Atom = Atom_fromString("event2");
handled = dispatcher->dispatchEvent(dispatcher, event1Atom, &incrementation);
TestCase_assert(!handled, "Event handled when no callbacks are registered");
dispatcher->registerForEvent(dispatcher, event1Atom, callback1, &callback1Context);
handled = dispatcher->dispatchEvent(dispatcher, event1Atom, &incrementation);
TestCase_assert(handled, "Event not handled when appropriate callback was registered");
TestCase_assert(callback1Context == 2, "Expected 2 but got %d", callback1Context);
TestCase_assert(lastSender == sender1, "Expected %p but got %p", sender1, lastSender);
incrementation = 3;
dispatcher->dispatchEvent(dispatcher, event2Atom, &incrementation);
TestCase_assert(callback1Context == 2, "Callback called for wrong type of event");
dispatcher->registerForEvent(dispatcher, event2Atom, callback2, &callback2Context);
handled = dispatcher->dispatchEvent(dispatcher, event2Atom, &incrementation);
TestCase_assert(callback2Context == 3, "Expected 3 but got %d", callback2Context);
TestCase_assert(!handled, "Expected false but got true");
dispatcher->unregisterForEvent(dispatcher, event1Atom, callback1, NULL);
handled = dispatcher->dispatchEvent(dispatcher, event1Atom, &incrementation);
TestCase_assert(handled, "Event not still handled after unregistering callbacks with the wrong context");
dispatcher->unregisterForEvent(dispatcher, event1Atom, callback1, &callback1Context);
handled = dispatcher->dispatchEvent(dispatcher, event1Atom, &incrementation);
TestCase_assert(!handled, "Event still handled after unregistering callbacks");
incrementation = 1;
handled = dispatcher->dispatchEvent(dispatcher, event2Atom, &incrementation);
TestCase_assert(callback2Context == 4, "Expected 4 but got %d", callback2Context);
TestCase_assert(!handled, "Expected false but got true");
dispatcher->dispose(dispatcher);
dispatcher = EventDispatcher_create(sender2);
dispatcher->registerForEvent(dispatcher, event1Atom, callback1, &callback1Context);
dispatcher->dispatchEvent(dispatcher, event1Atom, &incrementation);
TestCase_assert(lastSender == sender2, "Expected %p but got %p", sender2, lastSender);
dispatcher->dispose(dispatcher);
}
示例6: main
int main(int argc, char** argv) {
Logger::getLogger().setLogLevel(LogLevel::Debug);
bool client1EventCalled = false;
bool client2EventCalled = false;
EventDispatcher eventDispatcher;
eventDispatcher.registerEventHandler(typeid(RealEvent).hash_code(), [&](Event* e) {
RealEvent* event = static_cast<RealEvent*>(e);
switch(event->number) {
case 1:
std::cout << "client1's event called" << std::endl;
client1EventCalled = true;
break;
case 2:
std::cout << "client2's event called" << std::endl;
client2EventCalled = true;
break;
}
});
std::thread client1 = std::thread([&](){
eventDispatcher.dispatchEvent(new RealEvent(1));
});
client1.join();
std::thread client2 = std::thread([&](){
eventDispatcher.dispatchEvent(new RealEvent(2));
});
client2.join();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "after sleep" << std::endl;
assert(client1EventCalled);
assert(client2EventCalled);
return 0;
}
示例7: changeOneAchievement
void BagDataManager::changeOneAchievement(unsigned int achieveID, int data,long time)
{
CCASSERT(data > 0, "couldn't sub achieve");
for (int i=0,count=m_BagItemVec.size(); i<count; ++i)
{
if (m_BagItemVec[i].id == achieveID){
m_BagItemVec[i].value += data;
m_BagItemVec[i].value_2 = time;
//__NotificationCenter::getInstance()->postNotification(MSG_BagDataChanged, nullptr);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_BagDataChanged);
event.setUserData(&m_BagItemVec[i]);
eventDspc->dispatchEvent(&event);
saveData();
return;
}
}
if (data > 0){
BagItem temp;
temp.id = achieveID;
temp.value = data;
temp.value_2 = time;
m_BagItemVec.push_back(temp);
//__NotificationCenter::getInstance()->postNotification(MSG_BagDataChanged, nullptr);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_BagDataChanged);
event.setUserData(&temp);
eventDspc->dispatchEvent(&event);
saveData();
return;
}
if (1 == DEBUG_uMessageBox)
{
cocos2d::MessageBox(cocos2d::StringUtils::format("BagItem Id=[%d] is null, can not sub", achieveID).c_str(), "ERROR");
CCASSERT(false, "");
}
}
示例8: spcall
void Rolelayer::spcall(Node* node,bool mr){
Layer* p=(Layer*)this->getChildByTag(shop_layertag);
p->setPosition(Vec2(Actual_x,0));
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(Topmsg_ch);
MsgData data;
data.value =2;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
示例9: if
ActorHelper::ActorHelper()
{
EventListenerCustom* listener = EventListenerCustom::create(MSG_PayResult, [&](EventCustom* evt)
{
bool bIsLvMsg = false;
MsgData* payData=(MsgData*)evt->getUserData();
int gameProductID = payData->value;
if (gameProductID == ProductID_RoleLevelUp || gameProductID == ProductID_MagicLevelUp)
{
bIsLvMsg = true;
if (payData->errorCode == Rsp_Success)
{
BagDataManager::getManager()->changeOneUnitLevel(s_levelUpUnitID,1,0);
}
}
else if (gameProductID == ProductID_RoleFullLevel || gameProductID == ProductID_MagicFullLevel)
{
bIsLvMsg = true;
if (payData->errorCode == Rsp_Success)
{
int maxLv = ActorHelper::getHelper()->getMaxLevel(s_levelUpUnitID);
int curLv = BagDataManager::getManager()->getOneUnit(s_levelUpUnitID)->value;
BagDataManager::getManager()->changeOneUnitLevel(s_levelUpUnitID,maxLv-curLv,0);
}
}
//
if (bIsLvMsg)
{
log("ActorHelper ---> on MSG_PayResult");
int errCode = payData->errorCode;
Director::getInstance()->getRunningScene()->runAction(Sequence::create(
DelayTime::create(1),
CallFunc::create([=]() {
log("lambda ---> begin dispatch MSG_LevelUp_Rsp");
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_LevelUp_Rsp);
MsgData data;
data.errorCode = errCode;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
log("lambda ---> end dispatch MSG_LevelUp_Rsp");
}),
nullptr));
}
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener,-1); //优先级应该比UI层高
}
示例10: _tmain
int _tmain(int argc, _TCHAR* argv[])
{
EventDispatcher ed;
ed.AttachEvent("open", &OpenHandler);
ed.AttachEvent("open", &OpenHandler2);
ed.DetachEvent("open", &OpenHandler2);
ed.AttachEvent("close", &OpenHandler2);
Event evt("open");
ed.dispatchEvent(evt);
system("pause");
return 0;
}
示例11: layerChange
void Rolelayer::layerChange(){
Layer* p=(Layer*)this->getChildByTag(layer_left_tag);
auto move = MoveTo::create(0.25,Vec2(-Actual_x,0));
auto move_ease_inout0 = EaseExponentialOut::create(move);
p->runAction(
Sequence::create(move_ease_inout0,CallFuncN::create( CC_CALLBACK_0(Rolelayer::setcall,this,nullptr)),nullptr)
);
for(auto n:menus){
auto move = MoveTo::create(0.5f, Vec2(n->getPositionX()-250,n->getPositionY()));
auto move_ease_inout0 = EaseExponentialOut::create(move);
n->runAction(move_ease_inout0);
}
for(auto m:jianls){
auto move = MoveTo::create(0.5f, Vec2(m->getPositionX()-250,m->getPositionY()));
auto move_ease_inout0 = EaseExponentialOut::create(move);
m->runAction(move_ease_inout0);
}
//toplayer 成就 按钮消失
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(Topmsg_ch);
MsgData data;
data.value =1;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
//主页商店背景效果
{
EventCustom event_m(Layerchange);
LayerData data_m;
data_m.current=101;
data_m.push=-1;
event_m.setUserData(&data_m);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event_m);
}
}
示例12: event
MarketHelper::MarketHelper()
{
EventListenerCustom* listener = EventListenerCustom::create(MSG_PayResult, [&](EventCustom* evt)
{
MsgData* payData=(MsgData*)evt->getUserData();
int gameProductID = payData->value;
if (gameProductID == ProductID_20Diamond
|| gameProductID == ProductID_70Diamond
|| gameProductID == ProductID_140Diamond
|| gameProductID == ProductID_5000Gold)
{
if (payData->errorCode == Rsp_Success)
{
const SaleItem* saleItem = MarketConfigManager::getManager()->getOneGoods(s_buy_SaleItemId);
if (saleItem){
BagDataManager::getManager()->changeOneUnitCount(saleItem->unitSaleID,saleItem->saleCount);
}
}
//
int errCode = payData->errorCode;
Director::getInstance()->getRunningScene()->runAction(Sequence::create(
DelayTime::create(1),
CallFunc::create([=](){
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_MarketBuyGoods_Rsp);
MsgData data;
data.errorCode = errCode;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}),
nullptr));
}
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener,-1); //优先级应该比UI层高
}
示例13: changeOneUnitLevel
void BagDataManager::changeOneUnitLevel(unsigned int id, int addLv,int value)
{
//CCASSERT(addLv > 0, "couldn't sub level");
{
map<string,string> info;
time_t curTime = time(0);
struct tm *curTm = localtime(&curTime);//时区时间, 非时区时间使用struct tm *gmtime(const time_t *timep);
char * timeStr = asctime(curTm);
info.insert(std::make_pair("LevelUpTime", timeStr));
info.insert(std::make_pair("LevelUpId", String::createWithFormat("%u", id)->getCString()));
info.insert(std::make_pair("LevelUpNum", String::createWithFormat("%d", addLv)->getCString()));
//SdkManager::getInstance()->logEvent(analyticsEVENT_Role, &info);
}
for (int i=0,count=m_BagItemVec.size(); i<count; ++i)
{
if (m_BagItemVec[i].id == id){
m_BagItemVec[i].value += addLv;
m_BagItemVec[i].value_2 += value;
//__NotificationCenter::getInstance()->postNotification(MSG_BagDataChanged, nullptr);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_BagDataChanged);
event.setUserData(&m_BagItemVec[i]);
eventDspc->dispatchEvent(&event);
saveData();
return;
}
}
if (1 == DEBUG_uMessageBox)
{
cocos2d::MessageBox(cocos2d::StringUtils::format("BagItem Id=[%d] is null, can not sub", id).c_str(), "ERROR");
CCASSERT(false, "");
}
}
示例14: changeOneUnitCount
void BagDataManager::changeOneUnitCount(unsigned int id, int num)
{
for (int i=0,count=m_BagItemVec.size(); i<count; ++i)
{
if (m_BagItemVec[i].id == id){
m_BagItemVec[i].value += num;
//__NotificationCenter::getInstance()->postNotification(MSG_BagDataChanged, nullptr);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_BagDataChanged);
event.setUserData(&m_BagItemVec[i]);
eventDspc->dispatchEvent(&event);
saveData();
if (id == unitID_Gold)
{
Achieve* info = nullptr;
//检测小富豪成就
if (GoodsHelper::getHelper()->getGoldNum() >= 20000)
{
info = (Achieve*)AchievementHelper::getHelper()->getAchieveByID(achieveID_XiaoFuHao);
if (!info->isOver)
{
AchievementHelper::getHelper()->completeOneAchievement(achieveID_XiaoFuHao, 1);
}
}
//检测大土豪成就
if (GoodsHelper::getHelper()->getGoldNum() >= 50000)
{
info = (Achieve*)AchievementHelper::getHelper()->getAchieveByID(achieveID_DaTuHao);
if (!info->isOver)
{
AchievementHelper::getHelper()->completeOneAchievement(achieveID_DaTuHao, 1);
}
}
}
return;
}
}
if (num > 0){
BagItem temp;
temp.id = id;
temp.value = num;
m_BagItemVec.push_back(temp);
//__NotificationCenter::getInstance()->postNotification(MSG_BagDataChanged, nullptr);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_BagDataChanged);
event.setUserData(&temp);
eventDspc->dispatchEvent(&event);
saveData();
return;
}
else if (1 == DEBUG_uMessageBox){
cocos2d::MessageBox(cocos2d::StringUtils::format("BagItem Id=[%d] is null, can not sub", id).c_str(), "ERROR");
CCASSERT(false, "");
}
}
示例15: feeding
void ActorHelper::feeding(unsigned int heroOrPet_unitID,unsigned int count /* = 1 */)
{
const BagItem *item = BagDataManager::getManager()->getOneUnit(heroOrPet_unitID);
if (item)
{
int max_level = LevelDataConfigManager::getManager()->getMaxLevel(heroOrPet_unitID);
if (max_level > item->value)
{
const OneLevelInfo *info = LevelDataConfigManager::getManager()->getOneUnitLevelInfo(heroOrPet_unitID,item->value + 1);
const BagItem *bag = BagDataManager::getManager()->getOneUnit(info->needUnit->ID);
if (bag)
{
if ((info->needCount - item->value_2) > count)
{
if (bag->value >= count)
{
int price = count;
BagDataManager::getManager()->changeOneUnitCount(info->needUnit->ID,-price);
BagDataManager::getManager()->changeOneUnitLevel(heroOrPet_unitID,0,count);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_Feed_Rsp);
MsgData data;
data.errorCode = Rsp_Success;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
else
{
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_Feed_Rsp);
MsgData data;
data.errorCode = Rsp_DontHaveThing;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
}
else
{
if (bag->value >= count)
{
int price = count;
BagDataManager::getManager()->changeOneUnitCount(info->needUnit->ID,-price);
BagDataManager::getManager()->changeOneUnitLevel(heroOrPet_unitID,1,bag->value_2 + count - info->needCount);
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_Feed_Rsp);
MsgData data;
data.errorCode = Rsp_Success;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
}
}
else
{
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_Feed_Rsp);
MsgData data;
data.errorCode = Rsp_DontHaveThing;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
}
}
else
{
EventDispatcher * eventDspc = Director::getInstance()->getEventDispatcher();
EventCustom event(MSG_Feed_Rsp);
MsgData data;
data.errorCode = Rsp_DontHaveThing;
event.setUserData(&data);
eventDspc->dispatchEvent(&event);
}
}