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: 120
User prompt
enlarge text
Code edit (1 edits merged)
Please save this source code
User prompt
Which Movie Character Are You?
Initial prompt
How the Game Works The game is simple. You'll answer six multiple-choice questions about your personality, preferences, and lifestyle. For each question, you will be given three options. Your answers will be compared against a random selection of popular movie characters, and the character with the highest number of matching traits will be your result. A match rate will be displayed to show you how closely your answers aligned with the winning character. Important: This is not a scientific test and is purely for fun. The "match rate" is just a simple calculation of how many of your answers directly correspond to the character's profile in the game. The Game Character Pool: Imperator Furiosa from Mad Max: Fury Road Miles Morales from Spider-Man: Into the Spider-Verse Jordan Belfort from The Wolf of Wall Street Mia Dolan from La La Land Elsa from Frozen Peter Quill from Guardians of the Galaxy The Questions: 1. When faced with a challenge, you are most likely to: A. Strategize and lead a team to overcome it. B. Use your unique skills and creativity to find a solution. C. Push the boundaries and take a big risk to get what you want. 2. Your ideal Friday night is: A. Planning your next big project or adventure. B. Creating something new, whether it's art, music, or a good story. C. Attending a huge party and making connections. 3. What is your go-to style? A. Practical and rugged. You value function over form. B. Casual and expressive. You like to show off your personality. C. Sleek and expensive. You like to make a statement. 4. How do you handle emotional situations? A. You keep your emotions in check and focus on the task at hand. B. You are sensitive and open, often expressing yourself through creative outlets. C. You use charm and charisma to navigate social situations and get your way. 5. What drives you in life? A. A strong sense of purpose and a desire for justice. B. A passion for your craft and a hope for a brighter future. C. Ambition, money, and the thrill of the chase. 6. Pick a destination for a weekend getaway: A. A desert road trip in a powerful vehicle. B. A bustling city with a vibrant arts and culture scene. C. A luxurious party on a yacht.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var AnswerButton = Container.expand(function () { var self = Container.call(this); var buttonBg = self.attachAsset('answerButton', { anchorX: 0.5, anchorY: 0.5 }); var buttonHover = self.attachAsset('answerButtonHover', { anchorX: 0.5, anchorY: 0.5 }); buttonHover.visible = false; self.answerText = new Text2('', { size: 64, fill: '#ffffff' }); self.answerText.anchor.set(0.5, 0.5); self.addChild(self.answerText); self.answerIndex = 0; self.isSelected = false; self.setText = function (text) { self.answerText.setText(text); }; self.setHover = function (hover) { buttonHover.visible = hover; buttonBg.visible = !hover; }; self.down = function (x, y, obj) { if (!self.isSelected) { LK.getSound('buttonClick').play(); self.isSelected = true; selectAnswer(self.answerIndex); } }; return self; }); var QuestionCard = Container.expand(function () { var self = Container.call(this); var cardBg = self.attachAsset('questionCard', { anchorX: 0.5, anchorY: 0.5 }); self.questionText = new Text2('', { size: 80, fill: '#ffffff' }); self.questionText.anchor.set(0.5, 0.5); self.addChild(self.questionText); self.setQuestion = function (text) { self.questionText.setText(text); }; return self; }); var ResultScreen = Container.expand(function () { var self = Container.call(this); var resultBg = self.attachAsset('resultCard', { anchorX: 0.5, anchorY: 0.5 }); var portrait = self.attachAsset('characterPortrait', { anchorX: 0.5, anchorY: 0.5, y: -300 }); self.characterName = new Text2('', { size: 100, fill: '#ffffff' }); self.characterName.anchor.set(0.5, 0.5); self.characterName.y = -100; self.addChild(self.characterName); self.matchPercentage = new Text2('', { size: 80, fill: '#f39c12' }); self.matchPercentage.anchor.set(0.5, 0.5); self.matchPercentage.y = -20; self.addChild(self.matchPercentage); self.description = new Text2('', { size: 56, fill: '#ecf0f1' }); self.description.anchor.set(0.5, 0.5); self.description.y = 100; self.addChild(self.description); self.retryButton = new Text2('Take Quiz Again', { size: 70, fill: '#3498db' }); self.retryButton.anchor.set(0.5, 0.5); self.retryButton.y = 300; self.addChild(self.retryButton); self.setResult = function (character, percentage) { self.characterName.setText(character.name); self.matchPercentage.setText(percentage + '% Match'); self.description.setText(character.description); portrait.tint = character.color; }; self.down = function (x, y, obj) { // Use the x, y coordinates directly instead of converting through parent if (Math.abs(x) < 200 && Math.abs(y - 300) < 30) { LK.getSound('buttonClick').play(); restartQuiz(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1a252f }); /**** * Game Code ****/ var questions = [{ text: "What's your ideal weekend activity?", answers: ["Reading a book at home", "Going on an adventure outdoors", "Partying with friends"] }, { text: "How do you handle stress?", answers: ["Stay calm and think it through", "Take action immediately", "Talk to others about it"] }, { text: "What motivates you most?", answers: ["Personal growth and learning", "Helping others and making a difference", "Success and recognition"] }, { text: "Your biggest strength is:", answers: ["Intelligence and creativity", "Courage and determination", "Charisma and social skills"] }, { text: "In a group project, you are:", answers: ["The strategic planner", "The leader who takes charge", "The motivator who keeps spirits up"] }, { text: "What's most important to you?", answers: ["Freedom and independence", "Justice and doing what's right", "Love and relationships"] }]; var characters = [{ name: "Imperator Furiosa", description: "Strong, determined warrior who fights for justice", color: 0xff6b35, traits: [1, 1, 1, 1, 1, 1] // Answer B for all questions }, { name: "Miles Morales", description: "Young hero balancing responsibility with personal growth", color: 0xff0040, traits: [1, 0, 1, 1, 1, 1] // Mix of A and B answers }, { name: "Jordan Belfort", description: "Ambitious and charismatic, driven by success", color: 0x00ff87, traits: [2, 2, 2, 2, 2, 0] // Mostly C answers }, { name: "Mia Dolan", description: "Creative dreamer pursuing artistic passions", color: 0x9b59b6, traits: [0, 0, 0, 0, 0, 2] // Mix of A and C answers }, { name: "Elsa", description: "Independent spirit learning to embrace her uniqueness", color: 0x3498db, traits: [0, 0, 0, 0, 2, 0] // Mostly A answers }, { name: "Peter Quill", description: "Fun-loving adventurer with a good heart", color: 0xf39c12, traits: [1, 2, 1, 2, 2, 2] // Mix of B and C answers }]; var currentQuestion = 0; var answers = []; var gameState = 'quiz'; // 'quiz' or 'results' var titleText = new Text2('Which Movie Character Are You?', { size: 120, fill: '#ffffff' }); titleText.anchor.set(0.5, 0.5); titleText.x = 1024; titleText.y = 300; game.addChild(titleText); var progressBg = game.addChild(LK.getAsset('progressBg', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 500 })); var progressBar = game.addChild(LK.getAsset('progressBar', { anchorX: 0, anchorY: 0.5, x: 224, y: 500, scaleX: 0 })); var questionCard = game.addChild(new QuestionCard()); questionCard.x = 1024; questionCard.y = 800; var answerButtons = []; for (var i = 0; i < 3; i++) { var button = new AnswerButton(); button.x = 1024; button.y = 1200 + i * 180; button.answerIndex = i; answerButtons.push(button); game.addChild(button); } var resultScreen = null; function updateQuestion() { if (currentQuestion < questions.length) { var question = questions[currentQuestion]; questionCard.setQuestion(question.text); for (var i = 0; i < 3; i++) { answerButtons[i].setText(question.answers[i]); answerButtons[i].isSelected = false; answerButtons[i].setHover(false); } var progress = currentQuestion / questions.length; tween(progressBar.scale, { x: progress }, { duration: 300 }); } else { showResults(); } } function selectAnswer(answerIndex) { answers.push(answerIndex); LK.setTimeout(function () { currentQuestion++; updateQuestion(); }, 500); } function calculateResult() { var scores = []; for (var i = 0; i < characters.length; i++) { scores[i] = 0; for (var j = 0; j < answers.length; j++) { if (characters[i].traits[j] === answers[j]) { scores[i]++; } } } var bestMatch = 0; var bestScore = scores[0]; for (var k = 1; k < scores.length; k++) { if (scores[k] > bestScore) { bestScore = scores[k]; bestMatch = k; } } var percentage = Math.round(bestScore / questions.length * 100); return { character: characters[bestMatch], percentage: percentage }; } function showResults() { gameState = 'results'; LK.getSound('resultReveal').play(); // Hide quiz elements titleText.visible = false; progressBg.visible = false; progressBar.visible = false; questionCard.visible = false; for (var i = 0; i < answerButtons.length; i++) { answerButtons[i].visible = false; } // Show results var result = calculateResult(); resultScreen = new ResultScreen(); resultScreen.x = 1024; resultScreen.y = 1366; resultScreen.setResult(result.character, result.percentage); game.addChild(resultScreen); LK.setScore(result.percentage); } function restartQuiz() { gameState = 'quiz'; currentQuestion = 0; answers = []; // Show quiz elements titleText.visible = true; progressBg.visible = true; progressBar.visible = true; questionCard.visible = true; for (var i = 0; i < answerButtons.length; i++) { answerButtons[i].visible = true; } // Hide results if (resultScreen) { resultScreen.destroy(); resultScreen = null; } // Reset progress progressBar.scale.x = 0; updateQuestion(); } // Initialize first question updateQuestion(); game.update = function () { // Handle hover effects for answer buttons if (gameState === 'quiz') { // Reset all hover states for (var i = 0; i < answerButtons.length; i++) { answerButtons[i].setHover(false); } } };
===================================================================
--- original.js
+++ change.js
@@ -103,10 +103,10 @@
self.description.setText(character.description);
portrait.tint = character.color;
};
self.down = function (x, y, obj) {
- var localPos = self.toLocal(obj.parent.toGlobal(obj.position));
- if (Math.abs(localPos.x) < 200 && Math.abs(localPos.y - 300) < 30) {
+ // Use the x, y coordinates directly instead of converting through parent
+ if (Math.abs(x) < 200 && Math.abs(y - 300) < 30) {
LK.getSound('buttonClick').play();
restartQuiz();
}
};