Jump to content

Which is correct, and why others work? game, this, this.game


Recommended Posts



I am bit puzzled why all these work (okay I understand why global game works, its Phaser.Game object and can see the functions). So technically there is no problem here, except that I dont understand why these this and this.game work too. After all, where I create preload object the game is not assigned to anywhere.



var game = new Phaser.Game(x, y, Phaser.CANVAS, 'game');var BasicGame = {}; BasicGame.preload = function(game) {};   BasicGame.preload.prototype = {  preload : function() {    var logo = this.add.sprite(game.world.centerX, game.world.centerY, 'background');    logo.anchor.setTo(0.5, 0.5);    ......
but logo could be also defined:


var logo = game.add.sprite(...)       <-- I understand this .. as far as i understand, its that global game... or ...var logo = this.game.add.sprite(...)
And I see that BasicGame.preload is defined as function(game) which has no game object inside it and has no method add inside it (not even in prototype). And that makes me puzzle, why, how, what? What I am missing here?


Which one would be most correct?



Link to comment
Share on other sites

Thanks for the reply.


I did some debuggin. It seems that when entering to preload-method, "this" points to Basicgame.preload and that does contain the add method and game object, which contains again add method, which I assume are the same method (both). Apparently state manager injects the methods and objects as you said ...


So, most likely the correct answer to my question (Which one would be most correct?) is: they all are equal, yet probably this.add could be most "reusable" (if ever needed, maybe one day you drop game object from this or my code does not have global game)? 


That book looks very intriguing. Better get it. 


[edit] Stupid me ... its me who injects the game to the Basicgame.preload ... I better get my coat. [edit]



Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...