Tween to Last Position

Question?

How to tween to last/original position when an object is dropped?

What is the expected result?

When dropped, a playing card should return/tween from its dropped position back to the starting hand.

What is the actual result?

Currently, the code tweens dropped cards to a known x,y coordinates (see attached code); this is the best I can currently do. It would be much better, however, to tween to the last position of the card before it was dropped (ie tween card back to starting hand).

Thank you for your help!

There is a drag behavior with a cancel feature.

https://wiki.gdevelop.io/gdevelop5/extensions/cancellable-draggable/

Otherwise, you could use object variables like LastX and LastY. At some point either at the beginning or after a deal or when dragging with a trigger once you would set the object variables to the objects X and Y. Then when needed, you could tween the object to the object variables LastX and LastY.

Hi Keith, thank you for the quick response. In regards to my “card drawing” game:

  1. I tried “Cancel Drag”; that didn’t work to tween the card back to starting position.

  2. If I add “LastX” and “LastY” object variables to each card, how can the the varying LastX and LastY coordinates of each drawn card be auto-detected and transferred to a variable, so the object/card can be tweened to LastX, LastY?

  3. Why isn’t there an existing tween that snaps back to LastX, LastY? This seems like it would be the most needed tween of all.

Thank you!

This uses sprites named Tile and Zone. Tile has the basic drag and tween behaviors.

Tile object variable (the initial values don’t matter)

Events (this will set the LastX and LastY value for all Tile objects)

The other empty squares are just there for the look.
ScreenRecorderProject22

You saved the day, thank you! However, in my situation, P1 keeps drawing cards throughout the game, so the position of the drawn card changes throughout the game depending on how many cards are in hand. So I need a “Zone” that changes position in accordance with the new drawn cards position in the hand and tweens the card to that modified Tile x,y coordinate.

1 Like

In my situation, I want the Zone box (ie card draw position) to offset position from other cards in hand in an array depending on how many cards are in hand.

Thank you!

Can you post a screenshot of the scene? I’m having trouble imagining what you’re trying to do. You can update the variables when needed.

Thank you for your help. The cards are currently drawn at the bottom of the screen in a manual 3x3 array (ie tween cards to predetermined x,y coordinates). Next step is the cards will no longer be drawn into a manual array, but instead be drawn into an automatic, interactive array that changes/morphs from a centered 3X1 to a 3X2 to a 3X3 array, based on the number of cards in hand, similar to PvZ Heroes app. Depending on the phase of the game, the player may have anywhere from 1 to 9 cards in hand. I don’t want random gaps between cards or new drawn cards overlapping other cards in hand, therefore, some kind of offset array must eventually be coded in lieu of Tween to x,y to keep the cards evenly separated from each other in an interactive array so they are evenly spaced and not overlapping as cards are drawn 1 at a time throughout the game. Therefore, the tween must be able to sense the new position of the newly drawn offset array card so it knows where to tween the card back to the hand when necessary. If I use the same object variable “LastX, LastY” for each of the 40 cards in the deck, I’m not sure how to code it to keep track of a newly drawn card that could be in any 1 of 9 possible starting positions in the array, depending on how many cards are already in the hand. If a Tween Last X,Y tween existed, it would reduce my lines of code by 5X and just make sense; I can’t believe Tween Last X,Y doesn’t already exist; maybe I can sell a Tween Last X,Y extension in the Asset Store someday if I get better at programming.

The code you already provided me is so close, but it just needs to account for a moving array instead of a fixed position. Maybe you can sell this extension in the Asset Store after explaining it to me :slight_smile:

Thank you

I’m still not sure what your goal is but if you have 6 spaces, I’ll call placeholders then you can save the x,y when the object is dragged and then tween it back under whatever circumstances you choose.

Tile has the drag, tween behavior and LastX and LastY variables.
Placeholder has the Boolean variable IsEmpty and it is set to True for the boxes with objects on them.
Group is a group list with the tile objects inside it.

try me: You can drag between Placeholders and drop it in the Target object, anywhere else then it goes back. I added a textbox to display the value of the placeholder IsEmpty variable for testing.
https://gd.games/instant-builds/8d476bdd-2004-44de-b4ce-921e17eaeba3

