Hello I come posted to you because I have a problem with my code, before it worked well with a Zorder problem but it is fixed only, my current problem is that I got confused with my backups, I started a game again and now the mix does not respect my waiting time before starting, nor the duration of the mix, it starts directly and never stops… if a great javascript coder could help me, thank you in advance, It’s too big for a picture so here, sorry…and i speak french but I made a translation for you ![]()
runtimeScene.setBackgroundColor(0, 0, 0);
// Get the scene variable “pieces”
let piecesVar = runtimeScene.getVariables().get(“pieces”);
// Convert the scene variable “pieces” to a JavaScript array
let pieces = JSON.parse(piecesVar.getAsString());
// Create and initialize p1 to p16 only once if they have not been created yet
if (pieces.length === 0) {
for (let i = 1; i <= 16; i++) {
let pieceName = “p” + i;
let piece = runtimeScene.createObject(pieceName);
piece.setZOrder(1); // All sprites have the same initial ZOrder
pieces.push(pieceName); // Add the object name to the scene
}
// Update the scene variable "pieces" with the new array
piecesVar.setString(JSON.stringify(pieces));
}
// Initialize the puzzle array
let tableau = [
[pieces[0], pieces[4], pieces[8], pieces[12]],
[pieces[1], pieces[5], pieces[9], pieces[13]],
[pieces[2], pieces[6], pieces[10], pieces[14]],
[pieces[3], pieces[7], pieces[11], pieces[15]]
];
// Set the initial positions of the sprites
for (let i = 0; i < tableau.length; i++) {
for (let j = 0; j < tableau[i].length; j++) {
let sprite = runtimeScene.getObjects(tableau[i][j])[0];
if (sprite) {
sprite.setX(j * 100 + 160); // Adjust the X position
sprite.setY(i * 100 + 380); // Adjust the Y position
sprite.setWidth(100); // Set the sprite width
sprite.setHeight(100); // Set the sprite height
}
}
}
// Function to shuffle an array using the Fisher-Yates algorithm
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i–) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
}
}
// Create a list of all possible positions
let positions = ;
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
positions.push({ x: j * 100 + 160, y: i * 100 + 380 });
}
}
// Shuffle the list of positions
shuffleArray(positions);
// Assign each position to a piece
for (let i = 0; i < pieces.length; i++) {
let sprite = runtimeScene.getObjects(pieces[i])[0];
if (sprite) {
sprite.setX(positions[i].x);
sprite.setY(positions[i].y);
}
}
// Initialize the counter for the shuffle
let shuffleCounter = 0;
// Initialize the start of the shuffle
let shuffleStart = false;
// Function to shuffle the sprites
runtimeScene.shuffleSprites = function() {
// Check if the shuffle still needs to take place
if (shuffleStart && shuffleCounter < 800) { // Increase this value to extend the shuffle
// Increase the shuffle counter
shuffleCounter++;
// Only shuffle the pieces every 10 calls to the function
if (shuffleCounter % 10 === 0) {
console.log('Shuffle function called');
// Choose a random sprite
let spriteName = pieces[Math.floor(Math.random() * pieces.length)];
let sprite = runtimeScene.getObjects(spriteName)[0];
// Check if sprite is defined
if (sprite) {
// Choose a random position for the sprite
let posX = Math.floor(Math.random() * 4) * 100 + 160;
let posY = Math.floor(Math.random() * 4) * 100 + 380;
// Move the sprite to the new position
sprite.setX(posX);
sprite.setY(posY);
}
}
} else if (shuffleCounter >= 800) {
console.log('Shuffle finished');
// Stop the shuffle
runtimeScene.getVariables().get("shuffleInProgress").setNumber(0);
}
}
// Create a new event in GDevelop
runtimeScene.getOnceTriggers().triggerOnce(“StartMix”, true, function() {
// Set shuffleInProgress to true
runtimeScene.getVariables().get(“shuffleInProgress”).setNumber(1);
// Set shuffleStart to true to start the shuffle
shuffleStart = true;
// Call shuffleSprites every 50 milliseconds
runtimeScene.shuffleSprites();
});