How to fix my buggy card moving code

This code works fine, it expands when hovering over with the mouse, it also works fine when clicking and dragging the card. The problem comes with the playing and returning card function. This function will print “play” when the card is drag and released in the top 70% of the screen, which works, the problem is that when you play in the bottom 30% it will return like it should but after that when you click anywhere on the screen the card will move to where ever I click and then I am dragging it if I am holding click otherwise it will move back. This is what I want help with fixing. One thing I am not sure why, is that when I added a print(card_is_being_held) to delta it flips from being true/false every _delta.

Also if this why of doing this is not the greatest then feel free to suggest better ways, right now A problem with this one is that if you click on another card while another card is in front of another you pick up and drag both at the same time. Another issue is if you hover one card over the other

extends Node2D
var is_hover_over_card = false
var card_is_being_held = false
var card_initial_position: Vector2

func hover_over_expand():
	if not $template_image.is_pixel_opaque(get_local_mouse_position()) and is_hover_over_card == true:
		is_hover_over_card = false 
		$template_image.apply_scale(Vector2(0.8,0.8))
	if $template_image.is_pixel_opaque(get_local_mouse_position()) and is_hover_over_card == false:
		is_hover_over_card = true
		$template_image.apply_scale(Vector2(1.25,1.25))
	
func drag_card():
	if Input.is_action_just_pressed("Click") and $template_image.is_pixel_opaque(get_local_mouse_position()):
		card_is_being_held = true 
		card_initial_position = get_global_mouse_position()
		
	if Input.is_action_pressed("Click") and card_is_being_held == true:
			Globals.can_camera_move = false
			var tween = get_tree().create_tween()
			tween.tween_property($".", "position", get_global_mouse_position(), 0.05) 
	if Input.is_action_just_released("Click"):
		card_is_being_held = false

func on_tween_finished():
	Globals.cards_can_be_clicked = true

func play_and_return_card():
	if card_is_being_held == true and Input.is_action_just_released("Click"):
		if get_viewport().get_mouse_position().y / get_viewport().size.y <= 0.7:
			print("play")
		else:
			Globals.cards_can_be_clicked = false
			var tween2 = get_tree().create_tween()
			tween2.tween_property($".", "position", card_initial_position, 0.5)
			tween2.connect("finished", on_tween_finished)

func _process(_delta):
	play_and_return_card()
	hover_over_expand()
	if Globals.cards_can_be_clicked == true:
		drag_card()
       print(card_is_being_held)

NM I found much better way to do this with resources and classes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.