title: Adding and Removing Event Listeners in Cocos 2D-X 3.2
body: <p>I'm trying to implement an easy way to enable and disable a Touch listener within my class. I tried writing a method within my class:</p>
  
  <pre><code>void HelloWorld::setTouchEnabled(bool enabled)
  {
      if (enabled)
      {
          auto _touchListener = EventListenerTouchAllAtOnce::create();
          _touchListener-&gt;onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
          _eventDispatcher-&gt;addEventListenerWithSceneGraphPriority(_touchListener, this);
  
      }
      else if (!enabled)
      {
          _eventDispatcher-&gt;removeEventListener(_touchListener);
      }
  
  }
  </code></pre>
  
  <p>I was hoping to be able to then call <code>setTouchEnabled(true)</code> or <code>setTouchEnabled(false)</code> from within any other methods in this class. However, this does not work since <code>_touchListener</code> is released at the end of the function. When I tried to declare <code>EventListener *_touchListener</code> in my header file, I received an error in XCode on this line:</p>
  
  <pre><code>_touchListener-&gt;onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
  </code></pre>
  
  <p>The error said that no member named <code>onTouchesBegan</code> exists in <code>cocos2d::EventListener</code>.</p>
  
  <p>I'm assuming there must be an easy way to do this.</p>