User prompt
Add money set
User prompt
Add houses
User prompt
Please fix the bug: 'Uncaught ReferenceError: dragNode is not defined' in or related to this line: 'if (dragNode) {' Line Number: 98
User prompt
Please fix the bug: 'Uncaught TypeError: chocolates[i].containsPoint is not a function' in or related to this line: 'if (chocolates[i].containsPoint({' Line Number: 103
Initial prompt
Chocolate shop
/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Chocolate class var Chocolate = Container.expand(function () { var self = Container.call(this); var chocolateGraphics = self.attachAsset('chocolate', { anchorX: 0.5, anchorY: 0.5 }); self.containsPoint = function (point) { var bounds = self.getBounds(); return point.x >= bounds.x && point.x <= bounds.x + bounds.width && point.y >= bounds.y && point.y <= bounds.y + bounds.height; }; self.update = function () { // Update logic for chocolate }; }); // Customer class var Customer = Container.expand(function () { var self = Container.call(this); var customerGraphics = self.attachAsset('customer', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for customer }; }); // House class var House = Container.expand(function () { var self = Container.call(this); var houseGraphics = self.attachAsset('house', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for house }; }); var Money = Container.expand(function () { var self = Container.call(this); var moneyGraphics = self.attachAsset('money', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for money }; }); // Shop class var Shop = Container.expand(function () { var self = Container.call(this); var shopGraphics = self.attachAsset('shop', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for shop }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var chocolates = []; var customers = []; var score = 0; var money = []; function createMoney() { var moneyInstance = new Money(); moneyInstance.x = Math.random() * 2048; moneyInstance.y = Math.random() * 1000; money.push(moneyInstance); game.addChild(moneyInstance); } var dragNode = null; // Create and position shop var shop = game.addChild(new Shop()); shop.x = 2048 / 2; shop.y = 2732 - 200; // Create and position house var house = game.addChild(new House()); house.x = 2048 / 2; house.y = 2732 / 2; // Create score text var scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to create a new chocolate function createChocolate() { var chocolate = new Chocolate(); chocolate.x = Math.random() * 2048; chocolate.y = Math.random() * 1000; chocolates.push(chocolate); game.addChild(chocolate); } // Function to create a new customer function createCustomer() { var customer = new Customer(); customer.x = Math.random() * 2048; customer.y = Math.random() * 1000; customers.push(customer); game.addChild(customer); } // Update score function updateScore() { scoreTxt.setText('Score: ' + score); } // Handle move events function handleMove(x, y, obj) { // Handle dragging chocolates if (dragNode) { dragNode.x = x; dragNode.y = y; } } // Handle down events game.down = function (x, y, obj) { // Check if a chocolate is clicked for (var i = 0; i < chocolates.length; i++) { if (chocolates[i].containsPoint({ x: x, y: y })) { dragNode = chocolates[i]; break; } } handleMove(x, y, obj); }; // Handle up events game.up = function (x, y, obj) { dragNode = null; }; // Game update function game.update = function () { // Check for interactions between chocolates and customers for (var i = chocolates.length - 1; i >= 0; i--) { for (var j = customers.length - 1; j >= 0; j--) { if (chocolates[i].intersects(customers[j])) { // Increase score and remove chocolate and customer score += 10; updateScore(); chocolates[i].destroy(); customers[j].destroy(); chocolates.splice(i, 1); customers.splice(j, 1); break; } } } // Create new chocolates and customers periodically if (LK.ticks % 60 == 0) { createChocolate(); } if (LK.ticks % 120 == 0) { createCustomer(); } if (LK.ticks % 180 == 0) { createMoney(); } }; // Initialize game elements createChocolate(); createCustomer();
===================================================================
--- original.js
+++ change.js
@@ -38,8 +38,18 @@
self.update = function () {
// Update logic for house
};
});
+var Money = Container.expand(function () {
+ var self = Container.call(this);
+ var moneyGraphics = self.attachAsset('money', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Update logic for money
+ };
+});
// Shop class
var Shop = Container.expand(function () {
var self = Container.call(this);
var shopGraphics = self.attachAsset('shop', {
@@ -64,8 +74,16 @@
// Initialize arrays and variables
var chocolates = [];
var customers = [];
var score = 0;
+var money = [];
+function createMoney() {
+ var moneyInstance = new Money();
+ moneyInstance.x = Math.random() * 2048;
+ moneyInstance.y = Math.random() * 1000;
+ money.push(moneyInstance);
+ game.addChild(moneyInstance);
+}
var dragNode = null;
// Create and position shop
var shop = game.addChild(new Shop());
shop.x = 2048 / 2;
@@ -150,8 +168,11 @@
}
if (LK.ticks % 120 == 0) {
createCustomer();
}
+ if (LK.ticks % 180 == 0) {
+ createMoney();
+ }
};
// Initialize game elements
createChocolate();
createCustomer();
\ No newline at end of file