User prompt
add music hearing
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));' Line Number: 341
Code edit (1 edits merged)
Please save this source code
User prompt
Zoo Builder
Initial prompt
2d, build a zoo, animals: lion, elephant, zebra and penguin
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var GridSystem = Container.expand(function () { var self = Container.call(this); self.gridWidth = 8; self.gridHeight = 10; self.cellSize = 200; self.grid = []; for (var y = 0; y < self.gridHeight; y++) { self.grid[y] = []; for (var x = 0; x < self.gridWidth; x++) { self.grid[y][x] = null; var gridSquare = self.addChild(LK.getAsset('gridSquare', { anchorX: 0, anchorY: 0 })); gridSquare.x = x * self.cellSize + 200; gridSquare.y = y * self.cellSize + 400; gridSquare.alpha = 0.3; } } // Add water features var waterFeature1 = self.addChild(LK.getAsset('waterFeature', { anchorX: 0, anchorY: 0 })); waterFeature1.x = 2 * self.cellSize + 200; waterFeature1.y = 8 * self.cellSize + 400; var waterFeature2 = self.addChild(LK.getAsset('waterFeature', { anchorX: 0, anchorY: 0 })); waterFeature2.x = 5 * self.cellSize + 200; waterFeature2.y = 8 * self.cellSize + 400; self.canPlaceHabitat = function (habitat, gridX, gridY) { if (gridX < 0 || gridY < 0 || gridX + habitat.gridSize > self.gridWidth || gridY + habitat.gridSize > self.gridHeight) { return false; } for (var y = gridY; y < gridY + habitat.gridSize; y++) { for (var x = gridX; x < gridX + habitat.gridSize; x++) { if (self.grid[y][x] !== null) { return false; } } } return true; }; self.placeHabitat = function (habitat, gridX, gridY) { if (!self.canPlaceHabitat(habitat, gridX, gridY)) { return false; } for (var y = gridY; y < gridY + habitat.gridSize; y++) { for (var x = gridX; x < gridX + habitat.gridSize; x++) { self.grid[y][x] = habitat; } } habitat.gridX = gridX; habitat.gridY = gridY; habitat.x = gridX * self.cellSize + 200; habitat.y = gridY * self.cellSize + 400; habitat.placed = true; // Check for penguin water bonus if (habitat.animalType === 'penguin') { var nearWater = false; if ((gridX === 1 || gridX === 2 || gridX === 3) && gridY === 7) nearWater = true; if ((gridX === 4 || gridX === 5 || gridX === 6) && gridY === 7) nearWater = true; if (nearWater) { habitat.visitorPoints *= 1.5; habitat.revenue *= 1.5; LK.effects.flashObject(habitat, 0x00ff00, 1000); } } visitorSatisfaction += habitat.visitorPoints; updateUI(); LK.getSound('placeHabitat').play(); return true; }; self.removeHabitat = function (habitat) { if (!habitat.placed) return; for (var y = habitat.gridY; y < habitat.gridY + habitat.gridSize; y++) { for (var x = habitat.gridX; x < habitat.gridX + habitat.gridSize; x++) { self.grid[y][x] = null; } } visitorSatisfaction -= habitat.visitorPoints; habitat.placed = false; habitat.gridX = -1; habitat.gridY = -1; updateUI(); }; self.getGridPosition = function (worldX, worldY) { var gridX = Math.floor((worldX - 200) / self.cellSize); var gridY = Math.floor((worldY - 400) / self.cellSize); return { x: gridX, y: gridY }; }; return self; }); var Habitat = Container.expand(function (animalType, size) { var self = Container.call(this); self.animalType = animalType; self.gridSize = size; self.visitorPoints = 0; self.revenue = 0; self.placed = false; self.gridX = -1; self.gridY = -1; var habitatGraphics; if (animalType === 'lion') { habitatGraphics = self.attachAsset('lionHabitat', { anchorX: 0, anchorY: 0 }); self.visitorPoints = 10; self.revenue = 5; } else if (animalType === 'elephant') { habitatGraphics = self.attachAsset('elephantHabitat', { anchorX: 0, anchorY: 0 }); self.visitorPoints = 25; self.revenue = 15; } else if (animalType === 'zebra') { habitatGraphics = self.attachAsset('zebraHabitat', { anchorX: 0, anchorY: 0 }); self.visitorPoints = 8; self.revenue = 4; } else if (animalType === 'penguin') { habitatGraphics = self.attachAsset('penguinHabitat', { anchorX: 0, anchorY: 0 }); self.visitorPoints = 12; self.revenue = 7; } self.animalLabel = new Text2(animalType.toUpperCase(), { size: 24, fill: 0x000000 }); self.animalLabel.anchor.set(0.5, 0.5); self.animalLabel.x = habitatGraphics.width / 2; self.animalLabel.y = habitatGraphics.height / 2; self.addChild(self.animalLabel); self.lastRevenueTime = Date.now(); self.update = function () { if (self.placed && Date.now() - self.lastRevenueTime > 3000) { self.lastRevenueTime = Date.now(); var coinEffect = game.addChild(LK.getAsset('coin', { anchorX: 0.5, anchorY: 0.5 })); coinEffect.x = self.x + self.width / 2; coinEffect.y = self.y + self.height / 2; tween(coinEffect, { y: coinEffect.y - 100, alpha: 0 }, { duration: 1000, onFinish: function onFinish() { coinEffect.destroy(); } }); coins += self.revenue; updateUI(); LK.getSound('collectCoin').play(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ var coins = 100; var visitorSatisfaction = 0; var targetSatisfaction = 200; var selectedAnimalType = null; var draggedHabitat = null; var habitats = []; var gridSystem = game.addChild(new GridSystem()); // UI Elements var coinsText = new Text2('Coins: ' + coins, { size: 40, fill: 0xFFFFFF }); coinsText.anchor.set(0, 0); LK.gui.topRight.addChild(coinsText); var satisfactionText = new Text2('Satisfaction: ' + visitorSatisfaction + '/' + targetSatisfaction, { size: 40, fill: 0xFFFFFF }); satisfactionText.anchor.set(0.5, 0); LK.gui.top.addChild(satisfactionText); // Animal selection buttons var buttonY = 150; var buttonSpacing = 300; var lionButton = LK.gui.bottomLeft.addChild(LK.getAsset('lionHabitat', { anchorX: 0, anchorY: 1, scaleX: 0.8, scaleY: 0.8 })); lionButton.x = 100; lionButton.y = -buttonY; var elephantButton = LK.gui.bottomLeft.addChild(LK.getAsset('elephantHabitat', { anchorX: 0, anchorY: 1, scaleX: 0.4, scaleY: 0.4 })); elephantButton.x = 100 + buttonSpacing; elephantButton.y = -buttonY; var zebraButton = LK.gui.bottomLeft.addChild(LK.getAsset('zebraHabitat', { anchorX: 0, anchorY: 1, scaleX: 0.8, scaleY: 0.8 })); zebraButton.x = 100 + buttonSpacing * 2; zebraButton.y = -buttonY; var penguinButton = LK.gui.bottomLeft.addChild(LK.getAsset('penguinHabitat', { anchorX: 0, anchorY: 1, scaleX: 0.8, scaleY: 0.8 })); penguinButton.x = 100 + buttonSpacing * 3; penguinButton.y = -buttonY; // Button labels var lionLabel = new Text2('LION\n$20', { size: 20, fill: 0x000000 }); lionLabel.anchor.set(0.5, 0.5); lionLabel.x = lionButton.x + 75; lionLabel.y = lionButton.y - 75; LK.gui.bottomLeft.addChild(lionLabel); var elephantLabel = new Text2('ELEPHANT\n$50', { size: 20, fill: 0x000000 }); elephantLabel.anchor.set(0.5, 0.5); elephantLabel.x = elephantButton.x + 75; elephantLabel.y = elephantButton.y - 75; LK.gui.bottomLeft.addChild(elephantLabel); var zebraLabel = new Text2('ZEBRA\n$15', { size: 20, fill: 0x000000 }); zebraLabel.anchor.set(0.5, 0.5); zebraLabel.x = zebraButton.x + 75; zebraLabel.y = zebraButton.y - 75; LK.gui.bottomLeft.addChild(zebraLabel); var penguinLabel = new Text2('PENGUIN\n$25', { size: 20, fill: 0x000000 }); penguinLabel.anchor.set(0.5, 0.5); penguinLabel.x = penguinButton.x + 75; penguinLabel.y = penguinButton.y - 75; LK.gui.bottomLeft.addChild(penguinLabel); function updateUI() { coinsText.setText('Coins: ' + coins); satisfactionText.setText('Satisfaction: ' + visitorSatisfaction + '/' + targetSatisfaction); if (visitorSatisfaction >= targetSatisfaction) { LK.showYouWin(); } } function getAnimalCost(animalType) { switch (animalType) { case 'lion': return 20; case 'elephant': return 50; case 'zebra': return 15; case 'penguin': return 25; default: return 0; } } function getAnimalSize(animalType) { return animalType === 'elephant' ? 2 : 1; } function createHabitat(animalType) { var cost = getAnimalCost(animalType); if (coins < cost) return null; coins -= cost; updateUI(); var size = getAnimalSize(animalType); var habitat = new Habitat(animalType, size); habitats.push(habitat); return habitat; } // Button event handlers lionButton.down = function (x, y, obj) { var habitat = createHabitat('lion'); if (habitat) { draggedHabitat = habitat; game.addChild(habitat); var gamePos = game.toLocal({ x: lionButton.x + x, y: lionButton.y + y }); habitat.x = gamePos.x; habitat.y = gamePos.y; } }; elephantButton.down = function (x, y, obj) { var habitat = createHabitat('elephant'); if (habitat) { draggedHabitat = habitat; game.addChild(habitat); var gamePos = game.toLocal({ x: elephantButton.x + x, y: elephantButton.y + y }); habitat.x = gamePos.x; habitat.y = gamePos.y; } }; zebraButton.down = function (x, y, obj) { var habitat = createHabitat('zebra'); if (habitat) { draggedHabitat = habitat; game.addChild(habitat); var gamePos = game.toLocal({ x: zebraButton.x + x, y: zebraButton.y + y }); habitat.x = gamePos.x; habitat.y = gamePos.y; } }; penguinButton.down = function (x, y, obj) { var habitat = createHabitat('penguin'); if (habitat) { draggedHabitat = habitat; game.addChild(habitat); var gamePos = game.toLocal({ x: penguinButton.x + x, y: penguinButton.y + y }); habitat.x = gamePos.x; habitat.y = gamePos.y; } }; game.move = function (x, y, obj) { if (draggedHabitat) { draggedHabitat.x = x - draggedHabitat.width / 2; draggedHabitat.y = y - draggedHabitat.height / 2; } }; game.up = function (x, y, obj) { if (draggedHabitat) { var gridPos = gridSystem.getGridPosition(x, y); if (gridSystem.placeHabitat(draggedHabitat, gridPos.x, gridPos.y)) { // Successfully placed } else { // Failed to place, remove habitat and refund var cost = getAnimalCost(draggedHabitat.animalType); coins += cost; updateUI(); var habitatIndex = habitats.indexOf(draggedHabitat); if (habitatIndex > -1) { habitats.splice(habitatIndex, 1); } draggedHabitat.destroy(); } draggedHabitat = null; } }; game.update = function () { for (var i = 0; i < habitats.length; i++) { habitats[i].update(); } };
===================================================================
--- original.js
+++ change.js
@@ -315,9 +315,12 @@
var habitat = createHabitat('lion');
if (habitat) {
draggedHabitat = habitat;
game.addChild(habitat);
- var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));
+ var gamePos = game.toLocal({
+ x: lionButton.x + x,
+ y: lionButton.y + y
+ });
habitat.x = gamePos.x;
habitat.y = gamePos.y;
}
};
@@ -325,9 +328,12 @@
var habitat = createHabitat('elephant');
if (habitat) {
draggedHabitat = habitat;
game.addChild(habitat);
- var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));
+ var gamePos = game.toLocal({
+ x: elephantButton.x + x,
+ y: elephantButton.y + y
+ });
habitat.x = gamePos.x;
habitat.y = gamePos.y;
}
};
@@ -335,9 +341,12 @@
var habitat = createHabitat('zebra');
if (habitat) {
draggedHabitat = habitat;
game.addChild(habitat);
- var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));
+ var gamePos = game.toLocal({
+ x: zebraButton.x + x,
+ y: zebraButton.y + y
+ });
habitat.x = gamePos.x;
habitat.y = gamePos.y;
}
};
@@ -345,9 +354,12 @@
var habitat = createHabitat('penguin');
if (habitat) {
draggedHabitat = habitat;
game.addChild(habitat);
- var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));
+ var gamePos = game.toLocal({
+ x: penguinButton.x + x,
+ y: penguinButton.y + y
+ });
habitat.x = gamePos.x;
habitat.y = gamePos.y;
}
};
elephants in elephant enclosure. In-Game asset. 2d. High contrast. No shadows
sleeping lion in a lion enclosure, jungle styled. In-Game asset. 2d. High contrast. No shadows
snowy penguins in penguin exhibit. In-Game asset. 2d. High contrast. No shadows
plains zebra feeding wheat in zebra exhibit. In-Game asset. 2d. High contrast. No shadows
Elephant full body. In-Game asset. 2d. High contrast. No shadows
A masai Lion angry. In-Game asset. 2d. High contrast. No shadows
Bored Penguin holding ice cream. In-Game asset. 2d. High contrast. No shadows
A zebra. In-Game asset. 2d. High contrast. No shadows