How to call a function when 2 objects stop overlapping #2517
-
So, for example, i have a constantly moving object (let me call it mov). When it crosses another object (let me name it stat), i want function a to be called, when mov and stat stop overlapping (mov goes beyond the stat's trigger) i want function b to be called can someone please help? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
or simply, when they overlap, some bool gets true, when they stop overlapping, bool gets false |
Beta Was this translation helpful? Give feedback.
-
So there are two ways to achieve what you want to do. I say two there are probably way more but only two that I know of.
1. WIth FlixelYou can use FlxG.overlap to check if two objects have overlapped. FlxG.overlap(mov, stat, (_, _) -> functionA()); To detect if the two objects have exited overlapping with just plain old HaxeFlixel is a little more difficult. What you could do is have a boolean variable that // just below class name
var objectsTouched = false;
// inside update() function
FlxG.overlap(mov, stat, (_, _) -> {
functionA();
objectsTouched = true;
});
if (!FlxG.overlap(mov, stat) && objectsTouched) {
functionB();
} Note: The above code is not refactored so I understand that improvements can definitely be made to it 2. With EchoThis option has the caveat of first installing echo-flixel, putting it into your project.xml file, putting // inside create() function
mov.add_body();
stat.add_body({mass: 0});
mov.listen(stat, {
separate: false,
enter: (_, _, _) -> functionA(),
exit: (_, _) -> functionB(),
}); The choice is completely up to you. |
Beta Was this translation helpful? Give feedback.
So there are two ways to achieve what you want to do. I say two there are probably way more but only two that I know of.
1. WIth Flixel
You can use FlxG.overlap to check if two objects have overlapped.
To detect if the two objects have exited overlapping with just plain old HaxeFlixel is a little more difficult.
What you could do is have a boolean variable that
triggers when they overlap and in the update function check if they are not overlapping when that boolean variable is true.