Visually, My blocks aren't shifting down and I am not sure as to why.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By koehlerd

I have been creating this game and I have been trying to get my blocks to shift down in relation to an array called Location which has 4 vector spaces in it. I am not sure why the blocks aren’t shifting down and I would greatly appreciate some insight as to why this is happening.

var  locations = [Vector2(117,78),Vector2(119,185),Vector2(121,300),Vector2(114,500)]
signal blockUpdated
var blocks = []

func startBlocks():
	var blockInt1 = block.instantiate()
	var blockInt2 = block.instantiate()
	var blockInt3 = block.instantiate()
	var blockInt4 = block.instantiate()
	blockInt1.position = locations[0]
	add_child(blockInt1)
	blocks.append(blockInt1)
	blockInt2.position = locations[1]
	add_child(blockInt2)
	blocks.append(blockInt2)
	blockInt3.position = locations[2]
	add_child(blockInt3)
	blocks.append(blockInt3)
	blockInt4.position = locations[3]
	add_child(blockInt4)
	blocks.append(blockInt4)
	for i in blocks:
		var label = i.get_node("MarginContainer/VBoxContainer/Label")
		label.text = blockTypes[randf_range(0,blockTypes.size())]

func updateBlocks():
	var blockInt = block.instantiate()
	var label = blockInt.get_node("MarginContainer/VBoxContainer/Label")
	label.text = blockTypes[randf_range(0,blockTypes.size())]
	blockInt.z_index = 1
	blocks.push_front(blockInt)
	blocks.pop_back()
	add_child(blockInt)
	#var i = 0
	var bnodes = get_tree().root.get_node("/root/ControlGame").get_children()
	for k in range(0,3,1): # // loop sets new block to first positon in location
		bnodes[k].position = locations[k]
		blocks[0].position = locations[0]

func shiftBlocksDown():
	for i in range(blocks.size() - 1): #loops through each block in blocks array except for last
		var currPos = blocks[i].position 
		var nextPos = locations[i + 1]
		var diff = nextPos - currPos #differece between current and next position 
		blocks[i].position = + diff #adds difference to position to shift it down
	blocks[-1].position = locations[-1]

These functions are getting called in another script here:

func _on_texture_button_pressed():
	if !selectable:
		return
	print("click")
	selected = !selected
	if !selected && validTile:
		ControlGame.emitLock()
		selectable = false
		ControlGame.shiftBlocksDown()
		ControlGame.updateBlocks()

Here is an image as to what the game looks like and what I want to move

Edited to fix code formatting.

jgodfrey | 2023-03-16 21:32

A few things here look suspect:

func shiftBlocksDown():
    for i in range(blocks.size() - 1): #loops through each block in blocks array except for last
        var currPos = blocks[i].position 
        var nextPos = locations[i + 1]
        var diff = nextPos - currPos #differece between current and next position 
        blocks[i].position = + diff #adds difference to position to shift it down
    blocks[-1].position = locations[-1]

First, I assume this:

blocks[i].position = + diff

Should instead be:

blocks[i].position += diff

And, those -1 array element references on the next line should be generating errors I think. Not sure what you’re trying to do there…

jgodfrey | 2023-03-16 21:37