Jump to content

Trouble With Adding A New Function


fedora
 Share

Recommended Posts

I am building a game using this for the structure:  https://github.com/photonstorm/phaser-examples/blob/master/examples/wip/state/test1.php   Each state is a separate .js file.

 

As a test I have taken an existing game I developed in a single file (which works fine) and broke it up into the states to test the pieces and parts.  My problem is that I can get everything to work fine until I try to add a new function outside of these:

 

BasicGame.Game.prototype = {    create: function () {        //  Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out!    },    update: function () {        //  Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out!    },    quitGame: function (pointer) {        //  Here you should destroy anything you no longer need.        //  Stop music, delete sprites, purge caches, free resources, all that good stuff.        //  Then let's go back to the main menu.        this.state.start('MainMenu');    }

An example would be -- if I put the below into the update: function:

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

And add this new function: 

collectStar: function  (player, star) {    // Removes the star from the screenstar.kill();}

The game does not work.  

 

If I comment the above out -- it works fine.  I have tried everything and I am convinced I am probably missing something easy.

 

Any assistance would be greatly appreciated.

 

 

Link to comment
Share on other sites

In the line where you're calling "arcade.overlap", you need to specify it as "this.collectStar":

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

Otherwise JS is looking for a variable in first your local function scope and then the global scope, not finding it, and passing undefined.

 

Don't worry -- it's not easy until you've banged your ahead against it, oh, about a hundred times or so.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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