Phaser Functions with parameters in state issue

Recommended Posts

Hi guys,



Maybe a silly question, but I recently started using States in my game and now I am converting my functions to work with states.


The functions without parameters work fine, but the ones with parameters don't.


For example, when I call:


this.createBullet(x, y);


I get the error:

Uncaught typeError: undefined is not a function.


The function createBullet(x, y) does exist, it is created like this:

createBullet: function(x, y) {

      // blablabla content of function



p.s. these functions are in my Game.js state


Can anyone tell me what I'm doing wrong?

It has to be a silly mistake but I don't know how to fix it :(





Share this post

Link to post
Share on other sites

undefined issues mean if you have a firebullet function and are trying to instantiate it, you dont call fireBullet(); it is called with:

update: function(){fireBullet(); // without states this works, in states, nothis.fireBullet(); // this is for state like set ups.}

There should be no need for extra parameters, it must be the way you are calling the function. Use this.fireBullet(); [Example] instead of firebullet();

Share this post

Link to post
Share on other sites

I get that, but some of my functions need to have a couple of variables to work.


Normally, when I want to execute a certain function with parameters I would just call:



I know that at states you have to use this.function if the function is inside of the game.

So then I would call this.function(a)


However when I do that I get the error

Uncaught typeError: undefined is not a function
The function(a) does exist.
It might be that the context I'm using the function in is wrong, and not the function itself.
I'm using it within a socket.on like:

socket.on('newBullet', function(data) {

Share this post

Link to post
Share on other sites

Parameters aren't the cause of your problem I doubt, much more likely a scope issue. You need to show some actual code, it's hard to guess from posts alone. But the following is plucked from a game I'm working on (and abstracted down heavily to make it clear), may have some pointers:

TimesOfLores.Game = function (game) { = game;;    this.player;};TimesOfLores.Game.prototype = {    create: function () { = new TimesOfLores.Map(this);        this.player = new TimesOfLores.Player(this);        TimesOfLores.cursors.up.onDown.add(this.moveForward, this);    },    moveForward: function () {        this.checkExit(this.player.x, this.player.y);    },    checkExit: function (x, y) {        if (, y))        {             this.player.moveForward();        }    }};

Share this post

Link to post
Share on other sites
	    socket.on('newBullet', function(data) {	    	this.createBullet(data);	    });

Your 'this' reference within the anonymous function refers to itself, not the scope outside of the socket callback. Generally the way around this is to do 'var self = this;' within the correct scope, and then call 'self.createBullet(data);' instead.

            var self = this; // ensure 'self' is the desired context, as 'this' will change depending on where you call it	    socket.on('newBullet', function(data) {	    	self.createBullet(data);	    });

Share this post

Link to post
Share on other sites

You can call it anything, I tend to use 'self' but '_this' or 'that' are other common replacements. I tend to put 'var self = this;' at the top of my functions so that I can access that scope from inside the many anonymous functions I tend to end up using. Handily, Phaser's signals allow you to specify a context so it's not necessary for those, e.g., function() {; // is still accessible because...}, this); // ... the last parameter specifies the scope of the anonymous function

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.