Saturday, 15 January 2011

pixi - Javascript call constructor in constructor -



pixi - Javascript call constructor in constructor -

i'm working pixijs question general. created constructor (1) , within constructor (1) phone call constructor (2). need access methods of constructor (2) constructor (1) alway output 'cannot read property 'renderer' of undefined'. doing wrong?

call constructor 1 , it's method 'animate':

stage3 = new site3(); requestanimationframe(stage3.animate);

constructor 1:

function site3() { this.fullstage = new fullscreenstage("intimg"); this.snowfrontblur = new pixi.blurfilter(); this.snowfrontblur.blurx = 5; this.snowfrontblur.blury = 5; this.snowfront = spritefromimage("resources/img/snow.png",0,0,0.5,0.5); this.fullstage.stage.addchild(this.snowfront); this.snowfront.filters = [this.snowfrontblur]; } site3.prototype.animate = function() { this.fullstage.renderer.render(this.fullstage.stage); requestanimationframe(this.animate); };

constructor 2:

function fullscreenstage(cavansid){ this.renderer = new pixi.webglrenderer(ww, wh, null, true); document.getelementbyid(cavansid).appendchild(this.renderer.view); this.interactive = true; this.stage = new pixi.stage(0x000000, this.interactive); }

the problem face has how js binds context function: it's ad-hoc binding. context (represented this keyword) can vary depending on how , function object invoked. in statement:

requestanimationframe(stage3.animate);

you pass reference animate function object requestanimationframe, in doing so, function looses context, hence when phone call function within requestanimationframe, this keyword not bound stage3. easiest prepare pass entire object:

requestanimationframe(stage3); //in requestanimationframe: function requestanimationframe(instance) { instance.animate(); }

in case, this.fullstage.renderer.render(this.fullstage.stage); within animate function resolve correctly.

an other alternative bind context function in more permanent way:

site3.animate.bind(site3);

i'll add together links details on js's context binding on time, starting with:

this reply of mine create sure check links @ bottom

javascript "this" keyword , closure compiler warnings

javascript pixi

No comments:

Post a Comment