I have a KinematicBody2D name Actor at the root and attched script is Actor.gd. It has a Sprite child with hframe = 3 & vframe = 3 which I want to have size of 128x128.
My texture is any square shape image e.g. 512x512. So, I want to scale this texture to 384x384 and set a random 128x128 to the Sprite's frame.
I want to detect if the Sprite was clicked using below script as mentioned in the docs.
func _input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT:
if get_rect().has_point(to_local(event.position)):
print("A click!")
But even if I click nearby the sprite, it is detecting the click. I printed the variables value and I got below:
scale_factor = (0.3, 0.3)
original texture size = 1280x1280
target texture size = 384x384
Sprite.get_rect() = (-213, -213, 426, 426)
If we see the get_rect() output, as per my understanding, it should be (-64, -64, 128, 128) but here the Sprite itself is not getting scaled I think and that's why the click detector is not working. However, when I pass original image of 384x384, then the get_rect() prints (-64, -64, 128, 128) and detector works fine. Can someone please help fix the issue? Thanks.
#Actor.gd
extends KinematicBody2D
func new_scale(texture):
var sizeto=Vector2(384,384)
var size=texture.get_size()
var scale_factor=sizeto/size
return scale_factor
func _on_ready():
var puzzle_texture = load(im_path)
var scale_factor = new_scale(puzzle_texture)
$Sprite.texture = puzzle_texture
$Sprite.frame = randi()%9
$Sprite.set_scale(scale_factor)
print("Sprite rect: ", $Sprite.get_rect())