"It works! Ha ha, IT WORKS! I FINALLY build something that works!"

Extend createjs.Container

No comments
So I ran into a problem which made me re-look the way that I was using the Sprite and Container classes in the CreateJS framework. Unless you wrap your asset in either of the two you will not be able to adjust properties like x, y and alpha of that asset.

Previously I was doing this

var _container = new createjs.Container(); // now I can adjust the properties
stage.addChild(_container);

var _instance = new MyClass(_instance, imagePath)

_instance.init();


function MyClass(container, src){
    this._container = container;
    this._src = src;
}

MyClass.prototype.init = function(){
    this.bmp = new createjs.Bitmap(this._src);
    this._container.addChild(this.bmp);


}

I then had a problem with inheritance when trying to use getters and setters. Long story short I have discovered a better way of doing it. You can extend either the Sprite or Container classes by using the following code.

var _instance = new MyClass(imagePath);
stage.addChild(_instance);

_instance.x = 50;//now I can access properties
_instance.y = 50;

function MyClass(src) {
        this._src = src
        this.initialize();
    }
    
    MyClass.prototype = new createjs.Container(); //Inheritance from Container
    MyClass.prototype.Container_initialize = MyClass.prototype.initialize;
    MyClass.prototype.Container_tick = MyClass.prototype._tick; 
     
    MyClass.prototype.initialize = function () {
                
        this.Container_initialize();//call to initialize() method from parent class 
        
        this._bitmap = new createjs.Bitmap(this._src);
        this.addChild(this._bitmap);
    }


This is much cleaner and you will run into less errors down the line.

No comments :

Post a Comment