Sign in to follow this  
AbdSab

Problem with functions

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 ?

Share this post


Link to post
Share on other sites

I'am getting the button object that trigger the attack function.

 

But also when i use moveToEnnemy() instead of this.moveToEnnemy(), it don't work too, also an undefined error.

Share this post


Link to post
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 ;-).)

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.