0 votes

Hello. I am new in godot and recently I created project , prototype for twin stick shooter for android. Both analogs woks well. One of them is responsible for movement direction and second for shooting and shooting direction. They work good when I use one of them but when I want use both touchscreen work like with one touch. I think its a problem with index of event. But I dont know how it work exacly can someone help me ?

There is a code for the first analog :

extends Sprite

const RADIUS = 64
const SMALL_RADIUS = 32

var stick_pos
var evt_index = -1
var test = -2

func _init():
    stick_pos = position

func _input(event):
    if event is InputEventScreenTouch:
        if event.is_pressed():
            if stick_pos.distance_to(event.position) < RADIUS:
                evt_index = event.index
                print(evt_index)
        elif evt_index != -1:
            if evt_index == event.index:
                evt_index = -1
                print(evt_index)
                $Small.position = Vector2()
                $"../".stick_vector = Vector2()
                #$"../".stick_angle = 0
                $"../".stick_speed = 0

    if evt_index != -1 and event is InputEventScreenDrag:
        var dist = stick_pos.distance_to(event.position)
        if dist + SMALL_RADIUS >RADIUS:
            dist = RADIUS - SMALL_RADIUS
        var vect = (event.position - stick_pos).normalized()

        var ang = event.position.angle_to_point(stick_pos)

        $"../".stick_vector = vect
        $"../".stick_angle = ang
        $"../".stick_speed = dist

        $Small.position = vect * dist

and for the second analog :

extends Sprite

const RADIUS = 64
const SMALL_RADIUS = 32

var usage = false
var stick_pos
var evt_index = -1
var test

func _init():
    stick_pos = position

func _input(event):
    if event is InputEventScreenTouch:
        if event.is_pressed():
            if stick_pos.distance_to(event.position) < RADIUS:
                evt_index = event.index
                test = evt_index
        elif evt_index != -1:
            if evt_index == event.index:
                evt_index = -1
                $Small2.position = Vector2()
                $"../".stick_vector = Vector2()
                #$"../".stick_angle = 0
                $"../".stick_speed = 0
                usage = false

    if evt_index != -1 and event is InputEventScreenDrag:
        var dist = stick_pos.distance_to(event.position)
        if dist + SMALL_RADIUS >RADIUS:
            dist = RADIUS - SMALL_RADIUS
        var vect = (event.position - stick_pos).normalized()

        var ang = event.position.angle_to_point(stick_pos)

        $"../".stick_vector = vect
        $"../".stick_angle = ang
        $"../".stick_speed = dist

        usage = true

        $Small2.position = vect * dist
in Engine by (53 points)
edited by

Did you use any particular nodes for the sticks?

Hello. Just canvas layer as a parents as UI for two srpites. One for place for analog(how much u can deflect analog) and second is smaller as an analog.

Please log in or register to answer this question.

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.