Javascript and Famo.us: Engine.removeListener() not working

82 Views Asked by At

Quick question, I've got a simple function that creates some surfaces and animates them through defining a function 'animate' to be called every Engine prerender. This behaves as it should, however, removing this listener on prerender with Engine.removeListener does not work.

function _createCube(){
   //create of some surfaces and modifiers
   Engine.on('prerender',animate);


  surface.on('click',function(){
     _stopAnimation.call(this);
  }
}

function _stopAnimation(){
  Engine.removeListener('prerender',animate); 
}
1

There are 1 best solutions below

0
On

It does work, but you may have an error in your code. The following example code shows a simple working example of a using the removeListener

Working jsBin Example Here Click on the counter to start and stop listener.

  var mainContext = Engine.createContext();

  var surface = new Surface({ 
    content: 'Famo.us Count ',
    properties:{
      cursor: 'pointer'
    }
  });

  mainContext.add(surface);
  var counter = 0;

  function animate() {
    counter+=1;
    surface.setContent('Famo.us Count ' + counter);
  }

  function _create(){


    surface.on('click',function(){
      if (!surface.started) {
        Engine.on('prerender',animate);
        surface.started = true;
      } else {
        _stopAnimation.call(this);
        surface.started = false;
      }
    });
  }

  function _stopAnimation(){
    Engine.removeListener('prerender',animate); 
  }

  _create.call(this);