Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

10 years ago by susnick

Hello, I am trying to access the screen from a function i created in main.js.

goToCoordinate: function() {
this.screen.x = (this.currentTile.x-1) * 64;
this.screen.y = (this.currentTile.y-1) * 64;
},


Screen is undefined and so is currentTile. currentTile was created just above the init portion of main.js.

MyGame = ig.Game.extend({

mouseLast: {x: 0, y: 0},
mapSize: {x: 250, y: 250},
currentTile: {x: 1, y: 1},

// Load a font
font: new ig.Font( 'media/consolasfont.png' ),

init: function() {

If someone could assist it would be much appreciated.

10 years ago by Krisjet

"this" is a very scary keyword, even seasoned javascript veterans have problems knowing what this refers to in many situations. Here I'm guessing goToCoordinate is in some other class, or you're calling it from within a different scope (like in a forEach-loop for examle). Just refer to ig.game.screen.x and ig.game.currentTile and you'll be fine.

I've made a habit of always pointing directly to ig.game.whatever if I'm trying to access something I created in the main-function, even if I'm working within the main-function itself. Sometimes you've changed scope without realizing it and you get these inexplainable bugs, and it's just easier to read anyway.

10 years ago by Jesse

Is goToCoordinate a member of your game class? Otherwise, can I see more of the code?

10 years ago by susnick

@Krisjet thanks that worked. Was trying to stay away from passing this into each function and assigning to "that". Ill keep it simple as you suggested.

@Jesse Yes, goToCoordinate is in the game class
Page 1 of 1
« first « previous next › last »