Check if Sprite(self) is clicked

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AlGrande
:warning: Old Version Published before Godot 3 was released.

If this Sprite it pressed it should move. How do I do this?
Currently it always moves if you click anywhere. This is my current code:

Sprite Class:

extends Sprite

var input_states = preload("res://scripts/input_states.gd")

var click = input_states.new("click")
onready var Hero = get_tree().get_root().get_node("/root/Node2D/Hero")
onready var sprite = get_tree().get_root().get_node("/root/Node2D/Hero/Sprite")

func _ready():
	# Called every time the node is added to the scene.
	# Initialization here
	set_fixed_process(true)


func _fixed_process(delta):
	if(click.check() == 2):

		
		print("Clicked!")
		Hero.set_dir("LEFT")
		sprite.set_texture(Hero.left)
		Hero.move(Vector2(-Hero.speed,0.0)*Hero.delta)

Input_states class:

### class for input handling. Returns 4 button states
var input_name
var prev_state
var current_state
var input


var output_state
var state_old

### Get the input name and store it
func _init(var input_name):
	self.input_name = input_name
	
### check the input and compare it with previous states
func check():
	input = Input.is_action_pressed(self.input_name)
	prev_state = current_state
	current_state = input
	
	state_old = output_state
	
	if not prev_state and not current_state:
		output_state = 0 ### Released
	if not prev_state and current_state:
		output_state = 1 ### Just Pressed
	if prev_state and current_state:
		output_state = 2 ### Pressed
	if prev_state and not current_state:
		output_state = 3 ### Just Released
	
	return output_state
:bust_in_silhouette: Reply From: Beamer159

The best way to determine if a sprite is clicked is to actually use a different node, Area2D. You will use 3 nodes: a parent Area2D node with 2 child nodes, your sprite node and a CollisionShape2D node. It should be structured like this. For the CollisionShape2D, you will have to give it a Shape2D object. I usually use a RectangleShape2D. Then, change the Shape2D object’s extents to make it as big as you need, usually to match the size of your sprite (Note that your extent values should be half of your sprite’s dimensions. e.g. if your sprite is 32x32, make your extents 16x16).

As for code, add a script only to Area2D and use the input_event(…) function. The code will look something like this:

extends Area2D

func _input_event(viewport, event, shape_idx):
    if event.type == InputEvent.MOUSE_BUTTON \
    and event.button_index == BUTTON_LEFT \
    and event.pressed:
        print("Clicked")

This should get you a clickable sprite. Now, if your sprite could possibly overlap other sprites that do something when clicked, that gets a little more complicated (and interesting!). Let me know if you will need to handle this case.

Thanks, but it won’t work. Also the sprites overlap. I’m trying to make a virtual d-pad so the game is playable on android. It currently looks like this: enter image description here

This is the script I added to the Area2D:

 extends Area2D

var input_states = preload("res://scripts/input_states.gd")
var click = input_states.new("click")
onready var Hero = get_tree().get_root().get_node("/root/Node2D/Hero")
onready var sprite = get_tree().get_root().get_node("/root/Node2D/Hero/Sprite")

func _ready():
	# Called every time the node is added to the scene.
	# Initialization here
	#set_fixed_process(true)
	#self.get_global_mouse_pos()
	print("")


func _input_event(viewport, event, shape_idx):
    if event.type == InputEvent.MOUSE_BUTTON \
    and event.button_index == BUTTON_LEFT \
    and event.pressed:
        print("Clicked")

				Hero.set_dir("LEFT")
				sprite.set_texture(Hero.left)
				Hero.move(Vector2(-Hero.speed,0.0)*Hero.delta)

Also the code after the if should be executed if the button is pressed, not if the button is only clicked.

AlGrande | 2016-05-10 10:26

Hello @AIGrande, could you help me with this “Making a virtual analog stick for android - Archive - Godot Forum” ?

maurodias | 2016-08-31 10:49

Now, if your sprite could possibly overlap other sprites that do something when clicked, that gets a little more complicated (and interesting!). Let me know if you will need to handle this case.

I’m letting you know!!! :smiley: Need help! :slight_smile:

DaceKonn | 2018-02-01 00:16

If you still need help, post a question and I will answer it.

Beamer159 | 2018-02-15 19:40

THANK YOU VERY MUCH!

0xabhishek | 2020-05-13 12:47

Now, if your sprite could possibly overlap other sprites that do something when clicked, that gets a little more complicated (and interesting!). Let me know if you will need to handle this case.

Hello! I need your help too! ) *letting you know *

Dayls | 2021-07-15 10:48

:bust_in_silhouette: Reply From: angstyloop