Hi, I think that all solutions in game development must be subconscious. So I’ll try to help you with my methods. Just think simply. How can you detect what you need yourself?
Of course in this post I’ll explain how to resolve your issue. But I think, that it will be better to explain how you can get solutions for you.
Always try to use simple logic in your solutions. In your case you have zone, where you need to detect that character had fallen down into hole. So you need to detect 2 intersections.
- intersection with zone’s top line
- intersection with zone’s bottom line
Which is very easy to detect. Most of game objects in Phaser 3 have function called getBounds()
. This function returns Phaser.Geom.Rectange
type object, which shows bounds of your game object in the world. So you have 2 objects: character and hole. You can get bounds of them. Then you can get interesting lines to check for intersection.
var characterBounds = character.getBounds()
var holeBounds = hole.getBounds()
The line that must interest you in character is 2nd or 4th line (vertical). So you can get it by calling
var characterVerticalLine = characterBounds.getLineB()
Lines that must interest you in hole are 1st and 3rd (horizontal lines). So you can get them by
var holeTopLine = holeBounds.getLineA()
var holeBottomLine = holeBounds.getLineC();
Next step must be detection of their intersections.
So let’s define a function, that will do that for us and call it in our scene’s update
function.
function checkForCharacterFallDown(){
if(Phaser.Geom.Line.Intersects(characterVerticalLine, holeTopLine) ){
scene.events.emit('characterHittedTopLine');
}
if(Phaser.Geom.Line.Intersects(characterVerticalLine, holeBottomLine) ){
scene.events.emit('characterHittedBottomLine');
}
}
then call this in your update function
function update(){
checkForCharacterFallDown()
}
in your create
function set listeners for event 'characterHittedTopLine'
.
scene.events.on('characterHittedTopLine', onHittedTopLine)
lets define function onHittedTopLine
function onHittedTopLine(){
scene.events.once('characterHittedBottomLine', onHittedBottomLine);
}
lets define function onHittedBottomLine
function onHittedBottomLine{
scene.events.off('characterHittedTopLine'); // if you want stop listening this event
scene.cameras.main.stopFollow(character); // not sure that function calls that way, but you understood the idea
// and then other things that you planned to do when character falls down into hole.
}
just optimize your code to do checks only for holes that are visible in your camera and everything will be okay.