Jump to content

Battles / Passing enemies


Recommended Posts

Hey guys,


So I'm making this lite RPG with Phaser and right now I have only one battle hardcoded.

I want to make it more modular so that I don't have to hardcode every single fight.


so i'm doing this:

this.physics.arcade.collide(hero, snakemonster, this.startFight, null, this);

to start the battle which is defined as follows


startFight: function(enemy) {
this.state.start('Fight', false, false, enemy);
all this is on my main world.
the problem arises when I actually try to pass an argument to startFight like this
this.physics.arcade.collide(hero, snakemonster, this.startFight(snakemonster), null, this);
it will trigger the fight all the time, from anywhere on my main world.
has anyone run into this issue or have any ideas on what I'm doing wrong?
also, then i want to get enemy's abilities on my Fight.js file 
thank you,
Link to comment
Share on other sites

Yes, I got around it by doing something like this: (keep in mind I'm a noob so this could be a terrible practice, but it worked for me)

this.physics.arcade.collide(hero, snakemonster, function () {    this.startFight(snakemonster);}, this);
Link to comment
Share on other sites

Okay I was just being lazy. Looking at the Phaser code, you should just do the following:
var myFancyFunction = function (collider, collidee) {  //access to both objects here.};game.physics.arcade.collide(this.collider, this.collidee, myFancyFunciton);
edit: from the Phaser comments on the callback function... 
An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.



Link to comment
Share on other sites


  • Recently Browsing   0 members

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