Need help with switching objects places

Im building a game that require two objects once selected by the player, to switch places. The player will select one box, the box will highlight, and when the player selects another box on the screen the two selected boxes will switch places on the screen.

Ive gotten as far as selecting the first box and second box, I just do not know how to set up an event to make the boxes switch places. And if there is a way to make them animate the switch. This is in 2D top down btw

How do you want to animate and move them? Will they cross on top of each other or avoid each other? I would use an object position tween unless you’re looking for something more complex.

Can you post a screenshot of your selection events and your scene with the objects for reference?

So the selection event I have right now is for one of the colored boxes. Im still finalizing the event to work on all the different colors. Which I should be able to figure out. I just need to store the two selected boxes somehow.

Its just the switching places I have no Idea how to even start. And as far as animating them. I had a thought of making it look like one goes below and one goes above as they swap.

This is the selection even I have so far that works with one of the colors of cube

Sounds interesting. A Pic of the scene would really help. It’s tough to know if the objects are tiles or cubes or how big of a cube and what type of movement your going for. Are all the spaces filled. Is it a grid? Do they move one space at a time. Every bit of info will help you get an answer quicker.

Each cube is 64x64. Its just a basic prototype scene. Heres a pic of it in preview mode once they are rendered. The colors are generated at random at the beginning of game

So essentially when you select two cubes on the grid. They will switch places.

Oh sorry. Swapping cubes can only be done with another cube on the same row or column, Not diagonally

Sorry, I’ve been busy. I went to modify an old example but didn’t really like it. So, I reworked it. It might need work but the concepts are there. It probably needs some testing but I think it’s good.

Try me: Click 1st to select, 2nd to move, click empty space to unselect the 1st one.

Source: (click green [code] and download zip. extract and open Json file using gdevelop)

Edit: IDK if the tiles had to be next each other that would be different checks.


No they just need to be in the same row or column. thats all. Let me try and look at the json file. Do I open it with a like a coding software?>

Oh nvm. I open with gdevelop itself. Ok let me look

It works perfectly. You’re brilliant. Thanks so much man. I really appreciate your help. This community has been amazing so far

1 Like

Hey. So I made sense of pretty much everything except one part.

Here when its tweening the position of new sprite on move1 and then in the next condition its Picking all NewSprite Objects… How is it determining which NewSprite instances to pick?
Wouldnt pick all mean it would effect all 9 instances of NewSprite? Or am I missing something

It can get confusing.
When the first object is clicked, it sets its Boolean variable selected to true
When the 2nd object gets clicked, that instance becomes picked So, any action applies to it.
To repick the 1st object, you need to sort of refresh the list, you repick them with select all
The next line picks the object that has the Boolean selected set to true.
It’s just a way to mark it so you can find it later. There are other ways. In this situation. I felt it was the easiest.

Hey Keith. I really hate to reach out to you again. You’ve been a ton of help so far.

So I attempted to take the concept from your code and convert it to my game. Its almost working except every object in the group is tweening to the locations of the two selected cubes. Here is a look at my full event. Can you spot where im going wrong?

Does it work on the 1st time but not after it. I’m not sure if you’re setting the selected boolean back to false.

I’m not at my PC, so I can’t look at my events. I don’t see the tween is finished lines. Those reset various variables once the objects are done moving. It’s setup so you can’t swap tiles while other tiles are still moving.