+4 votes

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
in Engine by (64 points)

1 Answer

+9 votes

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.

by (140 points)

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.

Hello @AIGrande, could you help me with this "https://godotengine.org/qa/7574/making-a-virtual-analog-stick-for-android" ?

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!!! :D Need help! :)

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

THANK YOU VERY MUCH!

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 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 webmaster@godotengine.org with your username.

Categories