Jump to content

Uncaught TypeError: undefined is not a function with Discover Phaser Game Build


boomer1204
 Share

Recommended Posts

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.js

var 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

 

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

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

 

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)

 

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

post-9637-0-81768300-1426093278_thumb.pn

Link to comment
Share on other sites

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

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'});

Link to comment
Share on other sites

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

  • 2 years later...
  • 2 years later...

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

 Share

  • Recently Browsing   0 members

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