Choeeey Posted December 11, 2014 Share Posted December 11, 2014 Hi all, At the line ' screen.push(newRow);' it says this is not a function, adding var screen in front of it fixes this, however then at the bottom it doesn't know what screen is, Could anyone help please? Here is my code: myGame.randomMaze = function(game1) { game = game1; }; var ROWS = 10; var COLS = 10; var screen; // font size var FONT = 32; var map; myGame.randomMaze.prototype = { create: function() { this.initMap(); // initialize screen screen = []; for (var y = 0; y < ROWS; y++) { var newRow = []; screen.push(newRow); for (var x = 0; x < COLS; x++) newRow.push( this.initCell('', x, y) ); } this.drawMap(); initCell: function(chr, x, y) { // add a single cell in a given position to the ascii display var style = { font: FONT + "px monospace", fill:"#fff"}; return game.add.text(FONT*0.6*x, FONT*y, chr, style); }, update:function(){ }, initMap: function() { // create a new random map map = []; for (var y = 0; y < ROWS; y++) { var newRow = []; for (var x = 0; x < COLS; x++) { if (Math.random() > 0.8) newRow.push('#'); else newRow.push('.'); } map.push(newRow); } }, drawMap: function() { for (var y = 0; y < ROWS; y++) for (var x = 0; x < COLS; x++) screen[y][x].content = map[y][x];}}; Link to comment Share on other sites More sharing options...
Elindur Posted December 11, 2014 Share Posted December 11, 2014 Hi,Maybe I'm too tired, but I don't see where the create() function ending brace is ^^Otherwise for the push(), I don't see any problem. I tried a :var screen;screen = [];screen.push("test");and there was no error... Link to comment Share on other sites More sharing options...
Choeeey Posted December 12, 2014 Author Share Posted December 12, 2014 Thanks for replying, the problem is because in my code the push is inside a loop.In your code this isn't the case. What is really annoying is there is no problem where it does map.push Link to comment Share on other sites More sharing options...
JUL Posted December 12, 2014 Share Posted December 12, 2014 yep elindur is right. Take a closer look at your create function, it can't work like that. Link to comment Share on other sites More sharing options...
Choeeey Posted December 12, 2014 Author Share Posted December 12, 2014 I had actually fixed that problem before I posted this and forgot to add it in the post. The problem still remains. Any other ideas? Link to comment Share on other sites More sharing options...
Choeeey Posted December 12, 2014 Author Share Posted December 12, 2014 also at the very bottom it doesn't like the use of '0' Link to comment Share on other sites More sharing options...
Elindur Posted December 12, 2014 Share Posted December 12, 2014 I don't think that using a push() inside a for() loop wil create this problem. I also tried : var screen;screen = [];for (var i=1;i<5;i++){ var newRow = []; screen.push(newRow);}and there is still no error... the problem can't be the push() method, it has to be the screen variable.Could you show us all of your code, or a demo page ? And/or copy-paste the full Javascript error that happens ?Also, about which '0' are you talking about in your last question ? Link to comment Share on other sites More sharing options...
rtlehr Posted December 12, 2014 Share Posted December 12, 2014 I believe you need to addthis.screen = [];tomyGame.randomMaze = function(){}and then reference it asthis.screen.push(XXX);andthis.screen[x];in the prototypes. (remove the screen = [] inside of create: function() {}) Link to comment Share on other sites More sharing options...
Choeeey Posted December 14, 2014 Author Share Posted December 14, 2014 Thank you so much everyone for your advice, the solution was what rtlehr posted Link to comment Share on other sites More sharing options...
Recommended Posts