Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | picnic |
I have a Node2d ‘BG’ with a single Node2d child ‘Boss’ with a script attached to each. When the sprite attached to ‘Boss’ is moved, I’d like to draw a line from (0,0) to the sprite but my code doesn’t work. Can someone tell me what’s wrong?
Here’s BG.gd:
extends Node2D
func _draw():
var from = get_node("Boss").get_position()
var to = Vector2(0, 0)
print(from)
draw_line(from, to, Color(0, 0, 128, 1) , 2, true)
func _update():
CanvasItem.update()
and here’s Boss.gd:
extends Node2D
var screensize
const SPEED = 100
func _ready():
screensize = get_viewport_rect().size
func _process(delta):
if Input.is_action_pressed('ui_right'):
position.x += SPEED * delta
elif Input.is_action_pressed('ui_left'):
position.x -= SPEED * delta
position.x = clamp(position.x, 0, screensize.x)
Even though the Boss sprite moves as expected, the line is drawn from (0,0) to the original position and is not updated.