0 votes

I'm trying to create an animated button that has 3 states for a mobile app.

The states are Resting, Short-press, Long-press.

What node objects should I be using to experiment with a button that does the following:
In resting state it's just a handle (like on a toilet), When pressed, I want a timer to watch the press for how long it's being pressed. For a short press, the animation switched to "Jiggle", then on long-press it switches to "Flush"

Is TextureButton the right object to be using? or should I be using Button and figure out the animation switching on my own?

Godot version 3.2.3 stable
in Engine by (33 points)

The button you use is irrelevant you can use the following code on gui_input(event) or _input(event)

extends Node

signal long_press

const LONG_PRESS_DELAY = 0.5

var long_press_timer = Timer.new()

func _on_long_pressed():
    #Do long press stuff and ignore single press stuff

func _ready():
    var err = long_press_timer.connect("timeout", self, "_on_long_pressed")
    if err: print("Error could not connect long_press_timer")

func _input(event):
    if event is InputEventScreenTouch:
        if event.is_pressed():
            #When a touch event happens start timer to check for long press
            if long_press_timer.is_stopped(): long_press_timer.start(LONG_PRESS_DELAY)
            long_press_timer.stop() #Cancel long press on input released
            #Do single press stuff

    elif event is InputEventScreenDrag:
        long_press_timer.stop() #Cancel long press if moving

I don't see how this works.

The player must touch the screen where the sprite is. How do you capture the touch event associated with the sprite and then operate on it. The problem isn't how to code the long touch, the problem is how do you capture the event on a sprite!

Control from which TextureButton is derived has a Signal called gui_input(event)
connect that to a function and only when and input event happens while the pointer is over that TextureButton will the code i've provided before be executed.

enter image description here

P.s replacing func _input(event): with the function name of the connected signal of gui_input(event)

Thank you very much for following up! I learned something from this discourse.

Was your issue resolved or did i miss the mark? Because there is another way to achieve what you desire using the toggle_mode property of the TextureButton

I have a solution that works, you can read my answer below.

While it may have solved my problem, I like hearing the right way to do things so that it's in my arsenal of code thought when I work on the next problem.

1 Answer

0 votes

I used the following tree

I made the button transparent by using the inspector->visibility->modulate and set the A channel to 0 making it transparent. This allowed the button to receive the signals. I think made the button the same size as my sprite and put them on top of one another. Using the timer I was able to animate the sprite when I needed to, depending on the timing of signals from the button

by (33 points)
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.