Code edit (1 edits merged)
Please save this source code
User prompt
when a hextile is toggled, make it not clickable, until it is toggled again
User prompt
add a new validation to not allow the player to toggle a hextile back after it touches it for the first time. it will only be toggled back after the second hextile is touched
User prompt
add soft blue as random backround color
User prompt
on game start use startscreenbackground as background image over the current background color. however hide starscrennbackgournd after first touch
User prompt
show startscreenbackground insted of background color, unntil the first touch is done in the screen
User prompt
If hextile is selected, it cant be unselected until another hextile is selected
Code edit (3 edits merged)
Please save this source code
User prompt
on hextile class, if there has not been any touch in the screen set numbertext.visible = false, if not set it to true.
Code edit (1 edits merged)
Please save this source code
User prompt
On first screen touch please update hextile numbertext to visible true.
User prompt
Please fix the bug: 'ReferenceError: hexTile is not defined' in or related to this line: 'hexTile.numberText.visible = true;' Line Number: 99
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: numberText is not defined' in or related to this line: 'numberText.visible = true;' Line Number: 99
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < hexTiles.length; i++) {' Line Number: 91
User prompt
On game start set alpha of hextile number to 0, but set it back to 1 once the game starts
Code edit (1 edits merged)
Please save this source code
User prompt
When level is updated show and make visible the numbers in the hexes
User prompt
hex numbers should be set to visible when level 2 and above start
User prompt
on level update show hex numbers on start
User prompt
if player touches on the same tile twice, without touching another tile, do nothing, do not toggle that same tile
User prompt
Payer should not be able to toggle on a tile they have toggled of manually.
User prompt
Once the first hex from the pais is selected, it can be unselected until the second one is chosen.
User prompt
While countdown is not 0, show tile numbers
/**** * Classes ****/ // HexTile class for the hexagon tiles var HexTile = Container.expand(function (id, colorIndex) { var self = Container.call(this); self.id = id; var hexGraphics = self.attachAsset('hexTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); var numberText = new Text2('', { size: 100, fill: '#333333' // Slightly lighter black }); numberText.anchor.set(0.5, 0.5); numberText.visible = playerTouchedScreen; self.addChild(numberText); self.setNumber = function (number) { self.number = number; numberText.setText(number.toString()); }; self.isSelected = false; self.toggleSelect = function () { if (!self.isSelected || selectedTiles.length < 2) { self.isSelected = !self.isSelected; hexGraphics.alpha = self.isSelected ? 0.5 : 1; if (self.isSelected) { numberText.setText(self.number.toString()); } else { numberText.setText(''); } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: [0xADD8E6, 0xFAD7A5, 0xFFB6C1, 0xFFA07A, 0xB39BDB, 0x90EE90][Math.floor(Math.random() * 6)] // Randomly choose from soft light blue, soft light orange, soft pink, soft light red, soft light yellow, soft light green }); /**** * Game Code ****/ var gameTitle = new Text2('Memo-Hex', { size: 200, fill: '#ffffff', alpha: 1 }); gameTitle.anchor.set(0.5, 0); gameTitle.x = 0; // Center horizontally gameTitle.y = 100; // Move down 100 pixels LK.gui.top.addChild(gameTitle); // Blinking 'Touch to start' text var touchToStartText = new Text2('Touch to start', { size: 100, fill: '#ffffff', alpha: 1 }); touchToStartText.anchor.set(0.5, 0); touchToStartText.x = 0; // Center horizontally touchToStartText.y = gameTitle.y + gameTitle.height + 80; // Position below the game title LK.gui.top.addChild(touchToStartText); // Blinking effect var blink = true; LK.setInterval(function () { blink = !blink; touchToStartText.alpha = blink ? 1 : 0; }, 500); var howToPlayText = new Text2('How to Play: \n• Memorize the hex numbers on countdown \n• Wait for goal number to appear above hex tiles \n• Touch two hexes that add up to the number \n• Repeat until all combinations possible are found', { size: 60, fill: '#ffffff', alpha: 1 }); howToPlayText.anchor.set(0.5, 0.5); howToPlayText.x = 0; howToPlayText.y = 1500; LK.gui.top.addChild(howToPlayText); // Modify game's 'down' event listener to hide howToPlayText after first touch var originalGameDownHandler = game._events && game._events['down'] ? game._events['down'][0] : function () {}; function newGameDownHandler(obj) { howToPlayText.alpha = 0; originalGameDownHandler(obj); // Make level text visible on first touch levelText.alpha = 1; levelText.visible = true; countdownText.visible = true; touchToStartText.visible = false; gameTitle.visible = false; // Make hexTile numbers visible on first touch for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].getChildAt(1).visible = true; } } game.off('down', originalGameDownHandler); game.on('down', newGameDownHandler); game.on('down', function (obj) { playerTouchedScreen = true; // Make life icons visible on first touch for (var i = 0; i < lifeIcons.length; i++) { lifeIcons[i].alpha = 1; } }); var playerTouchedScreen = false; function resetGameForNextLevel() { // Reset hexTiles numbers for (var i = 0; i < hexTiles.length; i++) { // Number generation and assignment removed from here to be handled inside initializeHexTiles function. } // Reset goal var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; while (randomTile1 === randomTile2) { randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; } goalNumber = randomTile1.number + randomTile2.number; goalText.setText(goalNumber); // Reset combinations correctCombinations = 0; for (var i = 0; i < hexTiles.length; i++) { for (var j = i + 1; j < hexTiles.length; j++) { if (hexTiles[i].number + hexTiles[j].number === goalNumber) { correctCombinations++; } } } combinationsText.setText(correctGuess + '/' + correctCombinations); } var level = 1; var levelText = new Text2('Level ' + level, { size: 75, fill: '#ffffff', alpha: 0 // Make level text invisible initially }); levelText.anchor.set(1, 0); // Hide level text on game start levelText.visible = false; LK.gui.topRight.addChild(levelText); correctGuess = 0; var correctGuess = 0; var lives = 3; var lifeIcons = []; for (var i = 0; i < lives; i++) { var lifeIcon = LK.getAsset('lifeIcon', { anchorX: 1, anchorY: 0, x: -i * 90, y: 100, alpha: 0 // Make life icons invisible initially }); LK.gui.topRight.addChild(lifeIcon); lifeIcons.push(lifeIcon); } var countdown = 15; if (hexTiles) { for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].interactive = false; } } var countdownText = new Text2(countdown, { size: 300, fill: '#ffffff', alpha: 0 // Make invisible initially }); countdownText.visible = false; countdownText.anchor.set(0.5, 1); LK.gui.top.addChild(countdownText); var countdownInterval = LK.setInterval(function () { if (playerTouchedScreen) { countdown--; if (countdown <= 0) { for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].interactive = true; } countdownText.alpha = 0; goalText.visible = true; combinationsText.visible = true; LK.clearInterval(countdownInterval); for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].getChildAt(1).setText(''); } } else { countdownText.setText(countdown); } } }, 1000); countdownText.y += 550; //var scoreText = new Text2('Score: 0', { // size: 75, // fill: '#ffffff' //}); //scoreText.anchor.set(0.5, 0); //LK.gui.top.addChild(scoreText); // Create goal number and display it below the score if (hexTiles && hexTiles.length > 1) { sumOfNeighbours = hexTiles[hexTiles.length - 2].number + hexTiles[hexTiles.length - 1].number; } // sumText initialization moved to after hexTiles initialization var sumOfNeighbours = 0; var hexTiles = []; var selectedTiles = []; var guessedPairs = {}; var gridRows = []; function initializeHexTiles() { function initializeHexTiles() { // Define gridRows based on the current game level var gridRows; if (level <= 2) { gridRows = [2, 3, 2]; } else if (level === 3) { gridRows = [3, 4, 3]; } else if (level === 4) { gridRows = [4, 5, 4]; } else { // Default to the highest defined grid for levels beyond those explicitly defined gridRows = [3, 4, 5, 4, 3]; } var tileSpacingX = 250; var tileSpacingY = 210; var startX = (2048 - Math.max.apply(Math, gridRows) * tileSpacingX) / 2 + tileSpacingX / 2; var startY = (2732 - gridRows.length * tileSpacingY) / 2 + tileSpacingY / 2; // Initialize hex tiles for (var i = 0; i < gridRows.length; i++) { for (var j = 0; j < gridRows[i]; j++) { var hexTile = new HexTile(i * gridRows.length + j); var numberRange; if (level <= 2) { numberRange = 3; } else if (level <= 5) { numberRange = 9; } else { numberRange = 12; } hexTile.setNumber(Math.floor(Math.random() * numberRange) + 1); hexTile.x = startX + j * tileSpacingX + (Math.max.apply(Math, gridRows) - gridRows[i]) * tileSpacingX / 2; hexTile.y = startY + i * tileSpacingY; game.addChild(hexTile); hexTiles.push(hexTile); (function (hexTile) { hexTile.on('down', function (obj) { if (countdown > 0) { return; } if (selectedTiles.length < 2) { hexTile.toggleSelect(); if (hexTile.isSelected) { selectedTiles.push(hexTile); if (selectedTiles.length == 2) { if (selectedTiles[0].number + selectedTiles[1].number === goalNumber) { var pairId = [selectedTiles[0].id, selectedTiles[1].id].sort().join('-'); if (!guessedPairs[pairId]) { LK.setScore(LK.getScore() + 1); // Removed undefined reference to scoreText // scoreText.setText('Score: ' + LK.getScore()); guessedPairs[pairId] = true; correctGuess++; combinationsText.setText(correctGuess + '/' + correctCombinations); } LK.setTimeout(function () { if (selectedTiles.length > 0) { selectedTiles[0].toggleSelect(); } if (selectedTiles.length > 1) { selectedTiles[1].toggleSelect(); } selectedTiles = []; }, 300); if (correctGuess === correctCombinations) { level++; correctGuess = 0; guessedPairs = {}; combinationsText.setText(correctGuess + '/' + correctCombinations); levelText.setText('Level ' + level); countdown = 15; goalText.visible = false; combinationsText.visible = false; countdownText.alpha = 1; countdownText.setText(countdown); LK.clearInterval(countdownInterval); countdownInterval = LK.setInterval(function () { countdown--; if (countdown <= 0) { countdownText.alpha = 0; goalText.visible = true; combinationsText.visible = true; LK.clearInterval(countdownInterval); for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].getChildAt(1).setText(''); } } else { countdownText.setText(countdown); } }, 1000); LK.effects.flashScreen(0x90EE90, 500); // Remove previous hex tiles for (var i = 0; i < hexTiles.length; i++) { hexTiles[i].destroy(); } hexTiles = []; initializeHexTiles(); // Update goal and guesses with new numbers on the new grid // Ensure a different random background color is chosen when the level changes, different from the previous one. var previousBackgroundColor = game.backgroundColor; var newColors = [0xADD8E6, 0xFAD7A5, 0xFFB6C1, 0xFFA07A, 0xB39BDB, 0x90EE90].filter(function (color) { return color !== previousBackgroundColor; }); game.setBackgroundColor(newColors[Math.floor(Math.random() * newColors.length)]); var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; while (randomTile1 === randomTile2) { randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; } goalNumber = randomTile1.number + randomTile2.number; goalText.setText(goalNumber); // Reset combinations correctCombinations = 0; for (var i = 0; i < hexTiles.length; i++) { for (var j = i + 1; j < hexTiles.length; j++) { if (hexTiles[i].number + hexTiles[j].number === goalNumber) { correctCombinations++; } } } combinationsText.setText(correctGuess + '/' + correctCombinations); } } else { lives--; lifeIcons[lives].destroy(); if (lives <= 0) { LK.showGameOver(); } else { LK.effects.flashScreen(0xFFA07A, 500); } LK.setTimeout(function () { if (selectedTiles.length > 0) { selectedTiles[0].toggleSelect(); } if (selectedTiles.length > 1) { selectedTiles[1].toggleSelect(); } selectedTiles = []; }, 300); } } } else { var index = selectedTiles.indexOf(hexTile); if (index > -1) { selectedTiles.splice(index, 1); } } } }); })(hexTile); } } } initializeHexTiles(); } initializeHexTiles(); var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; while (randomTile1 === randomTile2) { randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)]; } var goalNumber = randomTile1.number + randomTile2.number; var goalText = new Text2(goalNumber, { size: 300, fill: '#ffffff', alpha: 0 // Make invisible initially }); goalText.visible = false; goalText.anchor.set(0.5, 1); goalText.y += 550; LK.gui.top.addChild(goalText); // Calculate the number of unique correct combinations var correctCombinations = 0; for (var i = 0; i < hexTiles.length; i++) { for (var j = i + 1; j < hexTiles.length; j++) { if (hexTiles[i].number + hexTiles[j].number === goalNumber) { correctCombinations++; } } } // Display the number of unique correct combinations below the goal var combinationsText = new Text2(correctGuess + '/' + correctCombinations, { size: 200, fill: '#ffffff', alpha: 1 // Make visible }); combinationsText.visible = false; combinationsText.anchor.set(0.5, 0); combinationsText.y += 1400; LK.gui.top.addChild(combinationsText); // Game tick function LK.on('tick', function () { // Game logic that needs to be executed every frame });
===================================================================
--- original.js
+++ change.js
@@ -23,12 +23,16 @@
numberText.setText(number.toString());
};
self.isSelected = false;
self.toggleSelect = function () {
- if (!self.isSelected) {
- self.isSelected = true;
- hexGraphics.alpha = 0.5;
- numberText.setText(self.number.toString());
+ if (!self.isSelected || selectedTiles.length < 2) {
+ self.isSelected = !self.isSelected;
+ hexGraphics.alpha = self.isSelected ? 0.5 : 1;
+ if (self.isSelected) {
+ numberText.setText(self.number.toString());
+ } else {
+ numberText.setText('');
+ }
}
};
});