IDK if this is the best way, I had to add a Boolean scene variable because if the 1st action was triggered, the other actions could also trigger and undo the move.
I decided to combine the last 2 events because it was getting too repetitive. My mistake is I was setting the placeholder as not empty for the current placeholder instead of the targeted one. So, it picks the targeted placeholder and sets it as not empty. I manually set the placeholder IsEmpty to false for the objects that weren’t empty. I also added a layer called “DragLayer” because I don’t like it when the dragging objects are hidden behind other objects. IDKW this isn’t built into the drag behavior.

Last update: I moved the trigger one up in the drag event because it was triggering multiple times when being dragged as the collision went from true to false.

Just in case:
Organizing objects into groups for events

There’s still a bug but I don’t have the time to fix it and it might not be an issue because IDK if my example matches your needs. The bug is it remembers the last position but it doesn’t check if an object has been moved to it while it is on the target object. Again, my example might not match your needs, so this might not be an issue,

I decided to try a different approach. Instead of saving the x,y it goes through the placeholders and chooses the first empty object. I added the scene variable Placed to track if an empty placeholder is found.

try me:
https://gd.games/instant-builds/a1015707-06bb-4a82-8a32-f1000dfb366b

What was the issue? The extension is used in this example:

Thank you for all your help so far.

Over the last couple days, I tried the 2nd method you suggested: 1) Add StartX and StartY object variables to each card
2) Change the variable of StartX and StartY of Deck_Cards: set to Deck_Cards.X(), Deck_Cards.Y()
3) Tween the position of Deck_Cards to x: Deck_Cards.StartX,
y: Deck_Cards.StartY with easing “linear” over 0.4 seconds

Unfortunately this tweens the card to 2 positions previous and not the last position. I had to revert back to my previous prototype that manually tweens to the last known position using “tween to position x,y”

I am now going to try the 3rd method you provided and will let you know in a couple days if this worked for me.

Thank you!

Hi Davy,

The Klondike Solitaire Playing Card extension doesn’t work for me, as Klondike tweens back to hand based off a fixed position (ie a box) that it tweens the card back to. In my case, I’m working with an 80 card array, so the “box” position would need to vary depending on how many cards are in hand when the card is drawn.

Also, the extension I need would need to account for 80 objects (ie cards) that each have their own animations; Klondike uses only 1 object and changes animations for that 1 object. I need to have 80+ objects tweening in an array; Klondike uses 1 object tweening to a fixed position.

Why is there no tween or extension that tweens the card back to it’s last position, even if the position varies??? Yes tween to position x,y works, but only if the position is known ahead of time. There really should be a TweenBack to last position added to the Tween extension, or a new ReturnCard extension developed, as this is probably the most needed type of action for card games.

Thank you!

HI Keith,

The “Placeholder” in your code is in a fixed position; however, I need “Placeholder” to be in a varying position to work with an array. A Tweenback to last position extension would solve all of this. Keith, can you please design and sell this TweenBack extension in the G-Develop asset store, and I and others will buy it and be very grateful?!

Thank you!

This is what the “Cancellable draggable object” extension already does.
https://wiki.gdevelop.io/gdevelop5/extensions/cancellable-draggable/

Hi Davy,

What is the G-Develop line of code used to cancellable draggable object?

Thank you!

image

example

Hi Keith,
I just found there is a “Cancellable/draggable” behavior that can be added to each object, so I added this behavior to each card, but “Cancel last dragging of Deck_Cards with easing “linear” in 0.5 seconds” code still doesn’t work and the card doesn’t tween/snap back to it’s original position, it just sits there when dropped.

Did it tween back to the original position for you with the last “Cancel last dragging of Deck_Cards with easing “linear” in 0.5 seconds” code you provided, because it’s not working for me?

Thank you for your help!

1 Like

The last image is too small. I can’t read the text. That being said, if the objects are in a group list then you wouldn’t need so many events.

Hi Keith,

Thank you I will learn how group lists work.

Did you get your “Cancel Draggable object” code to actually tween an object back to your hand?

This code works with “tween the position” and tweens card back to hand just fine.

This code with “cancel last dragging” doesn’t work and the card just sits there when dropped and it doesn’t tween back to starting position.

Thank you!