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
/****
* Classes
****/
// BlueBlock class for creating blue blocks
var BlueBlock = Container.expand(function () {
var self = Container.call(this);
var blueBlockGraphics = self.createAsset('blueBlock', 'Blue block', 0.5, 0.5);
self.layer = 1;
self.move = function () {};
self.update = function () {};
});
// BlackBlock class for creating black blocks
var BlackBlock = Container.expand(function () {
var self = Container.call(this);
var blackBlockGraphics = self.createAsset('blackBlock', 'Black block', 0.5, 0.5);
self.move = function () {};
self.update = function () {};
});
// SaveButton class for creating save button
var SaveButton = Container.expand(function () {
var self = Container.call(this);
var saveButtonGraphics = self.createAsset('saveButton', 'Save button', 0.5, 0.5);
saveButtonGraphics.rotation = Math.PI / 4; // Set button at 45 degrees angle
self.layer = 2;
self.on('rightdown', function () {
self.saveGame();
});
self.saveGame = function () {
if (typeof LK.localStorage !== 'undefined') {
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.');
}
};
});
// Function to save the current state of blueBlocks and blackBlocks
// ResetButton class for creating reset button
var ResetButton = Container.expand(function () {
var self = Container.call(this);
var resetButtonGraphics = self.createAsset('resetButton', 'Reset button', 0.5, 0.5);
self.layer = 2;
self.on('down', function () {
// Reset the game state
blueBlocks.forEach(function (block) {
block.destroy();
});
blackBlocks.forEach(function (block) {
block.destroy();
});
blueBlocks = [];
blackBlocks = [];
// Optionally, reset other game states here
});
});
// Floor class for creating the floor
var Floor = Container.expand(function () {
var self = Container.call(this);
var floorGraphics = self.createAsset('floor', 'Floor asset', 0.5, 1);
self.update = function () {};
self.interactive = true;
self.on('down', function (obj) {
if (obj.event.data && obj.event.data.button === 0) {
var pos = obj.event.getLocalPosition(game);
createFloor(pos.x, pos.y);
}
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Instantiate and position the floor object
var floor = game.addChild(new Floor());
floor.x = game.width / 2;
floor.y = game.height - floor.height / 2;
LK.on('init', function () {
// Automatically load the game state when the game initializes
savedBlueBlocks.forEach(function (data) {
var blueBlock = game.addChild(new BlueBlock());
blueBlock.x = data.x;
blueBlock.y = data.y;
});
savedBlackBlocks.forEach(function (data) {
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
game.saveGameState = function () {
if (typeof LK.localStorage !== 'undefined') {
LK.localStorage.setItem('blueBlocks', JSON.stringify(blueBlocks.map(function (block) {
return {
x: block.x,
y: block.y
};
})));
}
if (typeof LK.localStorage !== 'undefined') {
LK.localStorage.setItem('blackBlocks', JSON.stringify(blackBlocks.map(function (block) {
return {
x: block.x,
y: block.y
};
})));
}
};
var blueBlocks = [];
var blackBlocks = [];
// Function to handle the creation of the floor at the mouse position
function createFloor(x, y) {
var floor = game.addChild(new Floor());
floor.x = x - floor.width / 2;
floor.y = y - floor.height / 2;
}
// Function to handle the creation of blue blocks
function createBlueBlock(x, y) {
var blueBlock = new BlueBlock();
blueBlock.x = x;
blueBlock.y = y;
blueBlock.layer = 1;
blueBlocks.push(blueBlock);
game.addChild(blueBlock);
game.saveGameState();
}
// Function to handle the creation of black blocks
function createBlackBlock(x, y) {
var blackBlock = new BlackBlock();
blackBlock.x = x;
blackBlock.y = y;
blackBlock.layer = 1;
blackBlocks.push(blackBlock);
game.addChild(blackBlock);
game.saveGameState();
}
// Event listener for left click to create blue blocks
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
// Check if the position is not on the save button before creating a block
if (!saveButton.getBounds().contains(pos.x, pos.y)) {
createBlueBlock(pos.x, pos.y);
}
});
// Event listener for right up to create black blocks
game.on('rightup', function (obj) {
var pos = obj.event.getLocalPosition(game);
// Check if the position is not on the save button before creating a block
if (!saveButton.intersects(pos)) {
createBlackBlock(pos.x, pos.y);
}
});
// Main game update loop
LK.on('tick', function () {
// Update all blue blocks
for (var i = 0; i < blueBlocks.length; i++) {
blueBlocks[i].update();
}
// Update all black blocks
for (var j = 0; j < blackBlocks.length; j++) {
blackBlocks[j].update();
}
}); // Instantiate save button
var saveButton = new SaveButton();
saveButton.x = 20;
saveButton.y = 20;
game.addChild(saveButton);
// Instantiate reset button
var resetButton = new ResetButton();
resetButton.x = game.width - resetButton.width / 2 - 50;
resetButton.y = 50;
resetButton.layer = 2;
game.addChild(resetButton);
// Set up an automatic save function to trigger every 120 seconds (7200 frames at 60FPS)
var autoSaveTimer = LK.setInterval(game.saveGameState, 7200); ===================================================================
--- original.js
+++ change.js
@@ -75,9 +75,9 @@
var floorGraphics = self.createAsset('floor', 'Floor asset', 0.5, 1);
self.update = function () {};
self.interactive = true;
self.on('down', function (obj) {
- if (obj.event.data.button === 0) {
+ if (obj.event.data && obj.event.data.button === 0) {
var pos = obj.event.getLocalPosition(game);
createFloor(pos.x, pos.y);
}
});