play-co / timestep

GNU General Public License v3.0
16 stars 27 forks source link

SpriteView callback receives instance as argument #3

Closed duncanbeevers closed 11 years ago

duncanbeevers commented 11 years ago

This is useful when SpriteViews are used in a ViewPool, allowing a single closure to service all View instances, reducing allocations and GC churn.

duncanbeevers commented 11 years ago

See this simple example of using a single closure to clean up after all SpriteViews obtained from a ViewPool.

var viewPool = new ui.ViewPool({
  ctor: ui.SpriteView,
  initCount: 100,
  initOpts: {
    url: "resources/images/animations/explosions",
    frameRate: 30,
    autoStart: false,
    loop: false,
    callback: function(view) {
      view.removeFromSuperview();
      viewPool.releaseView(view);
    }
  }
});

contactListener.registerImpactListener("bullet001", "Asteroid", function(bullet, asteroid, strength, location) {
  viewPool.obtainView({
    x: location.x,
    y: location.y,
    superview: playfield
  }).startAnimation("explode");
});
duncanbeevers commented 11 years ago

It looks like since the callback property of a SpriteView is obliterated with every call to that instance's startAnimation method, preserving the closure is moot.