Jump to content

How to trigger a function when overlap occurs?


agonya
 Share

Recommended Posts

Hi friends. I want to activate a function when two objects overlap. For example when I overlap an object I want to change gravity of a specific area. I can do this with overlap but it does it for one time, after that everything is same again. For example in the game, I will overlap a potion and gravity between position x1 and x2 will be equal -50. But when I do this, I mean when I overlap the potion, it occurs for one second and after that the gravity -50 turns back its past value. 

 

Link to comment
Share on other sites

It sounds like you need to continuously check if the position is within the bounds. Try doing moving whatever you are doing to the update loop.

It's hard to understand what you mean. You are a lot more likely to get helpful responses if you show the applicable code, a link to your game so far, or even just a picture showing what you are trying to do.

Link to comment
Share on other sites

Yes it is a bit complicated, I will try to explain again; in the game, I am picking a potion and when I overlap the potion, takePotion() function works, and this function set gravity -50. And the problem starts here, because I want the takePotion() function to continue infinitely not for just one time. Or think about, for example I have an potion in the game and when I use it, my player's opacity will equal to 0.5 for a time period or limitless. What kind of system should I build? And If I give my source codes;

 

update: function() {

this.game.physics.arcade.overlap(this.ball, this.potionYB, this.takePotionYB, null, this);
this.hellium();

},

takePotionYB: function() {
         
         this.potionYB.kill();
          
     },

hellium: function() {
    
     if(!this.takePotionYB()) {    
         
         if(this.ball.x > 3350 && this.ball.x < 3480) {this.ball.body.gravity.y = -200;}else 
         {this.ball.body.gravity.y = 500;}

     }else if(this.takePotionYB()){
          return;
      },
     
     },

 

The codes are most probably incorrect, I write these codes here because maybe they can help you to understand what I mean. @Arcanorum

Link to comment
Share on other sites

The overlap callback function will run while there is an overlap. This example demonstrates it better:

I think the problem in your code is that your if statement will always be true (the syntax is valid, but the comparison is not) and so it will override the gravity. If I understand you correctly, you want the potion to influence the gravity of your ball , but if the ball did not take(overlap) the potion, then you want some other kind of gravity applied. In that case you can do something like this:


update: function() {
this.game.physics.arcade.overlap(this.ball, this.potionYB, this.takePotionYB, null, this);
this.hellium();
},

takePotionYB: function() {
         this.ball.gravity.y=-50;
         this.potionYB.kill();
     },

hellium: function() {    
	if(this.ball.body.gravity.y!=-50){ //check if we already have -50 gravity or not
		if(this.ball.x>100&&this.ball.x<200){
			this.ball.body.gravity.y=-1;
		}else{
			this.ball.body.gravity.y=1;
		}
	}
},

here is the above code in an example for your reference:

You could also do some boolean check instead of checking the gravity value in the hellium function.:


var tookPotion;

update: function() {
this.game.physics.arcade.overlap(this.ball, this.potionYB, this.takePotionYB, null, this);
this.hellium();
},

takePotionYB: function() {
         this.ball.gravity.y=-50;
         this.potionYB.kill();
         tookPotion=true;
     },

hellium: function() {    
	if(!tookPotion){ //check if we already have -50 gravity or not
		if(this.ball.x>100&&this.ball.x<200){
			this.ball.body.gravity.y=-1;
		}else{
			this.ball.body.gravity.y=1;
		}
	}
},

hope this helps..

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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