The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I have about 100 sprites on screen, and I want to touch/click them individually, and I need to create them only using script, not with the editor.

I've tried using






Here is my code:

    var click_dummy =
    click_dummy.color = Color(0,1,0,0.2)
    click_dummy.rect_size = Vector2(50,50)
    click_dummy.rect_position = Vector2(pt[0]-25,pt[1]-25)
    card_node.add_child(click_dummy) = 'click'+str(card_id)+'_'+str(i)

    var scale = 0.7 * rng.randf_range(0.75, 1.0)
    var angle = rng.randf_range(0,180)

    var spr =
    spr.scale = Vector2(scale,scale)
    spr.position = Vector2(pt[0],pt[1])
    spr.texture = load("res://jfsdlfhsdjo")

Here is the touchscript script:

extends ColorRect
#extends Sprite
#extends Control
#extends TouchScreenButton

func _ready():
func _process(delta):

#func _input_event(event):
func _input(event):
    if event is InputEventScreenTouch or event is InputEventMouseButton:
#        if event.is_pressed():

Clicking/touching fires the event on all the sprites I have, while I just want to fire the event on the one I touch/click.

Godot version 3.3.2
in Engine by (12 points)
edited by

1 Answer

0 votes

Can you show how you have your input and event code setup?

by (810 points)

I've edited my question.

Make your area2d/sprite a separate empty scene and add the event script to it. You can then check the name of what was clicked.

extends Area2D

func _on_Area2D_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
    if (event is InputEventScreenTouch or event is InputEventMouseButton) and event.is_pressed():
        pass # Replace with function body.

on your main app, load the empty scene and then customize accordingly.

extends Node2D

var rng =

func _on_Button_pressed() -> void:

    var scene = load("res://Testing/SpritesTest/ScriptsTest.tscn")
    var click_dummy = scene.instance()
    var i=rng.randi_range(1,1000)
    var j=rng.randi_range(1,1000) = 'click'+str(i,j)
    click_dummy.position=Vector2(i % 500,j % 500)
    print(click_dummy.position," ",    
    pass # Replace with function body.
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.