How do I tween an image from it's placed coordinates in my scene to the corner of the screen?

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

I have a little chest that I’d like a coin to appear out of and then fly to the upper right corner.

What I’m trying:
I placed the coin on a CanvasLayer and I’m doing a tween to send it to the corner. The problem is I need to get where the chest is located and then translate that to the CanvasLayer’s components to then tween to the corner.

tween.interpolate_property(sprite, "position:x", sprite.position.x, 310, 1, Tween.EASE_IN, Tween.TRANS_LINEAR)
tween.interpolate_property(sprite, "position:y", sprite.position.y, 10, 1, Tween.EASE_IN, Tween.TRANS_LINEAR)
tween.start()

The sprite position that I’m passing into coin to start the tween is I think the global_position to the entire game. Not sure how to translate between the two.

:bust_in_silhouette: Reply From: jgodfrey

I’ve done something similar by using the get_global_transform_with_canvas() function of the CanvasItem class.

In my case, when a coin collision is detected, I fire this signal:

Events.emit_signal("collect_coin", coin.get_global_transform_with_canvas().origin)

After the signal fires, I delete the in-game coin that triggered the collision.

When the signal is received (in my case by the CanvasLayer-based HUD scene) I instance a new coin at the (converted) position passed in the signal. From there, the new coin’s start and end position are both in canvas coordinates, which is easy to handle via a simple Tween.

I’m not sure if there’s a better way, but the above worked for me.

I couldn’t find a better way, or any way for that matter. The trick was to use the get_global_transform_with_canvas origin. I didn’t know this exsisted and still don’t know what the difference is between the different positions but this definitely worked. Thanks! I had actually given up on making “collections” in my game this way so glad I could add it back in :slight_smile:

rballonline | 2020-11-17 01:03