KevinnFtw Posted May 30, 2014 Share Posted May 30, 2014 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 Thanks,Kevin Link to comment Share on other sites More sharing options...
Heppell08 Posted May 30, 2014 Share Posted May 30, 2014 functions in states go:fireBullet: function(){// stuff here}, // the comma is important too Link to comment Share on other sites More sharing options...
KevinnFtw Posted May 30, 2014 Author Share Posted May 30, 2014 Yeah I know that, but most of my functions need one or more parameters. How should I handle them? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 30, 2014 Share Posted May 30, 2014 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(); Link to comment Share on other sites More sharing options...
Heppell08 Posted May 30, 2014 Share Posted May 30, 2014 so if you have your player who fires a bullet, you would have::if( ... keydown ... ){ this.fireBullet();}// orif(... keydown ...){ this.player.body.velocity.x = 250;} Link to comment Share on other sites More sharing options...
KevinnFtw Posted May 30, 2014 Author Share Posted May 30, 2014 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:function(a); 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 errorUncaught typeError: undefined is not a function The function(a) does exist. Edit: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) { this.function(data);}); Link to comment Share on other sites More sharing options...
rich Posted May 30, 2014 Share Posted May 30, 2014 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) { this.game = game; this.map; this.player;};TimesOfLores.Game.prototype = { create: function () { this.map = 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 (this.map.canPass(x, y)) { this.player.moveForward(); } }}; Link to comment Share on other sites More sharing options...
KevinnFtw Posted May 30, 2014 Author Share Posted May 30, 2014 I've uploaded the Game.js here: http://jsbin.com/senarewe/1 The problem is occuring at the createBullet() and createPlayer() functions. Link to comment Share on other sites More sharing options...
lewster32 Posted May 30, 2014 Share Posted May 30, 2014 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); }); Link to comment Share on other sites More sharing options...
KevinnFtw Posted May 30, 2014 Author Share Posted May 30, 2014 Ahh, i was thinking about the 'this' reference, but didn't know what to replace it with.Edit: Awesome, it is working now! Thanks alot Rich and Lewster32 :-) Link to comment Share on other sites More sharing options...
lewster32 Posted May 30, 2014 Share Posted May 30, 2014 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.this.game.time.events.add(1000, function() { this.game.whatever(); // this.game is still accessible because...}, this); // ... the last parameter specifies the scope of the anonymous function KevinnFtw 1 Link to comment Share on other sites More sharing options...
rich Posted May 30, 2014 Share Posted May 30, 2014 Scope. The bane of JS devs worldwide Link to comment Share on other sites More sharing options...
lewster32 Posted May 30, 2014 Share Posted May 30, 2014 Scope is easy, reliance on 'this' being whatever you want it to be - via witchcraft - is the true bane oliversb 1 Link to comment Share on other sites More sharing options...
Recommended Posts