javascript - phaser js undefined variable within loading function -


i'm starting out using phaser js game dev have come across odd issue need on.

let me show code , explain things going wrong:

class simplegame {      game: phaser.game;      csvtm: string;            constructor (csvtm: string) {            this.csvtm = csvtm;            this.game = new phaser.game(800, 600, phaser.auto, 'content', { preload: this.preload, create: this.create });      }        test() {          console.log("map test: " + this.csvtm);      }        preload() {          console.log("map preload: " + this.csvtm);          this.game.load.image('logo', 'empty_room.png');          this.game.load.tilemap("itsthemap", this.csvtm, null, phaser.tilemap.csv);          this.game.load.image("tiles", "concrete.png");      }        create() {          console.log("map create: " + this.csvtm);          var map = this.game.add.tilemap('itsthemap', 32, 32);          map.addtilesetimage("tiles");          var layer = map.createlayer(0);          layer.resizeworld();        }    }

now i'm trying here pass csv file path constructor of simplegame object. when using absolute path, works fine , i'l able see grid, etc. issue comes in when try use variable. notice have 3 log statements displaying contents of csvtm variable. first:

var game = new simplegame(msg["commands"][0][1]);              game.test();

which loads new phaser object , passes file path constructor. im 100% sure local variable csvtm set becuase when teh game.test() above see file path. in preload , create csvtm undefined.. breaking code. notice game object made in constructor seems work , defined.

does know why local variable string undefined in preload , create while game local variable seems defined?

thanks help!

it hard me sure since not have access of code. think might case of classic javascript "this" problem.

try defining methods as:

preload = () =>  { ... } create = () =>  { ... } 

this make sure "this" inside functions correct "version" of "this".

there many questions (and answers) how "this" works in typescript/javascript on stackoverflow answer previous similar question might explain why getting behaviour: visual studio shows wrong value `this` in typescript

edit:

as marwane k.a. pointed out in comment answer issue can solved replacing

{ preload: this.preload, create: this.create } 

in initiation "this".


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -