This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes
extends Area2D

var position_draw = Vector2(200, 100)

func _ready():

    var a = Sprite.new()
    a.name = "test_sprite"
    a.texture = load("res://img/120px-Roundel_of_El_Salvador.svg.png")
    a.position = position_draw
    self.add_child(a, true)

    var coll = CollisionShape2D.new()
    coll.name = "test_collision"
    var circle = CircleShape2D.new()
    circle.radius = 200
    coll.shape = circle
    coll.position = position_draw
    self.add_child_below_node($test_sprite, coll, true)

    print(self.get_children()) #[test_sprite:[Sprite:1267], test_collision [CollisionShape2D:1271]]

    print(self.print_tree_pretty())
    #┖╴Area2D
    #    ┠╴test_sprite
    #    ┖╴test_collision
    #Null

    self.connect("input_event", self, "_on_Area2D_input_event")

func _on_Area2D_input_event(viewport, event, shape_idx):
    if InputEventMouseButton and event.is_pressed():
        print("Click")

onArea2Dinputevent - doesn't work

Godot version 3.5.1
in Engine by (17 points)

1 Answer

0 votes

I think you asked this question on another forum too? I answered it there but in case you missed it, here it is for who needs it.

you need a variable to toggle on/off to show that your sprite can be clicked. Have that variable change whenever the mouse enters, change your signal to mouse entered (and have a second signal for mouse exited to turn the var off) and whenever the mouse enters, if the bool is flipped, have the click occur. The problem with running a check on the input event is it is only running when an input occurs. You need to make a function that tracks input vs location.

So everything up to that signal is fine. Just a bit of rework to try, you may want to play with the logic to get it right.

    extends Area2D

var position_draw = Vector2(200, 100)
var _can_click = false

func _ready():

    var a = Sprite.new()
    a.name = "test_sprite"
    a.texture = load("res://img/120px-Roundel_of_El_Salvador.svg.png")
    a.position = position_draw
    self.add_child(a, true)

    var coll = CollisionShape2D.new()
    coll.name = "test_collision"
    var circle = CircleShape2D.new()
    circle.radius = 200
    coll.shape = circle
    coll.position = position_draw
    self.add_child(coll)

    self.connect("mouse_entered", self, "_on_Area2D_mouse_entered")
    self.connect("mouse_exited", selfm "_on_Area_2D_mouse_exited")


func _on_Area2D_mouse_entered():
        _can_click = true


func _on_Area2D_mouse_exited():
        _can_click = false


func _click_check():
       if _can_click:
            if Input.is_action_pressed("click"):
                  print('Click')


func _process(_delta):
       _click_check()
by (562 points)

I fixed the typo in your answer:

self.connect("mouse_exited", selfm "_on_Area_2D_mouse_exited")
self.connect("mouse_exited", self, "_on_Area_2D_mouse_exited")

But the code still does not work. As before, there are no mistakes in the console...
Signals are simply not connected.

func _on_Area2D_mouse_entered():
        _can_click = true
        print("ok")


func _on_Area2D_mouse_exited():
        _can_click = false
        print("ok")

Does not work

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.