Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Fupicat |
I’m sorry for the title. I’m going insane over this.
I’m making a drag and drop thing for my game. The scene I have for the draggable object is organized like this:
This is what it looks like:
The script I have for the Drag and Drop Node2D has one export variant, that asks for a file path of a texture file. This variant is supposed to update the Sprite node’s texture with the variant’s texture file and change the CollisionShape2D’s extents based on the image file’s width and height divided by another export variable.
By itself, the scene works perfectly.
(The image’s borders are farther away from the drawing itself)
Now, when I instance this Drag and Drop scene into another scene, if I add only one, nothing changes. Now if I instance another one, the code that sets the collision extends breaks. The sprite changes but the collider’s shape for both is the same.
I discovered that the order of the instances in the hierarchy matters. The instance that’s lower on the scene tree is the one that affects the shape of every other instance’s collider.
This is the code the Drag and Drop scene uses. The only part that is breaking is inside the _ready() function:
extends Node2D
var mouse_on = false
var dragging = false
export (String) var spriteRes
export var reduzir = 2
# The only part that is breaking is inside the _ready() function
func _ready():
var theSprite = load(spriteRes)
$Sprite.set_texture(theSprite)
var spriteSize = Vector2($Sprite.texture.get_width() / reduzir, $Sprite.texture.get_height() / reduzir)
$Area2D/CollisionShape2D.shape.set_extents(spriteSize)
func _process(delta):
if dragging:
position = get_global_mouse_position()
func _input(event):
if event.is_action_pressed("mouse_left") and mouse_on:
$Timer.start()
$AnimationPlayer.play("Shake")
play_sound("Rubber Quick - Kbnevel")
if event.is_action_released("mouse_left") and dragging:
$Timer.stop()
dragging = false
$AnimationPlayer.play("Drop")
play_sound("Bed Hit")
func _on_Area2D_mouse_entered():
mouse_on = true
func _on_Area2D_mouse_exited():
mouse_on = false
$Timer.stop()
func _on_Timer_timeout():
dragging = true
$AnimationPlayer.play("Pop")
play_sound("Plunger Quick - Lloydevans09")
func play_sound(var nome):
var arquivo = "res://Sons/" + nome + ".wav"
if File.new().file_exists(arquivo):
var sfx = load(arquivo)
$AudioStreamPlayer.stop()
$AudioStreamPlayer.stream = sfx
$AudioStreamPlayer.play()
And here’s the project files if you want to mess around:
https://drive.google.com/open?id=1v0SKIZM-3Cd8LR3ydn9nWfgKReXW6Z1D
Please help.
Thanks.