简体   繁体   中英

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?

I want it so if it is pressed anywhere on the pause screen, it dissapears.

That is probably the simplest case. And the easier way to do it is to put the check in _physics_process , where the other check is:

    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"))
        {
            Visible = false;
        }
    }

So it has nothing to do with collision or the mouse. It is just checking for the action regardless of where or how it happens.

We can argue that using _input or _unhandled_input is better, in particular if timing is critical, but I don't expect it to be an issue in this case.


Addendum :

I want it so when it is pressed anywhere ON THE PAUSE SCREEN. it disappears. it doesn't cover the full screen.

At the time of writing Bugfish already has a working solution. I'll give you an alternative.

Since you are using an Area2D it must have the property input_pickable , when it is set to true (which is the default) it allows to "pick" the Area2D with the mouse. And you would get this interaction in the input_event signal or overwriting _input_event . Which would look like this:

void _InputEvent(Viewport viewport, InputEvent ev, int shapeIdx)
{
    var btn = ev as InputEventMouseButton;
    if(btn != null && ev.ButtonIndex == ButtonList.Left && ev.Pressed)
    {
        Visible = false;
    }
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM