Jump to content

Is there any way to add an argument to the collision check, that doesn't involve creating new functions for each time the collision happens?


Grandy12
 Share

Recommended Posts

So, here is the thing; I started a project in 2.5d. To do so, I created a shadow under the character that is in fact a platform which follow his x position, and when he collides with it his y speed becomes 0.

 

Problem is, every enemy character in the screen must have his own shadow, right? Otherwise they'd fall to the bottom of the screen. And each enemy character must have his own shadow, to prevent they colliding with each other's shadow and becoming stuck.

 

I managed to create those. no problem.

 

Problem is making them collide with their own shadows.

 

If there were a way to add arguments to collide, I'd just go

for (var i = 0; i < fighters.length; i++){game.physics.arcade.collide(fighters.getAt(i), fighters.getAt(i).shadow, fellDown(fighters.getAt(i)), null, this)function fellDown(fighter){fighter.body.gravity.y = 0;}

But that doesn't work.

 

 

 

I understand you can call it by going 

for (var i = 0; i < fighters.length; i++){game.physics.arcade.collide(fighters.getAt(i), fighters.getAt(i).shadow, function(){fighters.getAt(i).body.gravity.y = 0;}, null, this)

But that would create a new function everytime a character spawned or jumped, and I'm afraid that would eventually start lagging a good ammount.

Link to comment
Share on other sites

Unless I'm completely misunderstanding, can't you create references to your fighter and their shadow and pass those references to your collision function?

for (var i = 0; i < fighters.length; i++){var f = fighters.getAt(i);var s = fighters.getAt(i).shadow;game.physics.arcade.collide(f, s, fellDown(f.body), null, this)function fellDown(fighter){fighter.gravity.y = 0;}

It's not much different than what you were doing except 'f' is a reference to the instance of the fighter sprite...I think?

Link to comment
Share on other sites

Unless I'm completely misunderstanding, can't you create references to your fighter and their shadow and pass those references to your collision function?

for (var i = 0; i < fighters.length; i++){var f = fighters.getAt(i);var s = fighters.getAt(i).shadow;game.physics.arcade.collide(f, s, fellDown(f.body), null, this)function fellDown(fighter){fighter.gravity.y = 0;}

It's not much different than what you were doing except 'f' is a reference to the instance of the fighter sprite...I think?

 

 

...okay, now I feel like a fool >_>;

 

I'm 100% sure I tried doing exactly this before and got an 'not a function' error, but now I tried again just to confirm and it worked.

 

I must've screwed up the coding somewhere.

 

Thanks.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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