For the assignment I created a new KinematicBody2D scene ("Bumpers) and recreated the necessary nodes (Sprite, CollisionShape2D, Area2D (StompDetector), CollisionShape2D ) minus VisibilityEnabler2D under it. After that I added a bumper layer in Project Settings and set the StompDetector to the "bumper" layer, mask set to "player". Then in the player scene I connected area_entered( Area2D area) to the StompDetecter and copied the code from the _on_EnemyDetector_area_entered function. And I played it and it worked like in the solution video.
Then following the video I made changes to the game:
- deleted player scene's _on_EnemyDetector_area_entered function from both the script and signals and replaced it with _on_StompDetector_area_entered linked via signals
- changed the enemy's StompDetector layer to "bumper"
When I played the game, the enemy was gone. Then I changed its layer back to "enemies" and it was back. I tinkered with the settings and stared at the codes, but couldn't figure out what went wrong. If the enemy's StompDetector's sole function is to detect whether it's being stomped, how come changing the layer makes it unable to being drawn on the screen?
It's hard for me to know exactly from the text, but when you play the game, you have a Remote tab that appears in the scene tree. Could you look there and see if the enemy exists in the runtime tree? If not, it means that there's a physics layer and mask combination somewhere that makes your enemy queue_free, that is to say it gets removed from the game.
Right on the spot, Nathan. The enemy does not exist in the runtime tree's Remote tab.
The enemy appears when I set the layer to "enemies" or no layer at all. But once I change it to "bumper" it just refuse to show up at all. Let's see if I can figure this out...
Well I went back to the Creating the Stomp Area video and moved the StompDetector above the enemy's collision shape (was at the bottom), move the CollisionShape2D back down, and voila, the enemy's back when I played the game.
I don't understand that particular part of the video, why is the StompDetector Area2D's location matter here that it interferes with the enemy's showing up or not?
why is the StompDetector Area2D's location matter here that it interferes with the enemy's showing up or not?
It shouldn't, there's either a check missing in the code to not destroy the enemy, or a combination of physics layer and mask that's not set correctly, causing the enemy to queue_free. For instance, if the bump detector detects the enemy's collision box, it will send the signal that it was jumped on and the enemy will queue_free.
Yes, I think you're right, the problem seems to be the combination of layer and mask. It's still not intuitive to me, but I think maybe when I'm more fluent with the engine I'll get the hang of it. Thanks!
Yes, it takes some time to wrap your head around it. Here's some more explanation that can hopefully help a little bit. I also recorded a video guide for it some time ago: https://youtu.be/UZu8NwlkXcU
A physics object is on a given physics layer. Being on a given layer, it can be detected.
The word mask is a little misleading because it controls which physics layers a given node will detect and interact with.
Let's say I have two layers, characters, and world.
I assign my floor and houses to the world layer. I set its physics mask to nothing - all layers off.
I assign the main character to the characters layer. I set its physics mask to detect the world layer.
As a result, the main character will collide with everything on the world layer. But the houses and the floor won't detect the main character, saving some processing power.
It's much clearer now, thanks Nathan!