How to make an Area2D node to switch between two images each time I click on it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jaboc1234

I use this code below for this action. I tried to use a Boolean to show the button has been pressed but it just makes both the if and the elif true. My understanding was that with an if/elif statement, if the if is true, it ignores the rest but that doesn’t seem to be the case. Help me :slight_smile:

:bust_in_silhouette: Reply From: rossunger

It’s probably reacting to a different input event (such as InputEventMouseMotion and executing immediately after.
You should move the whole code block into the “if event i InputEventMouseButton:” block.

  if event is InputEventMouseButton && event.is_pressed():
       the rest of your code here

Also, you should store the textures in a variable and use preload instead of load… otherwise you’re loading the texture each time you click.

var YellowVialTexture = preload ("res://......" )
:bust_in_silhouette: Reply From: skysphr

The if / elif construct will indeed only execute one of the variants depending on the condition, however your function might be triggered more often than you would expect. Depending on how you wire the function, it might respond to mouse releases or mouse motion, which you might detect as instantaneous. Try putting the if event is InputEventMouseButton and if event.is_pressed() conditions outside of the filled check.