User prompt
I added log and leaf
User prompt
I added water and lava
User prompt
I added sand
Code edit (1 edits merged)
Please save this source code
User prompt
Block Builder World
Initial prompt
Block build. With 3 blocks that you can choose you can make a world. The three blocks are stone dirt and grass
/**** * Classes ****/ var Block = Container.expand(function (blockType) { var self = Container.call(this); self.blockType = blockType || 'stone'; var assetName = self.blockType + 'Block'; var blockGraphics = self.attachAsset(assetName, { anchorX: 0.5, anchorY: 0.5 }); return self; }); var BlockButton = Container.expand(function (blockType) { var self = Container.call(this); self.blockType = blockType; self.isSelected = false; self.buttonBg = self.attachAsset('buttonBg', { anchorX: 0.5, anchorY: 0.5 }); self.selectedBg = self.attachAsset('selectedButtonBg', { anchorX: 0.5, anchorY: 0.5 }); self.selectedBg.visible = false; var blockPreview = new Block(blockType); blockPreview.scale.set(0.6); self.addChild(blockPreview); self.setSelected = function (selected) { self.isSelected = selected; self.buttonBg.visible = !selected; self.selectedBg.visible = selected; }; self.down = function (x, y, obj) { selectBlockType(self.blockType); }; return self; }); var GridCell = Container.expand(function (gridX, gridY) { var self = Container.call(this); self.gridX = gridX; self.gridY = gridY; self.hasBlock = false; self.block = null; var cellBorder = self.attachAsset('gridCell', { anchorX: 0.5, anchorY: 0.5 }); cellBorder.alpha = 0.1; self.placeBlock = function (blockType) { if (!self.hasBlock) { self.block = new Block(blockType); self.addChild(self.block); self.hasBlock = true; LK.getSound('placeBlock').play(); } }; self.removeBlock = function () { if (self.hasBlock && self.block) { self.removeChild(self.block); self.block = null; self.hasBlock = false; LK.getSound('removeBlock').play(); } }; self.down = function (x, y, obj) { if (self.hasBlock) { self.removeBlock(); } else { self.placeBlock(selectedBlockType); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var GRID_SIZE = 120; var GRID_COLS = 16; var GRID_ROWS = 18; var GRID_START_X = (2048 - GRID_COLS * GRID_SIZE) / 2; var GRID_START_Y = 100; var selectedBlockType = 'stone'; var grid = []; var blockButtons = {}; // Create grid for (var row = 0; row < GRID_ROWS; row++) { grid[row] = []; for (var col = 0; col < GRID_COLS; col++) { var cell = new GridCell(col, row); cell.x = GRID_START_X + col * GRID_SIZE + GRID_SIZE / 2; cell.y = GRID_START_Y + row * GRID_SIZE + GRID_SIZE / 2; grid[row][col] = cell; game.addChild(cell); } } // Create block selection buttons var buttonContainer = new Container(); buttonContainer.x = 2048 / 2; buttonContainer.y = 2732 - 100; game.addChild(buttonContainer); var blockTypes = ['stone', 'dirt', 'grass']; var buttonSpacing = 200; var totalWidth = (blockTypes.length - 1) * buttonSpacing; var startX = -totalWidth / 2; for (var i = 0; i < blockTypes.length; i++) { var blockType = blockTypes[i]; var button = new BlockButton(blockType); button.x = startX + i * buttonSpacing; blockButtons[blockType] = button; buttonContainer.addChild(button); } // Create clear button var clearButton = new Container(); var clearBg = clearButton.attachAsset('buttonBg', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2 }); var clearText = new Text2('CLEAR', { size: 40, fill: 0xFFFFFF }); clearText.anchor.set(0.5, 0.5); clearButton.addChild(clearText); clearButton.x = 2048 / 2; clearButton.y = 2732 - 250; game.addChild(clearButton); clearButton.down = function (x, y, obj) { clearGrid(); }; function selectBlockType(blockType) { selectedBlockType = blockType; for (var type in blockButtons) { blockButtons[type].setSelected(type === blockType); } } function clearGrid() { for (var row = 0; row < GRID_ROWS; row++) { for (var col = 0; col < GRID_COLS; col++) { if (grid[row][col].hasBlock) { grid[row][col].removeBlock(); } } } } ; // Initialize with stone selected selectBlockType('stone'); game.update = function () { // Game runs continuously for creative building };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,161 @@
-/****
+/****
+* Classes
+****/
+var Block = Container.expand(function (blockType) {
+ var self = Container.call(this);
+ self.blockType = blockType || 'stone';
+ var assetName = self.blockType + 'Block';
+ var blockGraphics = self.attachAsset(assetName, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ return self;
+});
+var BlockButton = Container.expand(function (blockType) {
+ var self = Container.call(this);
+ self.blockType = blockType;
+ self.isSelected = false;
+ self.buttonBg = self.attachAsset('buttonBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.selectedBg = self.attachAsset('selectedButtonBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.selectedBg.visible = false;
+ var blockPreview = new Block(blockType);
+ blockPreview.scale.set(0.6);
+ self.addChild(blockPreview);
+ self.setSelected = function (selected) {
+ self.isSelected = selected;
+ self.buttonBg.visible = !selected;
+ self.selectedBg.visible = selected;
+ };
+ self.down = function (x, y, obj) {
+ selectBlockType(self.blockType);
+ };
+ return self;
+});
+var GridCell = Container.expand(function (gridX, gridY) {
+ var self = Container.call(this);
+ self.gridX = gridX;
+ self.gridY = gridY;
+ self.hasBlock = false;
+ self.block = null;
+ var cellBorder = self.attachAsset('gridCell', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ cellBorder.alpha = 0.1;
+ self.placeBlock = function (blockType) {
+ if (!self.hasBlock) {
+ self.block = new Block(blockType);
+ self.addChild(self.block);
+ self.hasBlock = true;
+ LK.getSound('placeBlock').play();
+ }
+ };
+ self.removeBlock = function () {
+ if (self.hasBlock && self.block) {
+ self.removeChild(self.block);
+ self.block = null;
+ self.hasBlock = false;
+ LK.getSound('removeBlock').play();
+ }
+ };
+ self.down = function (x, y, obj) {
+ if (self.hasBlock) {
+ self.removeBlock();
+ } else {
+ self.placeBlock(selectedBlockType);
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var GRID_SIZE = 120;
+var GRID_COLS = 16;
+var GRID_ROWS = 18;
+var GRID_START_X = (2048 - GRID_COLS * GRID_SIZE) / 2;
+var GRID_START_Y = 100;
+var selectedBlockType = 'stone';
+var grid = [];
+var blockButtons = {};
+// Create grid
+for (var row = 0; row < GRID_ROWS; row++) {
+ grid[row] = [];
+ for (var col = 0; col < GRID_COLS; col++) {
+ var cell = new GridCell(col, row);
+ cell.x = GRID_START_X + col * GRID_SIZE + GRID_SIZE / 2;
+ cell.y = GRID_START_Y + row * GRID_SIZE + GRID_SIZE / 2;
+ grid[row][col] = cell;
+ game.addChild(cell);
+ }
+}
+// Create block selection buttons
+var buttonContainer = new Container();
+buttonContainer.x = 2048 / 2;
+buttonContainer.y = 2732 - 100;
+game.addChild(buttonContainer);
+var blockTypes = ['stone', 'dirt', 'grass'];
+var buttonSpacing = 200;
+var totalWidth = (blockTypes.length - 1) * buttonSpacing;
+var startX = -totalWidth / 2;
+for (var i = 0; i < blockTypes.length; i++) {
+ var blockType = blockTypes[i];
+ var button = new BlockButton(blockType);
+ button.x = startX + i * buttonSpacing;
+ blockButtons[blockType] = button;
+ buttonContainer.addChild(button);
+}
+// Create clear button
+var clearButton = new Container();
+var clearBg = clearButton.attachAsset('buttonBg', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 1.2
+});
+var clearText = new Text2('CLEAR', {
+ size: 40,
+ fill: 0xFFFFFF
+});
+clearText.anchor.set(0.5, 0.5);
+clearButton.addChild(clearText);
+clearButton.x = 2048 / 2;
+clearButton.y = 2732 - 250;
+game.addChild(clearButton);
+clearButton.down = function (x, y, obj) {
+ clearGrid();
+};
+function selectBlockType(blockType) {
+ selectedBlockType = blockType;
+ for (var type in blockButtons) {
+ blockButtons[type].setSelected(type === blockType);
+ }
+}
+function clearGrid() {
+ for (var row = 0; row < GRID_ROWS; row++) {
+ for (var col = 0; col < GRID_COLS; col++) {
+ if (grid[row][col].hasBlock) {
+ grid[row][col].removeBlock();
+ }
+ }
+ }
+}
+;
+// Initialize with stone selected
+selectBlockType('stone');
+game.update = function () {
+ // Game runs continuously for creative building
+};
\ No newline at end of file