I'm creating an animation piece using EaselJS and am running into a problem with it's Inheritance when trying to declare reusable objects.
For example, in my animation piece I want to draw multiple coins at different points on the canvas, and then animate said coin.
This is how my code looks like at the moment:
(function() {
var Coin = function(container) {
this.initialize(container);
}
var c = Coin.prototype = new createjs.Container();
c.Container_initialize = c.initialize;
c.initialize = function() {
this.Container_initialize();
var coinInstance = this,
coin = new createjs.Bitmap(loader.getResult('coin'));
createjs.Ticker.addEventListener('tick', function() {
var characterBitmap = character.children[0].children[0];
var coinBitmap = coinInstance.children[0],
collided = ndgmr.checkPixelCollision(characterBitmap, coinBitmap);
if(collided && coinInstance.alpha == 1) {
createjs.Tween.get(coinInstance).to({y: coinInstance.y-100, alpha: 0}, 300);
}
});
this.addChild(coin);
}
window.Coin = Coin;
}());
function drawCoin(container, positionX, positionY) {
var coin = new Coin();
coin.x = positionX;
coin.y = positionY;
container.addChild(coin);
}
Now to clarify - this piece of code does work - however I feel it could be better executed. Ideally I would like to be able to declare the function the event listener fires once (e.g. how I have c.initialize, have c.animate). My problem is when I make this change, the animation looses the instance of 'this' and I can no longer locate the specific instance of the coin to animate based on its properties.
Any ideas fellow developers?