0 votes

I have created a Card scene with area2d as parent and collision shape and sprite as child and a lobby scene where the card scene gets instanced through code and the card gets data from an external JSON file, how should I go about duplicating the selected card with complete variable data and same texture as a sibling node.

extends Area2D

export (int) var id
export (Image) var cardFace
export (Image) var cardBack

onready var cardD = get_node("Sprite")
onready var move_tween = get_node("Tween")
var selected = false
var is_mouse_over = false
var oldPos 
var restPoint
var restNodes = []


func _ready():
    cardD.texture = cardFace
    oldPos = global_position
    restNodes = get_tree().get_nodes_in_group("zone")
    restPoint  = restNodes[0].global_position
    restNodes[0].select()
    pass

func _on_Area2D_input_event(_viewport, event, _shape_idx):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT:
            selected = true
            oldPos = global_position


func _process(delta):
    if selected:
        self.modulate = Color("#FFFF00")
        global_position = lerp(global_position, get_global_mouse_position(), 25 * delta)
    else:
        self.modulate = Color(1,1,1,1)
#       global_position = lerp(global_position, oldPos, 15 * delta)


func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and not event.pressed:
            selected = false
            var shortestDist = 75
            for child in restNodes:
                var distance = global_position.distance_to(child.global_position)
                if distance < shortestDist:
                    child.select()
                    restPoint = child.global_position
                    shortestDist = distance
    if event is InputEventScreenTouch:
        if event.is_action_released("ui_touch"):
            selected = false


func move(target):
    move_tween.interpolate_property(self, "position", position, target, .4, Tween.EASE_OUT)
    move_tween.start()

func _on_Area2D_mouse_entered():
    is_mouse_over = true

func _on_Area2D_mouse_exited():
    is_mouse_over = false

func _unhandled_input(event):
    if is_mouse_over:
        if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
            selected = event.pressed
            get_tree().set_input_as_handled()
        if event is InputEventScreenTouch and event.is_action_pressed("ui_touch"):
            selected = event.pressed
            get_tree().set_input_as_handled()

Card scene`s script looks like so

in Projects by (39 points)

Please log in or register to answer this question.

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.

Categories