Code edit (1 edits merged)
Please save this source code
Code edit (14 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'chunks[globalIndex].play();' Line Number: 52
User prompt
move chunks creation in a separate function then call it after 3s
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'play')' in or related to this line: 'chunks[globalIndex].play();' Line Number: 51
Code edit (1 edits merged)
Please save this source code
User prompt
choice1 is 118464ms long; create a global array 'chunks' that inits 119 sounds (`LK.init.sound`)
/**** * Classes ****/ //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Define a class for the PianoTile var PianoTile = Container.expand(function () { var self = Container.call(this); // Create and attach a black rectangle as the tile var tileGraphics = self.attachAsset('tile', { anchorX: 0.5, anchorY: 0.5 }); // Set the speed of the tile self.speed = globalSpeed; self.isTapped = false; // Update function to move the tile downwards self.update = function () { self.y += self.speed; // Check if tile is off-screen if (self.y > 2732) { // End game if a tile is missed if (self.isTapped) { if (self.y > 2732 + self.height) { self.destroy(); } return; } LK.showGameOver(); return; } }; // Handle tap events self.down = function (x, y, obj) { // Increase score and destroy the tile LK.setScore(LK.getScore() + 1); self.isTapped = true; self.alpha = 0.1; if (playTimeout) { LK.clearTimeout(playTimeout); } if (chunks.length > 0) { chunks[globalIndex].play(); } if (globalIndex < chunks.length - 1) { globalIndex++; } /* // Play choice1 sound at game launch choice1.play(); playTimeout = LK.setTimeout(function () { choice1.stop(); }, 1000); */ }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x4169e1 //Init game with black background }); /**** * Game Code ****/ function createChunks() { var chunks = []; var chunkLength = 118464 / 119; for (var i = 0; i < 119; i++) { var start = i * chunkLength / 118464; var end = (i + 1) * chunkLength / 118464; var soundChunk = LK.getSound('choice1', { start: start, end: end }); chunks.push(soundChunk); } return chunks; } var chunks = []; LK.setTimeout(function () { chunks = createChunks(); }, 3000); // Initialize global speed variable var globalSpeed = 18; // 10 / 12 / 15 / 18 / 20 / 25 var globalTickRate = 900 / globalSpeed; var globalIndex = 0; var perfectY = 1800; // Initialize global variable for choice1 var choice1 = LK.getSound('choice1'); var playTimeout; // Initialize score display var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create column assets to separate lanes var column1 = LK.getAsset('column', { anchorX: 0.5, anchorY: 0.5, x: 1024 - 400 - 3, y: 2732 / 2 }); game.addChild(column1); var column2 = LK.getAsset('column', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 2732 / 2 }); game.addChild(column2); var column3 = LK.getAsset('column', { anchorX: 0.5, anchorY: 0.5, x: 1024 + 400 + 3, y: 2732 / 2 }); game.addChild(column3); var perfectLine1 = LK.getAsset('perfectLine', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5, x: 1024, y: perfectY - 450 }); game.addChild(perfectLine1); var perfectLine2 = LK.getAsset('perfectLine', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5, x: 1024, y: perfectY + 450 }); game.addChild(perfectLine2); // Array to keep track of active tiles var tiles = []; // Function to create a new tile var lastLane = 0; function createTile() { var newTile = new PianoTile(); // Randomly position the tile in one of the two lanes newTile.x = lastLane = lastLane == 1024 - 200 - 3 ? 1024 + 200 + 3 : 1024 - 200 - 3; newTile.y = -500; // Start above the screen tiles.push(newTile); game.addChild(newTile); } // Game update function game.update = function () { // Update score display scoreTxt.setText(LK.getScore()); // speed 10 => every 90 ticks // speed 15 => every 60 ticks if (LK.ticks % globalTickRate == 0) { createTile(); } };
===================================================================
--- original.js
+++ change.js
@@ -38,10 +38,14 @@
self.alpha = 0.1;
if (playTimeout) {
LK.clearTimeout(playTimeout);
}
- chunks[globalIndex].play();
- globalIndex++;
+ if (chunks.length > 0) {
+ chunks[globalIndex].play();
+ }
+ if (globalIndex < chunks.length - 1) {
+ globalIndex++;
+ }
/*
// Play choice1 sound at game launch
choice1.play();
playTimeout = LK.setTimeout(function () {
@@ -81,9 +85,9 @@
}, 3000);
// Initialize global speed variable
var globalSpeed = 18; // 10 / 12 / 15 / 18 / 20 / 25
var globalTickRate = 900 / globalSpeed;
-var globalIndex = 2;
+var globalIndex = 0;
var perfectY = 1800;
// Initialize global variable for choice1
var choice1 = LK.getSound('choice1');
var playTimeout;