0 votes

I've been experimenting with using the _draw() function. The plan was to use said function instead of a sprite. However problems have arisen when it comes to matching the location of a rectangle I've drawn to the location of its parent node (and thus the CollisionShape2D). Even when I set them to the exact same vector2 the parent node seems to lag behind the shape. I have used a normal sprite in the below gif to represent the parent node's location (please ignore the second sprite which pops in briefly).

enter image description here

extends Area2D

var p1y = 0 #controls the y axis for this paddle
var p1x = 100
onready var main = get_node("../../Main") #main stores global variables such as gamespeed

func _ready():

func _draw():
    draw_rect(Rect2 ( self.position, Vector2(20,100) ), Color( 0, 1, 1, 1 ), true)

func _process(delta):

    if (Input.is_action_pressed("p1_up") and (p1y >= 0)): #move up
        p1y -= (100*main.getGameSpeed())*delta #move multiplied by gamespeed. We use delta to keep it consistent regardless of framerate.

    elif (Input.is_action_pressed("p1_down") and (p1y <= 500)):
        p1y += (100*main.getGameSpeed())*delta

    self.position = (Vector2(p1x, p1y))

I'm at a bit of a loss in trying to understand what's going on here. Perhaps _process and _draw work on two different systems? Any help would be greatly appreciated. Thanks in advance.

in Engine by (15 points)
edited by

Note: I edited your post to fix the code formatting. In the future, please use 4 spaces in front of code lines, or press the "Code Sample" formatting button on top of the posting window.

1 Answer

+1 vote
Best answer

_draw() positions are in the local space. To draw it at the sprite's origin:

draw_rect(Rect2 (Vector2.ZERO, Vector2(20,100) ), Color( 0, 1, 1, 1 ), true)
by (19,905 points)
selected by
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.