0

Looking for help on how to setup "EventListenerCustom" class instance so that I can broadcast to all objects a unique trigger has occurred. In older versions of Cocos2D-X, there was CCNotification, where I could set a flag and all my objects could check to see what the flag value was and based on whichever flag that an object reacted too would take an action.

I have tried looking for example where I could put all the movement, rotation, firing logic in one class and my other objects would take its specific action based on the flag that one class could control and change.

badboy11
  • 541
  • 1
  • 6
  • 19

1 Answers1

0

I would do this by creating a vector that keeps lambda functions.

class EventListenerCustom : public cocos2d::Layer
{
public:
    virtual bool init();
    CREATE_FUNC(EventListenerCustom);

    void startFire();
    void addFiringEventListener(std::function<void ()> listener);
protected:
    std::vector<std::function<void()>> _firingEvents;
};



void EventListenerCustom::addFiringEventListener(std::function<void ()> listener)
{
    _firingEvents.push_back(listener);
}

void EventListenerCustom::startFire()
{
    for( const auto &child : _firingEvents)
        child();
}

Or you can create a CustomEvent class for holding the lambda function and the target node in order to check if the target node is not null.

Baris Atamer
  • 700
  • 7
  • 13