Godev
Godev

Reputation: 1

my questions is about code in GDscript (language programing in godot engine)

sprite (attack) is not working in case :

if Input.is_action_just_pressed("ui_attack"):

   $Sprite.play("attack")

but in case:

if Input.is_action_pressed("ui_attack"):

$Sprite.play("attack")

it's working !!!!

what is the solution code pleas?!! Because I want one click on the keyboard to work of sprite...

Upvotes: 0

Views: 411

Answers (1)

Christopher Bennett
Christopher Bennett

Reputation: 801

Have you tried using if event.is_action_pressed("ui_attack"): instead? I've found that event works better than Input if I only want one action after a button press and not multiple.

If it still doesn't work, try adding if event.is_action_released("ui_attack"): afterwards. You may have to put an additional command afterward to make the code acceptable to the engine, such as print("Key Released"). In my case, it never ended up printing anything to console, but it did only give me one action per button press.

I am assuming this is the question you were looking for an answer to. If not, please disregard, I have been awake all night and am prone to confusion.

EDIT: I forgot to add that it must be in a func _input(event) function to work properly. I've included a simple example that prints text to the console once per press of the left button. See below:

extends Node2D

var text = PrintText()
func PrintText():
    return("Print Text Once")

func _ready():
    pass

func _input(event):
    if event.is_action_pressed("ui_left"):
    print(text)

(P.S. I only discovered Godot in the last week or so, but have been crash-coursing myself ever since. I love the engine, but if my understanding comes off as "basic", forgive me.)

Upvotes: 0

Related Questions