How to randomize custom Drag Source positions On Load

Hello Team,
I am creating a custom drag-and-drop interaction in ActivePresenter without using the default quiz block. I chose this approach because I need the draggable objects to hide completely as soon as they are dropped onto the target, regardless of whether the answer is correct or incorrect.
In this regard, I would like to know if it is possible to randomize the Drag Source objects when the slide starts, simulating the native behavior of standard “Drag-and-Drop” questions.
I look forward to your reply. Thank you!

ActivePresenter version: 10.6.2
Operating system: macOS Sequoia 15.7.7

Hi Betty,

You can use a standard Drag-n-Drop question for this. Configure the target to accept all Drag Sources, then add a Hide Object action for each corresponding source in the Accept List. This hides the source immediately when dropped, regardless of whether it is correct or incorrect. You can then enable Shuffle Answers to randomize the sources.

Alternatively, if you prefer your custom interaction, try adding the following Execute JavaScript action to the slide’s On Load event:

(function () {
    // Replace these with the names of your Drag Source objects.
    var names = ["Drag_1", "Drag_2", "Drag_3", "Drag_4"];

    var sources = names.map(function (name) {
        var obj = prez.object(name);
        if (!obj) {
            throw new Error("Object not found: " + name);
        }
        return obj;
    });

    var positions = sources.map(function (obj) {
        return { x: obj.left(), y: obj.top() };
    });

    for (var i = positions.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = positions[i];
        positions[i] = positions[j];
        positions[j] = temp;
    }

    sources.forEach(function (obj, index) {
        obj.left(positions[index].x);
        obj.top(positions[index].y);
    });
})();

Best regards,
Hoa

1 Like

Hi Hoa, I’ve tried using this code, but if you move the objects without dragging them into the drop zone, they behave strangely.
Anyway, thank you so much for your help. :folded_hands: