I am very new to Godot and I have been semi-following a youtube tutorial to create a candycrush-like game. However, I ran into some issues when trying to animate the moving pieces using tweens. The pieces are added as child nodes in the grid and when I drag the selected_piece
into the area of another piece, the other piece will emit a signal that is received by grid
to execute the swap_pieces
function. However, when the swap_pieces
function executes and calls the move
function on the piece, the tween simply did not play. I have been looking at this for hours and am really stumped, hoping it is a simple mistake that i couldn't notice. Any help is appreciated. I am using Godot4.3 if that is any useful information.
Grid.gd
func swap_pieces(piece):
var tmpgridpos = selected_piece.grid_position
selected_piece.grid_position = piece.grid_position
piece.grid_position = tmpgridpos
# piece.position = tmpgridpos * 64
print("moving from " + str(piece.position) + " to " + str(tmpgridpos*64))
piece.move(tmpgridpos * 64)
board[selected_piece.grid_position.y][selected_piece.grid_position.x] = selected_piece
board[piece.grid_position.y][piece.grid_position.x] = piece
Piece.gd
extends Node2D
@export var color: String
@export var grid_position: Vector2i
var is_dragging
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
SignalBus.dragging_start.connect(_on_dragging_start)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func pickup() -> void:
$Sprite2D.set_scale(Vector2(1.2,1.2))
func place() -> void:
$Sprite2D.set_scale(Vector2(1,1))
func _on_area_2d_mouse_entered() -> void:
SignalBus.mouse_in_my_area.emit(self)
func _on_dragging_start() -> void:
is_dragging = true
func move(target):
print("trying to move to " + str(target))
var tween: Tween = create_tween()
tween.tween_property(self,"position",target, 1)
tween.set_trans(Tween.TRANS_ELASTIC)
tween.set_ease(Tween.EASE_OUT)