Closed duncanbeevers closed 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");
});
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.
This is useful when SpriteViews are used in a ViewPool, allowing a single closure to service all View instances, reducing allocations and GC churn.