efecarranza Posted March 8, 2015 Share Posted March 8, 2015 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, fer Link to comment Share on other sites More sharing options...
MikeT Posted March 8, 2015 Share Posted March 8, 2015 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 More sharing options...
Matthew Cooley Posted March 10, 2015 Share Posted March 10, 2015 I'm doing the same thing as MikeT, but it feels a little dirty. If there is a way to pass an object to that handler instead of wrapping it in an anonymous function, that would be great. Link to comment Share on other sites More sharing options...
ZoomBox Posted March 10, 2015 Share Posted March 10, 2015 I think if you add parameters after the last one (this) it will be considered as your function's parameters. Link to comment Share on other sites More sharing options...
Matthew Cooley Posted March 10, 2015 Share Posted March 10, 2015 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 More sharing options...
Recommended Posts