Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | AlGrande | |
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