0 votes

I am making a game where a KinematicBody2D (which has a Sprite in the shape of a paint brush) moves to the new mouse click position. With this click, a Line2D also adds a new point and a line is drawn to the new position.
All this is working, however there is a problem:
Before, I move the KinematicBody2D to the new click position, I have a mouse click on a TouchScreenButton (out of many) to pick up a colour for the Line2D trail. The problem is this press also moves the KinematicBody2D. This is an undesired result. In other words, the press on the TouchScreenButton inserts the colour as well as moves the KinematicBody2D. The press should only collect the colour for the Line2D(and change the value of a flag).
Some part of the code execution must not happen when the click is on a TouchScreenButton. Anywhere else, the click is fine as it moves the KinematicBody2D as required.
I have any an idea that call-deferred may solve the problem. But I am not able to write the correct code for execution of the TouchScreenButton thread only.
Please help.

Here is my code:

extends KinematicBody2D

onready var line = get_node("../line")
var speed = 100
var velocity = Vector2(0,0)
var adjust =Vector2(0, 0)
var old_position
var colour =[false, false, false, false, false, false, false, false, false,false, false, false,false]
var value = Color(0,0,0,0)
var new_position = Vector2(0,0)

func _ready():
    connect("input_event",self,'get_input')


func get_input():
    velocity = Vector2()
    old_position = position

    if Input.is_action_pressed("click"):

        new_position = get_local_mouse_position()
        velocity = new_position

    velocity = velocity.normalized() * speed

func _physics_process(delta):
    get_input()
    if velocity != adjust:

        move_and_slide(velocity)
        line.set_default_color(value)
        var wide = 5.0
        line.width = wide

        line.add_point(old_position)
        line.add_point(position)

        old_position = new_position



func _on_Red1_pressed():
    colour[0]=true
    value = Color(1,0,0,1)

Please notice that in the _ready function, I tried to connect a signal for a call-deferred call. However, I am not able to insert code or function correctly. Maybe, I need a different approach altogether.

Godot version 3.2.2
in Engine by (100 points)

1 Answer

+1 vote

A non-ellegant solution could be check with signals if mouse is over touchscreenbutton:

var not_click =false

func on_TouchScreenButton_mouse_entered():
 not_click =true

func on_TouchScreenButton_mouse_exited()
 not_click =false

And in your input function check if your mouse is out of the button(s)

if Input.is_action_pressed("click") and !not_click:
rest of your code

Notice that I've written signals by mind, maybe there are spelling mistakes

by (129 points)

Elegant or not, this works.
Thanks a lot.
No Godot inbuilt signal of onTouchScreenButtonmouseentered((), so I have used onTouchScreenButton_pressed()

Thanks again.

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 Frequently asked questions and 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 [email protected] with your username.