Sort objects by tap sequence

I need to develop the logic of a filter that has numbers from 0 to 1 on the screen, you have to choose 2 of them. The first number that is touched goes to X position and the second goes to another position, which in this case is next to the first one. Is it possible to get the information of which was played first?

This is a quick and dirty script for how you could get the order of tapped objects. This doesn’t do any smart checks (like if the same object is tapped twice) and also doesn’t do any resetting, but you could add that functionality on top if this is close to what you’re looking for.

// How to load in modules
import Scene from 'Scene';
import Diagnostics from 'Diagnostics';
import Reactive from 'Reactive';
import TouchGestures from 'TouchGestures';

var numberObjects; //array to hold all 'number' scene objects
var tapCount = 0; //how many times any number object has been tapped
var chosenIndices = [0, 0]; // array to hold the indexes of which objects were tapped


(async function () {  // Enables async/await in JS [part 1]

    numberObjects = await Promise.all([
        Scene.root.findFirst("number0"),
        Scene.root.findFirst("number1"),
        Scene.root.findFirst("number2"),
        Scene.root.findFirst("number3")
    ]);

    //run for loop for every 'number' object and setup callback for when any object is tapped
    for(let i = 0; i < numberObjects.length; ++i)
    {
        let currentObj = numberObjects[i];
        TouchGestures.onTap(currentObj).subscribe(() => handleObjectTap(currentObj)); //when currentObj is tapped, it will run the callback handleObjectTap
    }

})(); // Enables async/await in JS [part 2]

function handleObjectTap(tappedObj)
{
    if(tapCount < 2) //only handle the tap if two objects haven't been tapped already
    {
        //for loop to check which object was tapped
        for(let i = 0; i < numberObjects.length; ++i)
        {
            if(tappedObj === numberObjects[i])
            {
                chosenIndices[tapCount] = i;
                Diagnostics.log(chosenIndices[tapCount]);
                
                //increase tap count
                if(tapCount < 2)
                {
                    tapCount += 1;
                }
                
                return;
            }
        }
    }
    
}

3 Likes

Thanks brother, I got it but I thought the screen was too polluted. I found a way to do it differently. Thanks!!

1 Like