How to switch two sprites?

Hi all !

I can’t switch two sprites by clicking on them …

What I will love is on a 4 x 4 grid (or an image cut into a tile), numbered from 1 to 15 and the 16th remains empty, by clicking on a number (imperatively next to the empty spirte), this number and the empty sprite exchange their positions.

I try to reproduce the “taquin game” (“jeu du taquin” in french), but I block for the exchange of positions. Do you have an idea please?

I’ll show you the screenshots of the tests I’m doing, as well as the .aia if that helps you.

Thank you !



Taquin.aia (35.4 KB)

The way I’d do it:

Create a list of lists,

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

where every number represents blocks of this type:
component_component_block

In a global variable, store the indices of the current “empty” tile (say, i=4, j=4).

When a sprite is touched, get the i (row) and j (column) indices from the list, let’s call these a and b for the time being.

Now, with a, b being indices of the touched tile and i, j being the indices of the empty tile,

if ((|a - i| = 1) and (|b - j| = 1)) {
    variable touched_tile = <touched sprite>.image
    set <touched sprite>.image to empty_tile.png
    set <previously empty tile>.image to variable touched_tile
    i = a
    j = b // keeping track of the tile that is now empty
}
1 Like

Hi and thank you for your reply !

I think I am not reproducing correctly what you explained to me, I did this:

But when I test, nothing happens, not even an error! ^^

Can you direct me please?

1 Like

This tutorial might help you

1 Like

This is a refined version of the implementation I described yesterday:


AIA:

SpriteExhangeDemo.aia (189.0 KB)


Edit: I should mention, the code is made freely available for use, but not the image assets in the demo AIA.

2 Likes

Thank you, it seems to me to work wonderfully!

I wasn’t too far from the truth about my first code, but I’m still having trouble understanding and using local variables.

Thank you !

1 Like

Awesome this tutorial! Thanks, I’ll read this and try to understand the mechanics! Thanks again !

1 Like

Obviously, I have already created tiles in Photoshop, so I will not use the images from your file, no problem!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.