2048 sound

This commit is contained in:
Jevinca Marvella 2025-11-30 16:03:06 +07:00
parent 48d2bdc1f3
commit c14ba859b8
4 changed files with 20 additions and 19 deletions

39
2048.js
View File

@ -1,4 +1,3 @@
/* ------------------------ /* ------------------------
State & Variables State & Variables
------------------------ */ ------------------------ */
@ -7,19 +6,19 @@ let currentScore = 0;
let bestScore = parseInt(localStorage.getItem('bestScore2048')) || 0; let bestScore = parseInt(localStorage.getItem('bestScore2048')) || 0;
let lastMoveDir = null; let lastMoveDir = null;
let isMoving = false; let isMoving = false;
let mergesInCurrentMove = 0; // NEW: Track combo let mergesInCurrentMove = 0;
/* ------------------------ /* ------------------------
Audio Setup Audio Setup
------------------------ */ ------------------------ */
const audio = { const audio = {
bg: new Audio("bgmusic.mp3"), bg: new Audio("Bgmusic.mp3"),
pop: new Audio("pop.mp3"), pop: new Audio("Pop.mp3"),
merge: new Audio("merge.wav") merge: new Audio("Merge.mp3")
}; };
audio.bg.volume = 0.25; audio.bg.volume = 0.25;
audio.pop.volume = 0.9; audio.pop.volume = 0.9;
audio.merge.volume = 0.9; audio.merge.volume = 1.0;
audio.bg.loop = true; audio.bg.loop = true;
function tryPlayBg() { function tryPlayBg() {
@ -192,7 +191,7 @@ function resetScore() {
} }
/* ------------------------ /* ------------------------
Add New Tile Add New Tile - FIXED: Only play pop sound here
------------------------ */ ------------------------ */
function addNewTile() { function addNewTile() {
const empty = []; const empty = [];
@ -210,6 +209,7 @@ function addNewTile() {
const tile = document.getElementById(`${spot.r}-${spot.c}`); const tile = document.getElementById(`${spot.r}-${spot.c}`);
if (tile) { if (tile) {
tile.classList.add("new"); tile.classList.add("new");
// ✅ POP SOUND: Hanya main di sini (tile baru muncul)
playSound(audio.pop); playSound(audio.pop);
setTimeout(() => tile.classList.remove("new"), 300); setTimeout(() => tile.classList.remove("new"), 300);
} }
@ -226,7 +226,7 @@ function playSound(soundObj) {
} }
/* ------------------------ /* ------------------------
Movement Logic - UPDATED WITH COMBO Movement Logic - FIXED: Merge sound plays consistently
------------------------ */ ------------------------ */
function filterZero(row) { function filterZero(row) {
return row.filter(n => n !== 0); return row.filter(n => n !== 0);
@ -236,25 +236,30 @@ function slide(row) {
row = filterZero(row); row = filterZero(row);
let mergedThisMove = false; let mergedThisMove = false;
let mergedPositions = []; let mergedPositions = [];
let mergeCount = 0; // NEW: count merges let mergeCount = 0;
for (let i = 0; i < row.length - 1; i++) { for (let i = 0; i < row.length - 1; i++) {
if (row[i] === row[i + 1]) { if (row[i] === row[i + 1]) {
row[i] = row[i] * 2; row[i] = row[i] * 2;
// ✅ MERGE SOUND & VIBRATION: Selalu main saat merge
playSound(audio.merge); playSound(audio.merge);
if (navigator.vibrate) navigator.vibrate(28);
if (navigator.vibrate) {
navigator.vibrate([80, 20, 80]);
}
currentScore += row[i]; currentScore += row[i];
row[i + 1] = 0; row[i + 1] = 0;
mergedThisMove = true; mergedThisMove = true;
mergedPositions.push(i); mergedPositions.push(i);
mergeCount++; // NEW: increment count mergeCount++;
} }
} }
row = filterZero(row); row = filterZero(row);
while (row.length < 4) row.push(0); while (row.length < 4) row.push(0);
return { row, merged: mergedThisMove, mergedPositions, mergeCount }; // NEW: return count return { row, merged: mergedThisMove, mergedPositions, mergeCount };
} }
function arraysEqual(a, b) { function arraysEqual(a, b) {
@ -265,7 +270,7 @@ function arraysEqual(a, b) {
function moveLeft() { function moveLeft() {
let moved = false; let moved = false;
let mergedCells = []; let mergedCells = [];
mergesInCurrentMove = 0; // Reset counter mergesInCurrentMove = 0;
for (let r = 0; r < 4; r++) { for (let r = 0; r < 4; r++) {
const { row: newRow, mergedPositions, mergeCount } = slide(board[r]); const { row: newRow, mergedPositions, mergeCount } = slide(board[r]);
@ -568,7 +573,7 @@ function hideGameOver() {
} }
/* ============================================= /* =============================================
COMBO EFFECT HANDLER - NEW! COMBO EFFECT HANDLER
============================================= */ ============================================= */
function triggerComboEffect(mergedCells, comboCount) { function triggerComboEffect(mergedCells, comboCount) {
if (mergedCells.length === 0) return; if (mergedCells.length === 0) return;
@ -603,7 +608,7 @@ function triggerComboEffect(mergedCells, comboCount) {
} }
/* ============================================= /* =============================================
COMBO POPUP - NEW! COMBO POPUP
============================================= */ ============================================= */
function showComboPopup(comboCount) { function showComboPopup(comboCount) {
const board = document.getElementById('board'); const board = document.getElementById('board');
@ -767,7 +772,3 @@ function getTileColor(value) {
}; };
return colors[value] || '#00eaff'; return colors[value] || '#00eaff';
} }
/* ------------------------
End of File
------------------------ */

BIN
Bgmusic.mp3 Normal file

Binary file not shown.

BIN
Merge.mp3 Normal file

Binary file not shown.

BIN
Pop.mp3 Normal file

Binary file not shown.