Jump to content

Problem with functions


AbdSab
 Share

Recommended Posts

Hi all,

 

I have a little problem with functions in states, when i want to use a function from a function i get an error of undefined.

 

To clarify here is the code:

Game.Combat.prototype = {        /* create and update codes ....*/        moveToEnnemy: function()	{		var tween = this.game.add.tween(player);		tween.to({x:ennemy.x-32, y:ennemy.y}, 1500, Phaser.Easing.Cubic.None,                           true);	},	moveToStart: function()	{		var tween = this.game.add.tween(player);		tween.to({x:PLAYER_START_X, y:PLAYER_START_Y}, 1500,                          Phaser.Easing.Cubic.None, true);	},		attack: function()	{		this.moveToEnnemy(); /*This is the probleme, i get an error of undefined                                           function*/		                console.log("ATTACKING ...");		this.moveToStart(); //This too.	},}

I get this error :

Uncaught TypeError: undefined is not a function

 

 

Is there any solution in how to use moveToEnnemy and moveToStart into the attack functions ?

Link to comment
Share on other sites

So this gives the correct object (can't decide that on my own, just always check whether this keyword is what you expect it to be).

 

When you just call moveToEnnemy itself can you call it?

Such as: yourObject.moveToEnnemy();

(just define moveToEnnemy with only console.log('test'); and comment the rest so you are sure you don't get some fishy errors but you still get 'test' message to console as a result that the function was actually called ;-).)

Link to comment
Share on other sites

Then define attack outside the object and try calling it again.

 

Such as this:

var tt = {  a: function() {    // something  },  b: function() {    // something  }};tt.c = function () {  return a+b;};

It just an example, so you can se what I mean by outside.

Let us know what it does, ok?

 

btw: I see that you have comma after ending curly brace for attack function, does your object has more properties or why is there?

Link to comment
Share on other sites

I've done what you say but when i click at the button nothing happen,

I think i have to make a player's object that hold all these functions.

Like that:

Game.Combat.prototype = {    create: function()    {        ennemy = this.add.sprite(100,10,"ennemy");        player = this.add.sprite(10,10,"player");        player.moveToEnnemy = function()	{		var tween = this.game.add.tween(player);		tween.to({x:ennemy.x-32, y:ennemy.y}, 1500, Phaser.Easing.Cubic.None,                           true);	};	player.moveToStart = function()	{		var tween = this.game.add.tween(player);		tween.to({x:PLAYER_START_X, y:PLAYER_START_Y}, 1500,                          Phaser.Easing.Cubic.None, true);	};		player.attack = function()	{		player.moveToEnnemy();		                console.log("ATTACKING ...");	};        attackButton = this.add.button(64,this.world.height-96,"button",player.attack,                                       null,0,0,1,0);    },    /* The rest of the code */} 

For the comma i have other properties.

Link to comment
Share on other sites

Does this work for you?

 

Just to be sure what I meant was set the function separately as:

Game.Combat.prototype = {/* create and update codes ....*/moveToEnnemy: function()    {        var tween = this.game.add.tween(player);        tween.to({x:ennemy.x-32, y:ennemy.y}, 1500, Phaser.Easing.Cubic.None, true);    },  moveToStart: function()    {        var tween = this.game.add.tween(player);        tween.to({x:PLAYER_START_X, y:PLAYER_START_Y}, 1500, Phaser.Easing.Cubic.None, true);    }};Game.Combat.prototype.attack = function()    {console.log("ATTACKING ...");        this.moveToEnnemy(); /*This is the probleme, i get an error of undefined function*/        this.moveToStart(); //This too.    };

One thing, when I said check it with console.log() at the beginning it should be as in the example above, you placed console.log() call after this.moveToEnnemy(); that doesn't tell you if the function attack() was called at all, if it was called and error occured with move.toEnnemy() then console.log won't occur either.

Above is the way how I meant you to set it.

 

Could you please set a simple fiddle for your example? No need to put your logic into it, just create same situation of how you create object, set prototype in the same way as you did so we can actually see and test where is the problem? :-) I have a few ideas but can't figure it out of this piece of code itself for sure, if possible working example would greatly help to understand what exactly yo uare doing (well working .. we know it doesn't work but you know what I mean ;-)).

 

------------------------------ Note

Why spelling enemy as ennemy - I'm just curious?

Link to comment
Share on other sites

It's not working, but assingnig these functions to the player's object work fine.

 

Why spelling enemy as ennemy - I'm just curious?

It's just i'am confusing the English enemy with the French ennemi, English is not good on it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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