[SOLVED] How to move node to node?

How you make gui node move toward another gui node?

If you look video, green spawned node must move toward green node on topright corner. Position are somehow wrong.

script used:

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		local screen = vmath.vector3(action.x, action.y, 0)
		local prefab = gui.get_node("prefab")
		local clone = gui.clone(prefab)
		gui.set_enabled(clone, true)
		gui.set_position(clone, screen)

		local target_position = gui.get_screen_position(gui.get_node("icon"))
		gui.animate(clone, "position", target_position, gui.EASING_LINEAR, 0.5, 0, function()
			gui.delete_node(clone)
		end)
	end
end

Is there gui animate screen position? because if you try place at exact position that works.

gui.set_screen_position(clone, target_position)

mrp:

node to node.zip (66.9 KB)

1 Like

You could create an empty node with the same parent as the clone, use gui.set_screen_position() to set its position to the target, then get the empty node’s new position with gui.get_position(), and use that target position for gui.animate().

3 Likes

tbanks, parenting works.

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		local screen = vmath.vector3(action.x, action.y, 0)
		local prefab = gui.get_node("prefab")
		local clone = gui.clone(prefab)
		gui.set_enabled(clone, true)
		gui.set_position(clone, screen)
        -- parenting
		local panel = gui.get_node("panel")
		gui.set_parent(clone, panel, true)

		local target_position = gui.get_position(gui.get_node("icon"))
		gui.animate(clone, "position", target_position, gui.EASING_LINEAR, 0.5, 0, function()
			gui.delete_node(clone)
		end)
	end
end

1 Like

Could you not just use
local target_position = gui.get_position(gui.get_node(“icon”))

In your first version? (Instead of get_screen_position)

Not if they have different parents. The position of a node is relative to its parent.