Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ashish |
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.