if (spinningRef.current) return;
spinningRef.current = true;
// Hide the payline total win container during the spin
// if (paylineTotalWinContainer) {
paylineTotalWinContainer.visible = false; // Hides the win text container
paylineWinContainer.removeChildren()
textContainerBet.removeChildren()
paylineTotalWinContainer.removeChildren();
// }
keepShowingRef.current = false; // 🔁 Stop payline cycling
textContainerBet.text = "GOOD LUCK!";
paylineWinContainer.visible = false;
if (winTextRef.current && winTextRef.current.length) {
const app = appRef.current?.app;
if (app) {
winTextRef.current.forEach(child => {
if (child.parent) {
child.parent.removeChild(child);
}
if (child.destroy) {
child.destroy();
}
});
}
winTextRef.current = [];
}
// Clear previous GIFs
gifSpritesRef.current.forEach(({ gif, original, parent }) => {
if (gif && gif.parent) {
gif.parent.removeChild(gif); // ✅ safe cleanup
}
if (original && parent && !original.parent) {
parent.addChild(original); // ✅ restore original sprite
}
});
gifSpritesRef.current = [];
// console.log(turboMode, isTurbo);
const spinSpeed = turboMode ? 50 : 30;
let stopDelays;
let column0ScrollY = 0; // Tracks how much col 0 has scrolled
let turboScrollTriggered = false; // When to start columns 1-4
const baseHeight = (itemHeight + spacingY);
const reelHeights = isTurbo ? Math.max(baseHeight * 3, baseHeight * 2.5) : baseHeight * 3;
const obj = {
amount: betAmount.text,
sessionId: sessionStorage.getItem('sessionId-Baklaha'),
};
// console.log("⚡⚡⚡ ~ spinReels ~ obj:", obj)
let matrix
let paylines
let totalwin
try {
const response = await betSpin.mutateAsync(obj);
matrix = response?.gameResult?.matrix;
userBal = response?.newBalance;
paylines = response?.gameResult?.paylines
totalwin = response?.win
// console.log("⚡⚡⚡ ~ spinReels ~ paylines:", paylines)
updateCreditsDisplay();
} catch (err) {
console.error("Error during betSpin.mutateAsync:", err);
}
if (!matrix) {
console.error("Invalid matrix from API:", matrix);
// isSpinning = false;
return;
}
const newColumns = Array(5).fill().map(() =>
Array(3).fill().map(() => Math.floor(Math.random() * items.length))
);
const ticker = app.ticker;
const doubleColumnContainers = columnContainers.map((originalContainer, index) => {
const current = new Container();
const next = new Container();
//while spinning
const setup = (container, data) => {
container.removeChildren();
for (let row = 0; row < 3; row++) {
const itemIndex = data[row];
const itemX = 150;
const itemY = row * (itemHeight + spacingY);
// Add background rectangle
const background = Sprite.from(rectangleback);
background.x = itemX;
background.y = itemY;
background.width = itemWidth - 20;
background.height = itemHeight - 35;
background.zIndex = 10
container.addChild(background);
// Add symbol sprite
const sprite = Sprite.from(items[itemIndex]);
const symbolMarginX = 1;
const symbolMarginY = 2;
sprite.x = itemX + symbolMarginX - 5;
sprite.y = itemY + symbolMarginY - 10;
sprite.width = itemWidth - symbolMarginX - 15;;
sprite.height = itemHeight - symbolMarginY - 20;
sprite.zIndex = 11;
container.addChild(sprite);
}
};
setup(current, columns[index]);
const columnData = [
matrix[0][index],
matrix[1][index],
matrix[2][index],
];
setup(next, columnData);
current.y = 0;
next.y = reelHeights;
const container = new Container();
container.x = startX + index * (itemWidth + spacingX) - 80;
container.y = startY - 3;
container.addChild(current);
container.addChild(next);
gridContainer.addChild(container); // Add new container inside masked gridContainer
gridContainer.removeChild(originalContainer); // Remove old container from masked gridContainer
originalContainer.destroy({ children: true });
columnContainers[index] = container;
return { container, current, next, index, stopped: false };
});
if (turboMode) {
stopDelays = doubleColumnContainers.map((_, index) => 20 + index * 5);
} else {
stopDelays = doubleColumnContainers.map((_, index) => 60 + index * 15);
}
// const stopDelays = doubleColumnContainers.map((_, index) => 60 + index * 15);
let frame = 0;
// Pre-scroll config
const preScrollOffset = 40;
const preScrollFrames = 10;
const preScrollDelayPerCol = 5; // frames to delay each column's movement
let preScrollCounter = 0;
ticker.add(function preScroll() {
let allDone = true;
doubleColumnContainers.forEach(({ current, next, index }) => {
const delayFrames = index * preScrollDelayPerCol;
// Start scrolling only after its delay has passed
if (preScrollCounter >= delayFrames && preScrollCounter < delayFrames + preScrollFrames) {
const offsetPerFrame = preScrollOffset / preScrollFrames;
current.y -= offsetPerFrame;
next.y -= offsetPerFrame;
allDone = false;
} else if (preScrollCounter < delayFrames + preScrollFrames) {
allDone = false; // Still waiting for some columns to finish
}
});
preScrollCounter++;
if (allDone) {
ticker.remove(preScroll);
// startInitialSpin();
ticker.add(scroll); // Begin actual spin
}
});
function createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState) {
const container = new Container();
let scatterPlaced = false;
for (let row = 0; row < 3; row++) {
let itemIndex = matrix[row][index];
const itemX = 150;
const itemY = row * (itemHeight + spacingY) + 36;
const background = Sprite.from(rectangleback);
background.x = itemX;
background.y = itemY;
background.width = itemWidth - 20;
background.height = itemHeight - 35;
background.zIndex = 10;
container.addChild(background);
const sprite = Sprite.from(items[itemIndex]);
sprite.x = itemX - 4;
sprite.y = itemY - 8;
sprite.width = itemWidth - 10;
sprite.height = itemHeight - 30;
sprite.zIndex = 11;
sprite._itemId = itemIndex;
container.addChild(sprite);
if (!doubleColumnContainers[index].finalSprites) doubleColumnContainers[index].finalSprites = [];
doubleColumnContainers[index].finalSprites[row] = sprite;
}
return container;
}
let globalScatterState = { count: 0 };
const scroll = function () {
frame++;
doubleColumnContainers.forEach((col, index) => {
if (col.stopped) return;
let currentSpeed = spinSpeed;
if (isTurbo) {
doubleColumnContainers.forEach((col, index) => {
if (col.stopped) return;
const speed = spinSpeed;
if (index === 0) {
// Column 0 scrolls continuously
col.current.y += speed;
col.next.y += speed;
column0ScrollY += speed;
if (col.current.y >= reelHeights) {
col.current.y = col.next.y - reelHeights;
}
if (col.next.y >= reelHeights) {
col.next.y = col.current.y - reelHeights;
}
if (column0ScrollY >= reelHeights && !turboScrollTriggered) {
turboScrollTriggered = true;
}
} else if (turboScrollTriggered) {
// Columns 1-4 scroll only once
col.scrollY = (col.scrollY || 0) + speed;
col.current.y += speed;
col.next.y += speed;
if (col.scrollY >= reelHeights) {
const finalContainer = createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState);
col.container.removeChildren();
col.container.addChild(finalContainer);
col.stopped = true;
}
}
});
// Stop column 0 and finish when all stopped
if (doubleColumnContainers.every((col, i) => col.stopped || i === 0)) {
const col = doubleColumnContainers[0];
col.container.removeChildren();
const finalContainer = createFinalContainer(0, matrix, items, rectangleback, scatter, globalScatterState);
col.container.addChild(finalContainer);
col.stopped = true;
// Finalize spin
ticker.remove(scroll);
setColumns(matrix);
// checkForMatches(matrix, doubleColumnContainers, items.indexOf(scatter));
// spinningRef.current = false;
textContainerBet.text = "";
// checkForMatches(newColumns, doubleColumnContainers, items.indexOf(scatter));
if (loadButtonRef.current) {
loadButtonRef.current.interactive = true;
loadButtonRef.current.buttonMode = true;
loadButtonRef.current.alpha = 1;
loadButtonRef.current.cursor = 'pointer';
}
}
} else {
// Normal mode: all columns spin until delay
col.current.y += currentSpeed;
col.next.y += currentSpeed;
if (col.current.y >= reelHeights) {
col.current.y = col.next.y - reelHeights;
}
if (col.next.y >= reelHeights) {
col.next.y = col.current.y - reelHeights;
}
if (frame >= stopDelays[index]) {
const finalContainer = createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState);
col.container.removeChildren();
col.container.addChild(finalContainer);
col.stopped = true;
}
}
});
if (doubleColumnContainers.every(col => col.stopped)) {
ticker.remove(scroll);
// console.log("⚡⚡⚡ ~ scroll ~ matrix:", matrix)
setColumns(matrix);
if (totalwin > 0) {
paylineWinContainer.text = "";
paylineWinContainer.visible = false;
checkForMatches(matrix, doubleColumnContainers, paylines, totalwin, spinningRef);
textContainerBet.text = "";
updateLastWinDisplay(totalwin, lastWinAmount);
// spinningRef.current = false;
} else {
textContainerBet.text = "PLACE YOUR BETS";
paylineWinContainer.visible = false;
updateLastWinDisplay(lastWin, lastWinAmount);
paylineWinContainer.text = "";
spinningRef.current = false;
}
// Finalize spin
// spinningRef.current = false;
if (loadButtonRef.current) {
loadButtonRef.current.interactive = true;
loadButtonRef.current.buttonMode = true;
loadButtonRef.current.alpha = 1;
loadButtonRef.current.cursor = 'pointer';
}
}
}
};
in the above code after spin completed each column arranged stopped one by one where the functionality is implemented
Looking at your spinReels function, I can see exactly where the "each column stops one by one" functionality is implemented. Let me break it down clearly:
🔍 The Core Stopping Logic (Normal Mode)
The key implementation is in the main scroll function where you check each column's stop condition:
javascript
const scroll = function () {
frame++;
doubleColumnContainers.forEach((col, index) => {
if (col.stopped) return;
// ... (scrolling logic)
// ✅ THIS IS WHERE COLUMNS STOP ONE-BY-ONE
if (frame >= stopDelays[index]) {
const finalContainer = createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState);
col.container.removeChildren();
col.container.addChild(finalContainer);
col.stopped = true;
}
});
// ... (finalization logic)
}
🧠 How It Works
Stop Delays Setup (Critical for sequential stopping):
javascript
if (turboMode) {
stopDelays = doubleColumnContainers.map((_, index) => 20 + index * 5);
} else {
stopDelays = doubleColumnContainers.map((_, index) => 60 + index * 15);
}
For normal mode: [60, 75, 90, 105, 120]
For turbo mode: [20, 25, 30, 35, 40]
Frame Counter:
let frame = 0; initialized before the ticker
Incremented every frame: frame++ in the scroll function
Per-Column Stop Check:
javascript
if (frame >= stopDelays[index]) {
// Replace spinning column with final static result
// Mark column as stopped
}
Column 0 stops when frame >= 60
Column 1 stops when frame >= 75
Column 2 stops when frame >= 90
Column 3 stops when frame >= 105
Column 4 stops when frame >= 120
⚙️ Why This Creates Sequential Stopping
Column
Stop Delay
Stops When
Effect
0
60
First (earliest frame)
Starts stopping first
1
75
15 frames later
Stops after col 0
2
90
30 frames later
Stops after col 1
3
105
45 frames later
Stops after col 2
4
120
60 frames later
Stops last
💡 Key Insight
The magic happens because:
All columns start spinning together
Each has a unique stop frame (increasing by 15 frames per column)
The frame counter (frame) is shared globally
Each column checks its own stop condition independently
This creates the classic slot machine effect where reels stop one after another from left to right.
🚫 Why Your Column 4 Isn't Stopping Last
Looking at your code, I see the issue in your normal mode implementation:
javascript
// ❌ PROBLEM: ALL COLUMNS USE THE SAME STOP LOGIC
col.current.y += currentSpeed;
col.next.y += currentSpeed;
// ... (looping logic)
if (frame >= stopDelays[index]) { // ← Column 4 uses stopDelays[4] = 120
// stops immediately when frame >= 120
}
The problem: Column 4 does stop last (at frame 120), but it stops abruptly without the "solo scroll" effect you want. It's not continuing to spin after others stop - it's just using a later stop time.
✅ How to Fix "Column 4 Spins Alone After Others Stop"
To make column 4 continue spinning after others stop (like real slot machines), you need to:
Separate the stopping logic for column 4
Keep it spinning until all others stop
Then apply its stop delay
Here's the fix (replace your normal mode section):
javascript
// In the scroll function (normal mode section)
doubleColumnContainers.forEach((col, index) => {
if (col.stopped) return;
let currentSpeed = spinSpeed;
// For columns 0-3: stop at their scheduled time
if (index < 4) {
col.current.y += currentSpeed;
col.next.y += currentSpeed;
// Loop the columns
if (col.current.y >= reelHeights) col.current.y = col.next.y - reelHeights;
if (col.next.y >= reelHeights) col.next.y = col.current.y - reelHeights;
if (frame >= stopDelays[index]) {
const finalContainer = createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState);
col.container.removeChildren();
col.container.addChild(finalContainer);
col.stopped = true;
}
}
// Special handling for column 4
else {
// Only start slowing down AFTER others stop
const othersStopped = doubleColumnContainers.slice(0, 4).every(c => c.stopped);
if (othersStopped) {
// Now apply column 4's stop delay
if (frame >= stopDelays[index]) {
const finalContainer = createFinalContainer(index, matrix, items, rectangleback, scatter, globalScatterState);
col.container.removeChildren();
col.container.addChild(finalContainer);
col.stopped = true;
}
// Keep spinning until stop time
else {
col.current.y += currentSpeed;
col.next.y += currentSpeed;
if (col.current.y >= reelHeights) col.current.y = col.next.y - reelHeights;
if (col.next.y >= reelHeights) col.next.y = col.current.y - reelHeights;
}
}
// Before others stop - keep spinning normally
else {
col.current.y += currentSpeed;
col.next.y += currentSpeed;
if (col.current.y >= reelHeights) col.current.y = col.next.y - reelHeights;
if (col.next.y >= reelHeights) col.next.y = col.current.y - reelHeights;
}
}
});
✨ Why This Works
Columns 0-3 stop at their scheduled times (60, 75, 90, 105)
Column 4 keeps spinning while others are stopping
Only when columns 0-3 are all stopped does column 4:
Start counting toward its stop time (120)
Continue spinning for 15 more frames (120-105=15)
Creates the classic "last reel suspense" effect
📌 Implementation Tip
For a more realistic effect, add a deceleration to column 4 when it's the last spinning:
javascript
// In column 4's solo section
if (othersStopped) {
// Gradually slow down as we approach stop time
const framesLeft = stopDelays[index] - frame;
const speedFactor = Math.max(0.3, framesLeft / 30); // Slows down as it approaches stop
col.current.y += currentSpeed * speedFactor;
col.next.y += currentSpeed * speedFactor;
// ... rest same
}
This creates that satisfying "slowing down" effect you see in real slot machines before the final stop! 🎰
🎰 When Each Column Stops in Your Slot Machine
Based on your code, I can tell you exactly when each column stops during the spin. Let's break it down:
🔢 Stop Timing Logic
Your stopping mechanism is controlled by the stopDelays array, which determines at which frame number each column should stop:
javascript
if (turboMode) {
stopDelays = doubleColumnContainers.map((_, index) => 20 + index * 5);
} else {
stopDelays = doubleColumnContainers.map((_, index) => 60 + index * 15);
}
It will show true for a column once frame reaches its specific stop delay
Example: When frame = 75, it will show true for column index 1 (in normal mode)
💡 The Key Insight
Your current implementation makes columns stop at predetermined frame numbers, but column 4 doesn't actually keep spinning after the others stop - it just has a later stop time.