User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var localPos = self.toLocal(obj.parent.toGlobal(obj.position));' Line Number: 86
Code edit (1 edits merged)
Please save this source code
User prompt
DAW - Draw And Win
Initial prompt
daw
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Button = Container.expand(function (text, color) { var self = Container.call(this); var buttonShape = self.attachAsset(color === 0xff4444 ? 'clearButton' : 'submitButton', { anchorX: 0.5, anchorY: 0.5 }); var buttonText = new Text2(text, { size: 40, fill: 0xFFFFFF }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.setText = function (newText) { buttonText.setText(newText); }; return self; }); var DrawingCanvas = Container.expand(function () { var self = Container.call(this); var canvas = self.attachAsset('canvas', { anchorX: 0.5, anchorY: 0.5 }); self.drawingPoints = []; self.isDrawing = false; self.lastX = 0; self.lastY = 0; self.startDrawing = function (x, y) { self.isDrawing = true; self.lastX = x; self.lastY = y; self.addPoint(x, y); }; self.continuDrawing = function (x, y) { if (!self.isDrawing) return; var distance = Math.sqrt(Math.pow(x - self.lastX, 2) + Math.pow(y - self.lastY, 2)); if (distance > 5) { self.addPoint(x, y); self.lastX = x; self.lastY = y; } }; self.stopDrawing = function () { self.isDrawing = false; }; self.addPoint = function (x, y) { var point = new DrawingPoint(); point.x = x; point.y = y; self.drawingPoints.push(point); self.addChild(point); }; self.clearDrawing = function () { for (var i = 0; i < self.drawingPoints.length; i++) { self.drawingPoints[i].destroy(); } self.drawingPoints = []; }; self.down = function (x, y, obj) { var localPos; if (obj && obj.parent && obj.position) { localPos = self.toLocal(obj.parent.toGlobal(obj.position)); } else { localPos = { x: x, y: y }; } self.startDrawing(localPos.x, localPos.y); }; self.move = function (x, y, obj) { var localPos; if (obj && obj.parent && obj.position) { localPos = self.toLocal(obj.parent.toGlobal(obj.position)); } else { localPos = { x: x, y: y }; } self.continuDrawing(localPos.x, localPos.y); }; self.up = function (x, y, obj) { self.stopDrawing(); }; return self; }); var DrawingPoint = Container.expand(function () { var self = Container.call(this); var dot = self.attachAsset('drawingLine', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xf0f0f0 }); /**** * Game Code ****/ // Game state variables var gameState = 'playing'; // 'playing', 'evaluating', 'nextRound' var currentPrompt = ''; var timeLeft = 30; var streak = 0; var roundsPlayed = 0; var isDrawing = false; // Drawing prompts by difficulty var easyPrompts = ['CAT', 'DOG', 'HOUSE', 'SUN', 'TREE', 'CAR', 'FISH', 'BIRD']; var mediumPrompts = ['ELEPHANT', 'AIRPLANE', 'BICYCLE', 'FLOWER', 'CASTLE', 'ROCKET', 'GUITAR', 'BUTTERFLY']; var hardPrompts = ['DINOSAUR', 'HELICOPTER', 'BASKETBALL', 'SNOWFLAKE', 'OCTOPUS', 'TELESCOPE', 'LIGHTHOUSE', 'DRAGONFLY']; // UI Elements var promptText = new Text2('Draw: CAT', { size: 80, fill: 0x333333 }); promptText.anchor.set(0.5, 0); LK.gui.top.addChild(promptText); var timerText = new Text2('30', { size: 60, fill: 0xFF4444 }); timerText.anchor.set(1, 0); LK.gui.topRight.addChild(timerText); var scoreText = new Text2('Score: 0', { size: 50, fill: 0x333333 }); scoreText.anchor.set(0, 0); LK.gui.topLeft.addChild(scoreText); scoreText.x = 120; // Offset from platform menu var streakText = new Text2('', { size: 40, fill: 0x4444FF }); streakText.anchor.set(0.5, 0); LK.gui.top.addChild(streakText); streakText.y = 100; // Game objects var drawingCanvas = game.addChild(new DrawingCanvas()); drawingCanvas.x = 2048 / 2; drawingCanvas.y = 2732 / 2; var clearButton = game.addChild(new Button('CLEAR', 0xff4444)); clearButton.x = 2048 / 2 - 220; clearButton.y = 2732 - 150; var submitButton = game.addChild(new Button('SUBMIT', 0x44ff44)); submitButton.x = 2048 / 2 + 220; submitButton.y = 2732 - 150; // Game timer var gameTimer = LK.setInterval(function () { if (gameState === 'playing' && timeLeft > 0) { timeLeft--; timerText.setText(timeLeft.toString()); if (timeLeft <= 10) { timerText.fill = "#ff0000"; if (timeLeft <= 5) { LK.getSound('tick').play(); } } if (timeLeft <= 0) { evaluateDrawing(false); } } }, 1000); // Button interactions clearButton.down = function (x, y, obj) { if (gameState === 'playing') { drawingCanvas.clearDrawing(); tween(clearButton, { scaleX: 0.9, scaleY: 0.9 }, { duration: 100 }); } }; clearButton.up = function (x, y, obj) { tween(clearButton, { scaleX: 1, scaleY: 1 }, { duration: 100 }); }; submitButton.down = function (x, y, obj) { if (gameState === 'playing' && drawingCanvas.drawingPoints.length > 0) { evaluateDrawing(true); tween(submitButton, { scaleX: 0.9, scaleY: 0.9 }, { duration: 100 }); } }; submitButton.up = function (x, y, obj) { tween(submitButton, { scaleX: 1, scaleY: 1 }, { duration: 100 }); }; // Game functions function getRandomPrompt() { var prompts; if (roundsPlayed < 3) { prompts = easyPrompts; } else if (roundsPlayed < 8) { prompts = mediumPrompts; } else { prompts = hardPrompts; } var randomIndex = Math.floor(Math.random() * prompts.length); return prompts[randomIndex]; } function startNewRound() { gameState = 'playing'; currentPrompt = getRandomPrompt(); timeLeft = Math.max(15, 30 - Math.floor(roundsPlayed / 3) * 3); promptText.setText('Draw: ' + currentPrompt); timerText.setText(timeLeft.toString()); timerText.fill = "#ff4444"; drawingCanvas.clearDrawing(); roundsPlayed++; // Update streak display if (streak > 0) { streakText.setText('Streak: ' + streak); } else { streakText.setText(''); } } function evaluateDrawing(submitted) { gameState = 'evaluating'; // Simple evaluation based on drawing complexity and submission var pointsDrawn = drawingCanvas.drawingPoints.length; var timeBonus = timeLeft * 2; var complexityBonus = Math.min(pointsDrawn * 0.5, 50); var success = false; if (submitted && pointsDrawn > 20) { // Basic success criteria: submitted with reasonable drawing success = Math.random() > 0.3; // 70% success rate for demonstration } if (success) { var points = 100 + timeBonus + complexityBonus + streak * 10; LK.setScore(LK.getScore() + Math.floor(points)); streak++; LK.getSound('correct').play(); LK.effects.flashScreen(0x00ff00, 500); promptText.setText('Correct! +' + Math.floor(points) + ' points'); promptText.fill = "#00aa00"; } else { streak = 0; LK.getSound('wrong').play(); LK.effects.flashScreen(0xff0000, 500); if (submitted) { promptText.setText('Not quite right... Try again!'); } else { promptText.setText('Time\'s up!'); } promptText.fill = "#aa0000"; } scoreText.setText('Score: ' + LK.getScore()); // Check win condition if (LK.getScore() >= 2000) { LK.showYouWin(); return; } // Schedule next round LK.setTimeout(function () { startNewRound(); promptText.fill = "#333333"; }, 2000); } // Initialize first round startNewRound(); // Update score display scoreText.setText('Score: ' + LK.getScore()); game.update = function () { // Handle any per-frame updates if needed if (gameState === 'playing') { // Update button states based on drawing if (drawingCanvas.drawingPoints.length > 0) { submitButton.alpha = 1.0; } else { submitButton.alpha = 0.5; } } };
===================================================================
--- original.js
+++ change.js
@@ -65,13 +65,29 @@
}
self.drawingPoints = [];
};
self.down = function (x, y, obj) {
- var localPos = self.toLocal(obj.parent.toGlobal(obj.position));
+ var localPos;
+ if (obj && obj.parent && obj.position) {
+ localPos = self.toLocal(obj.parent.toGlobal(obj.position));
+ } else {
+ localPos = {
+ x: x,
+ y: y
+ };
+ }
self.startDrawing(localPos.x, localPos.y);
};
self.move = function (x, y, obj) {
- var localPos = self.toLocal(obj.parent.toGlobal(obj.position));
+ var localPos;
+ if (obj && obj.parent && obj.position) {
+ localPos = self.toLocal(obj.parent.toGlobal(obj.position));
+ } else {
+ localPos = {
+ x: x,
+ y: y
+ };
+ }
self.continuDrawing(localPos.x, localPos.y);
};
self.up = function (x, y, obj) {
self.stopDrawing();