Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Check Overlap inside of Collision


midda
 Share

Recommended Posts

Hi everybody,

 

i'm testing phaser since a few days ago and i am quite happy.

But I've got a little problem, so I hope for your help.

 

I'm coding a simple plattform game (some kind of doodle jump) and in the update-method i check if the player and the plattformGroup collide.

Inside this CollisionCallback i'd like to check if the player and a sprite of the coinGroup overlap and call another function if thats true.

 

I think it's way more performant to just check the coin-collision if the player stands on the plattform than every frame, even if he is jumping in the air (in this chase he souldn't get any coins, even if they overlap).

update: function() {    //...    game.physics.arcade.collide(player, plattformGroup, this.plattformCollision);    //...}plattformCollision: function (tPlayer, tPlattform) {    game.physics.arcade.overlap(tPlayer, coinGroup, this.collectCoins)    //...}collectCoins: function (tPlayer, tCoin) {    //...}

I've read that the overlap/collide-Function aren't recursive, so that's why i can't call any function inside the collisionCallback?

Any clous how i shoud solve this problem?

 

Thanks for the great framework and the help.

midda

 

PS: I hope this isn't a repost, but i haven't found anything like this.

Link to comment
Share on other sites

You didn't state what your problem was. Are you not overlapping coins?

 

A couple of things: check the collision in the update method. Don't worry about performance problems until you have performance problems and, even then, only do something when you can prove through a profiler that it's a real problem.

 

The "non-recursive" thing is that the collision calls don't check Groups within other Groups. So it your coinGroup had other groups inside of it those groups wouldn't be checked.

Link to comment
Share on other sites

Thank you for the reply.

so my gess about the "non-recursive" thing was a dead end ... and you are right, i should have made my point clearer:

 

i check the collision of player & plattform in the update-method. Inside the collistionCallback i'd like to check if player & coinGroup overlap and afterwards call the jump-function. if i call these 3 steps in the update-method every thing works.

But i can't call the coin-collision inside the plattform-collisionCallback. If i do so this collision will never be fired though the 2 sprites overlap.

Also i can't call another function (in my case "this.jump();"). Error: "TypeError: this.jump is not a function".

 

I somehow solved the jump-function problem (set inside the callback a property "player.jump = true;" an check this in the update-function, after the collision), but i still don't unterstand why i can't call this function inside the callback.

 

So: how do i check the coin.overlap inside the plattform-collisionCallback and call the jump-function inside there too?

 

Thanks a lot!

Link to comment
Share on other sites

The fifth parameter to "collide" is the callback context. You want to pass "this" as that argument (and probably pass "null" as the fourth argument).

 

JavaScript is loose about its "this". If you've used Python, it's very easy to make a method unbound from its context. That's why the value of "this" within your callback was the global object (window) instead of the object you meant it to be. You can verify that by typing "console.log(this);" in your function before adding "this" as the fifth param to collide.

Link to comment
Share on other sites

ohh man -.-'

you are so right, i'm still not very familiar with 'this'.

thanks to your hint, i solved my problem:

//updategame.physics.arcade.collide(player, platformGroup, this.platformCollision, null, this);   platformCollision: function (tPlayer, tPlatform) {        game.physics.arcade.overlap(player, gimmickGroup, this.gimmickAction);

now it works perfect!

 

Thank you!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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