OSamo Posted December 27, 2014 Share Posted December 27, 2014 (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:127Line 127 is where collectStar is called. Link to comment Share on other sites More sharing options...
AbdSab Posted December 28, 2014 Share Posted December 28, 2014 I think you have to add 'this.' before the function : this.collectStarthis.physics.arcade.overlap(player, stars, this.collectStar, null, this); Link to comment Share on other sites More sharing options...
spencerTL Posted December 28, 2014 Share Posted December 28, 2014 Phaser has a useful set of templates for using states in the resources folder that are very useful: https://github.com/photonstorm/phaser/tree/master/resources/Project%20Templates Link to comment Share on other sites More sharing options...
OSamo Posted December 28, 2014 Author Share Posted December 28, 2014 spencerLT, I have been following the template, somewhat, but I do not remember seeing anything about functions other than the ones that are overloaded. AbdSab, 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 More sharing options...
AbdSab Posted December 28, 2014 Share Posted December 28, 2014 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' OSamo 1 Link to comment Share on other sites More sharing options...
Recommended Posts