Jump to content


Recommended Posts

(Warning, new Phaser user ahead) 


I´m making my first tests with States on Phaser, and I am now having a problem creating functions inside states that are not the predefined ones (create, update, etc).


All in all, this is the code that is giving me trouble:


   this.physics.arcade.overlap(player, stars, collectStar, null, this);

I define my collectStar function like this, before it is ever called:


collectStar: function(){// Removes the star from the screen   star.kill();   //  Add and update the score   score += 10;   scoreText.text = 'Score: ' + score;},

This is inside a state on a different Javascript file called Lvl1.js.


The error I am receiving when running on Chrome is this:

Uncaught ReferenceError: collectStar is not defined             Lvl1.js:127

Line 127 is where collectStar is called.


Link to comment
Share on other sites



I have been following the template, somewhat, but I do not remember seeing anything about functions other than the ones that are overloaded.




Adding "this" solved that, and other issues. Do I have to use "this" every time I reference a variable or function inside the state it was declared? How can I reference items that were initialized outside the current state?

Link to comment
Share on other sites

If you have made a function outside the create function() for example,

you have to use the 'this', but if you have made the function inside the create function, then you don't have to use 'this';


And for variable is the same but in states not the function, 

for example you have 2 states  : MainMenu and Game


If you have a variable 'var' in MainMenu and you want to use this variable 'var' in Game you have to use 'this.var'

Link to comment
Share on other sites


  • Recently Browsing   0 members

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