Continuous press on Android / Mobile

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

I am getting started in Godot and trying to write a simple game, where, when you tap the screen and hold down, the player should be running.

This works fine when I run the scene in Godot on the desktop, but it does not work when I run it on Android, and it also does not work when I set emulate_touchscreen on, on the desktop.

Below is the code for _input:

func _input(event):
  if event.type == 6 or event.type == 3: #InputEvent.SCREEN_TOUCH:
    player_running = !player_running
    if player_running:
      player.play("run")
    else:
      player.play("idle")
:bust_in_silhouette: Reply From: mydoghasworms

OK, so from looking at examples, I eventually figured out that I need to process this in _process(event), and now I have the following, which works:

func _process(delta):
  player_running = Input.is_mouse_button_pressed(BUTTON_LEFT)

It’s just not intuitive that on a mobile, the input for primary mouse button is treated the same as touch, but I’m glad that’s sorted!

:bust_in_silhouette: Reply From: eons

You can make a big invisible/transparent TouchScreenButton and use the button action to not depend on input event methods (you can use Input.is_action_ once the button set the action).

Is a strategy used with any engine.

Thanks, I was actually doing this (using a sprite that covers the screen to catch presses), but I didn’t know what event to expect, and also I found out it was better to check on _process rather than _input.

mydoghasworms | 2016-10-30 12:59

One of the properties of TouchScreenButton, is called “action”, available on the inspector too, you can use a common action or set a unique one with no key binding and make the button fire that action and get it with Input.

TouchScreenButton — Godot Engine (stable) documentation in English

eons | 2016-10-30 20:16