Code edit (6 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'button')' in this line: 'if (obj.event.data.button === 0) {' Line Number: 79
User prompt
Fix Bug: 'ReferenceError: createBlueBlock is not defined' in this line: 'createBlueBlock(pos.x, pos.y);' Line Number: 171
User prompt
create floor using 1 on mause position
User prompt
add floor
User prompt
fix save into one key
User prompt
log for game saved and loaded
User prompt
fix save and load
User prompt
Fix Bug: 'ReferenceError: loadButton is not defined' in this line: 'if (!saveButton.getBounds().contains(pos.x, pos.y) && !loadButton.getBounds().contains(pos.x, pos.y)) {' Line Number: 139
User prompt
remove load button
Code edit (1 edits merged)
Please save this source code
User prompt
set reset button pos to right angle
User prompt
set reset button pos to 40 40
===================================================================
--- original.js
+++ change.js
@@ -26,24 +26,27 @@
self.saveGame();
});
self.saveGame = function () {
if (typeof LK.localStorage !== 'undefined') {
- LK.localStorage.setItem('blueBlocks', JSON.stringify(game.children.filter(function (child) {
- return child instanceof BlueBlock;
- }).map(function (block) {
- return {
- x: block.x,
- y: block.y
- };
- })));
- LK.localStorage.setItem('blackBlocks', JSON.stringify(game.children.filter(function (child) {
- return child instanceof BlackBlock;
- }).map(function (block) {
- return {
- x: block.x,
- y: block.y
- };
- })));
+ var gameState = {
+ blueBlocks: game.children.filter(function (child) {
+ return child instanceof BlueBlock;
+ }).map(function (block) {
+ return {
+ x: block.x,
+ y: block.y
+ };
+ }),
+ blackBlocks: game.children.filter(function (child) {
+ return child instanceof BlackBlock;
+ }).map(function (block) {
+ return {
+ x: block.x,
+ y: block.y
+ };
+ })
+ };
+ LK.localStorage.setItem('gameState', JSON.stringify(gameState));
console.log('Game state saved.');
}
};
});
@@ -78,10 +81,8 @@
* Game Code
****/
LK.on('init', function () {
// Automatically load the game state when the game initializes
- var savedBlueBlocks = typeof LK.localStorage !== 'undefined' && LK.localStorage.getItem('blueBlocks') ? JSON.parse(LK.localStorage.getItem('blueBlocks')) : [];
- var savedBlackBlocks = typeof LK.localStorage !== 'undefined' && LK.localStorage.getItem('blackBlocks') ? JSON.parse(LK.localStorage.getItem('blackBlocks')) : [];
savedBlueBlocks.forEach(function (data) {
var blueBlock = game.addChild(new BlueBlock());
blueBlock.x = data.x;
blueBlock.y = data.y;
@@ -90,8 +91,22 @@
var blackBlock = game.addChild(new BlackBlock());
blackBlock.x = data.x;
blackBlock.y = data.y;
});
+ var savedGameState = typeof LK.localStorage !== 'undefined' && LK.localStorage.getItem('gameState') ? JSON.parse(LK.localStorage.getItem('gameState')) : {
+ blueBlocks: [],
+ blackBlocks: []
+ };
+ savedGameState.blueBlocks.forEach(function (data) {
+ var blueBlock = game.addChild(new BlueBlock());
+ blueBlock.x = data.x;
+ blueBlock.y = data.y;
+ });
+ savedGameState.blackBlocks.forEach(function (data) {
+ var blackBlock = game.addChild(new BlackBlock());
+ blackBlock.x = data.x;
+ blackBlock.y = data.y;
+ });
console.log('Game state loaded.');
});
// Function to save the current state of blueBlocks and blackBlocks
// Initialize important asset arrays