cykod / Quintus

HTML5 Game Engine
http://html5quintus.com
GNU General Public License v2.0
1.41k stars 401 forks source link

passing arguments to event handlers #79

Open wirthmi opened 10 years ago

wirthmi commented 10 years ago

Hello,

I've noticed that in Core Quintus Basics chapter of Guide accessible on Quintus website is written that up to 3 arguments can be passed to event handlers when triggering some event. It doesn't work. According to implementation of trigger() method in Evented class only one argument can be passed to handlers. IMHO possibility to pass up to 3 arguments would be nice and handy.

jerone commented 10 years ago

The whole documentation needs an update...

qsrahman commented 10 years ago
trigger: function(event) {
      // First make sure there are any listeners, then check for any listeners
      // on this specific event, if not, early out.
      if(this.listeners && this.listeners[event]) {
         var args = [].slice.call(arguments, 1);
        // Call each listener in the context of either the target passed into
        // `on` or the object itself.
        for(var i=0,len = this.listeners[event].length;i<len;i++) {
          var listener = this.listeners[event][i];
          listener[1].apply(listener[0], args);
        }
      }
    },

I think if the trigger method is changed to above code than any number of arguments can be passed.

cykod commented 10 years ago

In this implementation the strain on the GC is too much - trigger is called a lot (thousands of times a second in many games), so calling slice (which creates a new array with the result each time) will result in lots and lots of objects being created (even if [].slice was rewritten as Array.prototype.slice)

qsrahman commented 10 years ago

you are right :) thanks for the info.