Hi there,
I have an issue where I have a grid with a bunch of blocks that are children of that grid, so their position is set relative to that boards parent. I have a cursor that can move around the board, hover over blocks and switch their position. There are two issues with this right now:
1) Only the first row of blocks has their position set properly when the game loads, all the other rows after that have their block position set to ( 0, 0 )
2) When switching blocks, the block that ends up on the left side of the cursor ends up with its position set to ( 0, 0 ) as well
I didn't have this issue before and I can't think of anything that I changed that would cause weird behavior like this. Does anyone have an idea?
Here is the code for initializing the blocks and swapping them:
extends Node2D
const tile_size = 16
const BlockResourceB = preload("res://Blocks/BlockB.tscn")
const BlockResourceG = preload("res://Blocks/BlockG.tscn")
const BlockResourceR = preload("res://Blocks/BlockR.tscn")
const BlockResourceY = preload("res://Blocks/BlockY.tscn")
const Cursor = preload("res://Cursor/Cursor.tscn")
onready var main = get_node("TileBoard")
var array = [BlockResourceB, BlockResourceG, BlockResourceR, BlockResourceY]
func _ready():
for i in range(4,12):
for ii in range(6):
var type = array[randi() % array.size()]
var block = type.instance()
main.add_child(block)
block.position = Vector2(ii*16, i*16)
var cursor = Cursor.instance()
main.add_child(cursor)
extends Area2D
const tile_size = 16
func _unhandled_input(event):
if event.is_action_pressed("ui_right"):
<...>
if event.is_action_pressed("swap"):
var left = get_node("DetectionLeft").get_overlapping_areas()
var right = get_node("DetectionRight").get_overlapping_areas()
swapBlocks(left, right)
func swapBlocks(left,right):
if left.size() > 0 && right.size() > 0:
var temp = left[0].position
left[0].position = right[0].position
right[0].position = temp
elif left.size() > 0 && right.size() == 0:
left[0].position += Vector2.RIGHT * tile_size
elif right.size() > 0:
right[0].position += Vector2.LEFT * tile_size