Search code examples
c#collisiongame-developmentgodotpause

Area2D only becomes invisible when clicked on the edges. (c#)


I have created a pause screen Area2D for my game, in any case the player goes AFK. I wrote a code so whenever the key P is pressed it appears and everything pauses, and if it is clicked it disappears and everything goes back to normal. I wrote a code, but it didn't work

I tried this:

    public override void _PhysicsProcess(float delta)
    {
       // Makes the pause screen visible when P is pressed
       if (Input.IsActionPressed("Pause"))
       {
         Visible = true;
       }
    }
    public override void _Ready()
    {
        
    }
    public void _on_PauseScreen_mouse_entered()
    {
        if (Input.IsActionPressed("click"))
        {
            Visible = false;
        }
    }

But it only works when clicked on the edges, I know that's how collisions are but how do I make it so when anywhere the sprite is pressed, it disappears?


Solution

  • The problem in your code should be, that you are just checking if the action is pressed, when the mouse entered the area.

    If you move the mouse to the middle of the area and click then, your event was already fired.

    Create a variable to track, if the mouse is currently in the area by using entered and exit signals and use that to determine if something should happen when you click:

    bool bMouseEntered = false
    public override void _PhysicsProcess(float delta)
    {
       // Makes the pause screen visible when P is pressed
       if (Input.IsActionPressed("Pause"))
       {
         Visible = true;
       }
       if (Input.IsActionPressed("click") && bMouseEntered)
       {
         Visible = false;
       }
    }
    public override void _Ready()
    {
        
    }
    public void _on_PauseScreen_mouse_entered()
    {
        bMouseEntered = true;
    }
    
    public void _on_PauseScreen_mouse_exited()
    {
        bMouseEntered = false;
    }