kidos Posted August 17, 2014 Share Posted August 17, 2014 I'm trying to find the best way to pass argument (params) to the collide callback but I don't know how to do it.For example, I have this collision check for the player and for the enemies (which works great):this.game.physics.arcade.collide(player.sprite, this.enemies, player.killEnemy, null, player);But I'd like to pass player.killEnemy also a score parameter, ex: player.killEnemy(score) but obviously it doesn't make sense.So, how could I pass parameters to a callback function? Thanks! Link to comment Share on other sites More sharing options...
Consty Posted August 18, 2014 Share Posted August 18, 2014 Assuming the killEnemy is a function, you can wrap the callback for the collision inside a function like the following example.this.game.physics.arcade.collide(player.sprite, this.enemies, function(){ player.killEnemy(score);}, null, player);Where you get the score variable from is up to you . However, this creates a new function every time this code gets executed, which can be optimized by declaring the function elsewhere and then pass the reference to the collide function. But, if you just want to see how you can do it, this should suffice lewster32 1 Link to comment Share on other sites More sharing options...
kidos Posted August 18, 2014 Author Share Posted August 18, 2014 Assuming the killEnemy is a function, you can wrap the callback for the collision inside a function like the following example.this.game.physics.arcade.collide(player.sprite, this.enemies, function(){ player.killEnemy(score);}, null, player);Where you get the score variable from is up to you . However, this creates a new function every time this code gets executed, which can be optimized by declaring the function elsewhere and then pass the reference to the collide function. But, if you just want to see how you can do it, this should suffice thanks, but I need the two objects which passed to this collision also.from the doc:"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." So, I have 1 player and a group of enemies. I'de like to set different score when the player is killing an enemy (different enemies).Which means that I need to pass also two collided objects (the player and the enemies group) and if I do it in your way I'm not getting any collided object (just the score) How can I pass the collided objects (as original) and also the 'score' param in addition? Link to comment Share on other sites More sharing options...
kidos Posted August 18, 2014 Author Share Posted August 18, 2014 Oh, I think I got it, this one works:this.game.physics.arcade.collide(player.sprite, this.enemies, function(obj1, obj2){ player.killEnemy(obj1, obj2, score);}, null, player);Is this is the right way to do it? Link to comment Share on other sites More sharing options...
Consty Posted August 18, 2014 Share Posted August 18, 2014 Yes that is a way to do it. If it is the correct way is always debatable depending on your project etc..But this should definitely work. In my first response I did not include the collided objects, because you did not specify that you wanted them in your initial question Hope that this has helped you kidos 1 Link to comment Share on other sites More sharing options...
kidos Posted August 18, 2014 Author Share Posted August 18, 2014 thanks! Link to comment Share on other sites More sharing options...
Reinkaos Posted September 22, 2015 Share Posted September 22, 2015 Hey bro, Just adding that you can pass params to the callback function like this as well, this.game.physics.arcade.collide(sprite, sprite2, function, null, { this: this, var1: "Var1", var2: "Var2" }); Then inside the function console.log(this.var1); Hope this helps . Link to comment Share on other sites More sharing options...
Recommended Posts