boomer1204 Posted March 10, 2015 Share Posted March 10, 2015 Hey guys, Just bought the Discover Phaser book and working through the code. I have just setup the game and i'm running into an issue with the loading screen. It shows loading and the progress bar get's half way then it throws an error. I have commented out alot of code and the error shows in the phaser.js file at line 34646. It looks like something is wrong with my game.add.text but I can't figure it out for the life of me. Any assistance would be awesome. index.html<!DOCTYPE html><html> <head> <meta charset = 'utf-8' /> <title>First Game</title> <script type = 'text/javascript' src ='phaser.min.js'></script> <script type = 'text/javascript' src = 'js/boot.js' /></script> <script type = 'text/javascript' src = 'js/load.js' /></script> <script type = 'text/javascript' src = 'js/menu.js' /></script> <script type = 'text/javascript' src = 'js/play.js' /></script> <script type = 'text/javascript' src = 'js/game.js' /></script> </head> <body> <div class="gameDiv"> </div> </body></html>here is the load.jsvar loadState = { preload: function() { // Add a loading .... label on screen var loadingLabel = game.add.text(game.world.centerX, 150, 'loading....', { font: '30px Arial', fill: '#ffffff' }); loadingLabel.anchor.setTo(0.5, 0.5); // Display the progress bar var progressBar = game.add.sprite(game.world.centerX, 200, 'progressBar'); progressBar.anchor.setTo(0.5, 0.5); game.load.setPreloadSprite(progressBar); // Load all assets game.load.image('player', 'assets/player.png'); game.load.image('enemy', 'assets/enemy.png'); game.load.image('coin', 'assets/coin.png'); game.load.image('wallV', 'assets/wallVertical.png'); game.load.image('wallH', 'assets/wallHorizontal.png'); // Load a new asset that we will use in menu state game.load.image('background', 'assets/background.png'); }, create: function() { // Go to the menu state game.state.start('menu'); }};the error is Uncaught TypeError: undefined is not a function phaser.js:34646 Thanks for all the help guys. Link to comment Share on other sites More sharing options...
Retronalle Posted March 10, 2015 Share Posted March 10, 2015 If the issues is the game.add.text call then you probably are missing the game object. As the actual call to the game.add.text is correct. Taken form the Phaser example, draw text:var text = game.add.text(game.world.centerX-300, 0, text, style); Link to comment Share on other sites More sharing options...
boomer1204 Posted March 10, 2015 Author Share Posted March 10, 2015 @retronalle, Awesome thanks for the advice. I'll dig around the code when I get home and report back! Link to comment Share on other sites More sharing options...
boomer1204 Posted March 11, 2015 Author Share Posted March 11, 2015 If the issues is the game.add.text call then you probably are missing the game object. As the actual call to the game.add.text is correct. Taken form the Phaser example, draw text:var text = game.add.text(game.world.centerX-300, 0, text, style); So the game variable is called in the game.js file. Is that the one that is called for in all of the game."whatever"."whatever" functions?? Here is the game.js code. If i'm just being a complete newb moron just give me a hint and make me figure it out thanks again!!// Initialise Phaservar game = new Phaser.Game(500, 500, Phaser.AUTO, 'gameDiv');// Define our 'global' variablegame.global = { score: 0};// Add all the statesgame.state.add('boot', bootState);game.state.add('load', loadState);game.state.add('menu', menuState);game.state.add('play', playState);// Start 'boot' stategame.state.start('boot'); Link to comment Share on other sites More sharing options...
stauzs Posted March 11, 2015 Share Posted March 11, 2015 All your code looks fine. * replace phaser.min.js - with unminimised version. latest version is here: https://raw.githubusercontent.com/photonstorm/phaser/master/build/phaser.js - it will give "better" error * get chrome - other modern browser also should be fine - I just found this tutorial - https://developer.chrome.com/devtools/docs/javascript-debugging - for chrome especially check - https://developer.chrome.com/devtools/docs/javascript-debugging#exceptions basically you need to get call stack to that error - and then paste call stack here. it should look something like:window.axa.state.play.create (play.js:9)c.StateManager.loadComplete (phaser.js:18858)c.StateManager.preUpdate (phaser.js:18618)c.Game.updateLogic (phaser.js:26735)c.Game.update (phaser.js:26684)c.RequestAnimationFrame.updateRAF (phaser.js:46614)c.RequestAnimationFrame.start.window.requestAnimationFrame.forceSetTimeOut._onLoop (phaser.js:46598) Link to comment Share on other sites More sharing options...
boomer1204 Posted March 11, 2015 Author Share Posted March 11, 2015 All your code looks fine. * replace phaser.min.js - with unminimised version. latest version is here: https://raw.githubusercontent.com/photonstorm/phaser/master/build/phaser.js - it will give "better" error * get chrome - other modern browser also should be fine - I just found this tutorial - https://developer.chrome.com/devtools/docs/javascript-debugging - for chromeespecially check - https://developer.chrome.com/devtools/docs/javascript-debugging#exceptions basically you need to get call stack to that error - and then paste call stack here. it should look something like:window.axa.state.play.create (play.js:9)c.StateManager.loadComplete (phaser.js:18858)c.StateManager.preUpdate (phaser.js:18618)c.Game.updateLogic (phaser.js:26735)c.Game.update (phaser.js:26684)c.RequestAnimationFrame.updateRAF (phaser.js:46614)c.RequestAnimationFrame.start.window.requestAnimationFrame.forceSetTimeOut._onLoop (phaser.js:46598) Awesome thanks for the advice. I am using the newest phaser and chrome. I added the full phaser instead of the min file. The links were super helpful but when i went to the section for callbacks it didn't return anything and i have posted a screenshot. All though when I click the red arrow next to the error in the console I received this which looks similiar to what you said the call stack should look like. Let me know if this in not enough. Thanks for all the help guys!Uncaught TypeError: undefined is not a functionphaser.js:34646 Phaser.GameObjectFactory.textmenu.js:16 menuState.createphaser.js:18858 Phaser.StateManager.loadCompletephaser.js:18631 Phaser.StateManager.preUpdatephaser.js:26735 Phaser.Game.updateLogicphaser.js:26684 Phaser.Game.updatephaser.js:46614 Phaser.RequestAnimationFrame.updateRAFphaser.js:46598 Phaser.RequestAnimationFrame.start._onLooprequestAnimationFrame (async)phaser.js:46616 Phaser.RequestAnimationFrame.updateRAFphaser.js:46598 Phaser.RequestAnimationFrame.start._onLooprequestAnimationFrame (async)phaser.js:46616 Phaser.RequestAnimationFrame.updateRAFphaser.js:46598 Phaser.RequestAnimationFrame.start._onLooprequestAnimationFrame (async)phaser.js:46616 Phaser.RequestAnimationFrame.updateRAFphaser.js:46598 Phaser.RequestAnimationFrame.start._onLooprequestAnimationFrame (async)phaser.js:46616 Phaser.RequestAnimationFrame.updateRAFphaser.js:46598 Phaser.RequestAnimationFrame.start._onLoop Link to comment Share on other sites More sharing options...
stauzs Posted March 11, 2015 Share Posted March 11, 2015 can you paste menu.js,particulary line 16 but full file would be more useful from call stack we can see that phaser has competed loading state and started menu state... and something bad is happening there Link to comment Share on other sites More sharing options...
boomer1204 Posted March 12, 2015 Author Share Posted March 12, 2015 Here you guys go.var menuState = { create: function() { // Add background image game.add.image(0, 0, 'background'); // Display the name of the game var nameLabel = game.add.text(game.world.centerX, 80, 'Super Coin Box', {font: '50px Arial', fill: '#ffffff'}); nameLabel.anchor.setTo(0.5, 0.5); // Show the score at the center of the screen var scoreLabel = game.add.text(game.world.centerX, game.world.centerY, 'Score :' + game.global.score, {font: '25px Arial', fill: '#ffffff'}); scoreLabel.anchor.setTo(0.5, 0.5); // Explain how to start the game var startLabel = game.add.text(game.world.centerX, game.world.centerY, game.world.height - 80, 'press the up arrow key to start', {font: '25px Arial', fill: '#ffffff'}); startLabel.anchor.setTo(0.5, 0.5); // Create new phaser keyboard variable the up key var upkey = game.input.keyboard.addKey(Phaser.Keyboard.UP); // When the up is pressed call start function upKey.onDown.addOnce(this.start, this); }, start: function() { // Start the actual game game.state.start('play'); }}; Link to comment Share on other sites More sharing options...
stauzs Posted March 12, 2015 Share Posted March 12, 2015 var startLabel = game.add.text( game.world.centerX, game.world.centerY, game.world.height - 80, //<- this is incorrect 'press the up arrow key to start', {font: '25px Arial', fill: '#ffffff'} ); // arguments are: game.add.text( x, y, text, style, group ) // replace line 16 with: var startLabel = game.add.text(game.world.centerX, game.world.centerY, 'press the up arrow key to start', {font: '25px Arial', fill: '#ffffff'}); boomer1204 1 Link to comment Share on other sites More sharing options...
boomer1204 Posted March 12, 2015 Author Share Posted March 12, 2015 var startLabel = game.add.text( game.world.centerX, game.world.centerY, game.world.height - 80, //<- this is incorrect 'press the up arrow key to start', {font: '25px Arial', fill: '#ffffff'});// arguments are:game.add.text( x, y, text, style, group)// replace line 16 with:var startLabel = game.add.text(game.world.centerX, game.world.centerY, 'press the up arrow key to start', {font: '25px Arial', fill: '#ffffff'}); Spot on! Thank you very much I really appreciate it!! Link to comment Share on other sites More sharing options...
Dimaslocoxtr Posted November 19, 2017 Share Posted November 19, 2017 Hi, i am new in this and i have the same error i made a game in three states (Menu, game, gameover) the "MENU" has a button that launch de "game" but when i click it nothing happens and i press inspect and has this error: please help Link to comment Share on other sites More sharing options...
markfilan Posted April 1, 2020 Share Posted April 1, 2020 This is a common JavaScript error that happens when you try to call a function before it is defined. You get "'undefined' is not a function" error when you try to execute a function that is uninitialized or improperly initialized . It means that the expression did not return a function object. So you have to figure out what you are trying to execute isn't a function. In Javascript , when you execute a function, it's evaluated like the following: expression.that('returns').aFunctionObject(); // js execute -> expression.that('returns').aFunctionObject // what the JS engine does That expression may be complex. So when you get undefined is not a function it means that expression did not return a function object. So you have to figure out what you are trying to execute isn't a function. Link to comment Share on other sites More sharing options...
Recommended Posts