Problem with the InputEventMouseButton

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

Hello, I wrote this code … which makes clicking on any part of the scene to hide a sprite …

extends Node2D
	
func _input(event):
	if(event is InputEventMouseButton):
		if(event.pressed):
			$Sprite.hide()

This code is from another scene … and it says that clicking a button instantiates the scene…

extends Node2D


var carta = load("res://Scenas/Carta.tscn")

	
func open_scene():
	var newcarta = carta.instance()
	add_child(newcarta)
	newcarta.global_position = global_position + Vector2(600,400)

func _on_boton_pressed():
	open_scene()
    

EVERYTHING WORKS WELL … THE ONLY BAD THING IS THAT WHEN THE SCENE IS INSTANCED … THE IMAGE OF THE INSTANCE SCENE THAT SHOULD BE HIDDEN AFTER CLICKING … IS HIDDEN IMMEDIATELY … . AS IF YOU HAD BEEN CLICKED … BUT I DIDN’T TAKE THAT ACTION…

Maybe they know why that happens …

So you have a Main-scene looking like this:

- Node2D (with the second script attached to it)
  - Button (with its pressed-signal connected to _on_boton_pressed)

And a Carta-scene looking like this:

- Node2D (with the first script attached to it)
  - Sprite

Correct? And clicking the Button spawns an instance of the Carta-scene (have you made sure this really happens?) but the Sprite never becomes visible? If so, I cannot reproduce your issue, works fine. Maybe you can provide an example project?

njamster | 2020-05-02 15:01

:bust_in_silhouette: Reply From: Yamz

upgrade …
I think what happens is the scene is instantiated so fast that it also detects the click that was made to instantiate it … how can I fix that

I wouldn’t recommend you answer your own questions just for an “update”, as this decreases it’s visibility because it’s no longer marked as “unanswered”. You can edit your question by clicking on the three little dots in the bottom right corner of your question and selecting “Edit”.

njamster | 2020-05-02 15:03