Jump to content

Check overlap between sprites in if statement


Recommended Posts



First of all I would like to thank you all for the great support in learning about this great framework  :D 

I have another question:

Is it possible to check for an overlap in an if statement? Currently I have this in my update function:

game.physics.arcade.overlap(player, enemyGroup, this.collisionHandler, null, this);

and this in my collisionHandler:

if(game.physics.arcade.overlap(player, enemyGroup.children[0])) {    this.vow1();}

But as expected, it's not working... Is it even possible? And if yes, how?  :) 

Thanks in advance! 


Link to comment
Share on other sites

I'm new to this stuff, too, but I guess it is not forbidden to try to help.


The first use of the overlap function seems to be right. So if player hits any sprite in the enemyGroup group the collisionHandler function gets executed.


Now on the collisionHandler function. You get the two colliding sprites as arguments. So it looks something like

var collisionHandler = function(_player, _enemy) {  // Your code here}

What I don't get is why are you trying to check if there is an overlap between your player and the first enemy in the enemyGroup. I can think of two problems that you are trying to solve.


1. You want to check if the player hits the first enemy


Then you can put in the first enemy directly in the overlap function like this:

game.physics.arcade.overlap(player, firstEnemy, this.collisionHandler, null, this);

2. You want to check if the player hits any enemy


Then you can just remove the if part from your collisionHandler

var collisionHandler = function(_player, _enemy) {  this.vow1();}

So maybe try to describe your stuff more detailed so that I can be better help for you :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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