User prompt
update the game
User prompt
add 10 questions
User prompt
upgrade game logic
User prompt
fix it
Code edit (1 edits merged)
Please save this source code
User prompt
add more questions
User prompt
shuffle the answers
User prompt
top text for the question doesnt dissappear when i answer
User prompt
Please fix the bug: 'Timeout.tick error: option.destroy is not a function' in or related to this line: 'option.destroy();' Line Number: 76
User prompt
add hitboxes for the all the answers
User prompt
scramble the questions more
User prompt
questions light red when i get it correct
User prompt
remove the previous question text and i mean all the text from the previous question so it only shows the new questions
/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on usage in the game code. // Question class for displaying trivia questions and handling user interaction var Question = Container.expand(function () { var self = Container.call(this); questionText = new Text2('', { size: 100, fill: "#ffffff" }); questionText.anchor.set(0.5, 0); questionText.x = 1024; // Center horizontally questionText.y = 200; // Position from top self.addChild(questionText); var answerOptions = []; // Array to hold answer option assets // Initialize question and answers self.init = function (question, answers) { questionText.setText(''); // Clear previous question text self.cleanup(); // Clean up previous question and answers // Clear previous question text before setting new question questionText.setText(question); // Set new question text // Create and position answer options answers.forEach(function (answer, index) { answerOption = new Text2(answer.text, { size: 100, fill: "#ffffff" }); answerOption.anchor.set(0.5, 0.5); answerOption.x = 1024; answerOption.y = 600 + index * 200; answerOption.width = 1500; answerOption.height = 150; self.addChild(answerOption); answerOption.data = answer; // Attach answer data to the option answerOptions.push(answerOption); // Handle answer selection answerOption.down = function (x, y, obj) { self.checkAnswer(answerOption.data.isCorrect); }; }); }; // Check if the selected answer is correct self.checkAnswer = function (isCorrect) { if (isCorrect) { LK.effects.flashObject(self, 0x00ff00, 500); // Flash green for correct answer LK.setScore(LK.getScore() + 1); // Increase score // Moved cleanup to the beginning of displayNextQuestion to ensure the previous question is cleared before displaying the next one displayNextQuestion(); // Proceed to the next question } else { LK.effects.flashObject(self, 0xff0000, 500); // Flash red for incorrect answer // Provide feedback for incorrect answer and wait before moving to the next question LK.setTimeout(function () { self.cleanup(); // Clean up the current question displayNextQuestion(); // Proceed to the next question even if the answer is incorrect }, 1000); // Wait for 1 second to allow the player to see the feedback } // Prepare for next question or end game }; // Clean up for the next question self.cleanup = function () { answerOptions.forEach(function (option) { option.destroy(); }); answerOptions = []; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000, // Init game with black background zIndex: -1 }); /**** * Game Code ****/ var questions = [{ question: "What's Taylor Swift's debut album name?", answers: [{ text: "Taylor Swift", isCorrect: true }, { text: "Fearless", isCorrect: false }, { text: "Speak Now", isCorrect: false }, { text: "Red", isCorrect: false }] }, { question: "What is Taylor Swift's birth date?", answers: [{ text: "December 13, 1989", isCorrect: true }, { text: "January 31, 1989", isCorrect: false }, { text: "November 23, 1989", isCorrect: false }, { text: "October 10, 1989", isCorrect: false }] }, { question: "What is the name of Taylor Swift's first single?", answers: [{ text: "Tim McGraw", isCorrect: true }, { text: "Love Story", isCorrect: false }, { text: "You Belong with Me", isCorrect: false }, { text: "Our Song", isCorrect: false }] }]; var currentQuestionIndex = 0; var scoreTxt; function displayNextQuestion() { if (currentQuestionIndex < questions.length) { var questionData = questions[currentQuestionIndex]; var question = game.addChild(new Question()); question.init(questionData.question, questionData.answers); currentQuestionIndex++; } else { // No more questions, show game over LK.showGameOver(); } } function setupGame() { // Display score scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = 1024; // Center horizontally scoreTxt.y = 50; // Position from top LK.gui.top.addChild(scoreTxt); // Start with the first question displayNextQuestion(); } // Update score display game.update = function () { scoreTxt.setText('Score: ' + LK.getScore()); }; setupGame();
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on usage in the game code.
// Question class for displaying trivia questions and handling user interaction
var Question = Container.expand(function () {
var self = Container.call(this);
questionText = new Text2('', {
size: 100,
fill: "#ffffff"
});
questionText.anchor.set(0.5, 0);
questionText.x = 1024; // Center horizontally
questionText.y = 200; // Position from top
self.addChild(questionText);
var answerOptions = []; // Array to hold answer option assets
// Initialize question and answers
self.init = function (question, answers) {
questionText.setText(''); // Clear previous question text
self.cleanup(); // Clean up previous question and answers
// Clear previous question text before setting new question
questionText.setText(question); // Set new question text
// Create and position answer options
answers.forEach(function (answer, index) {
answerOption = new Text2(answer.text, {
size: 100,
fill: "#ffffff"
});
answerOption.anchor.set(0.5, 0.5);
answerOption.x = 1024;
answerOption.y = 600 + index * 200;
answerOption.width = 1500;
answerOption.height = 150;
self.addChild(answerOption);
answerOption.data = answer; // Attach answer data to the option
answerOptions.push(answerOption);
// Handle answer selection
answerOption.down = function (x, y, obj) {
self.checkAnswer(answerOption.data.isCorrect);
};
});
};
// Check if the selected answer is correct
self.checkAnswer = function (isCorrect) {
if (isCorrect) {
LK.effects.flashObject(self, 0x00ff00, 500); // Flash green for correct answer
LK.setScore(LK.getScore() + 1); // Increase score
// Moved cleanup to the beginning of displayNextQuestion to ensure the previous question is cleared before displaying the next one
displayNextQuestion(); // Proceed to the next question
} else {
LK.effects.flashObject(self, 0xff0000, 500); // Flash red for incorrect answer
// Provide feedback for incorrect answer and wait before moving to the next question
LK.setTimeout(function () {
self.cleanup(); // Clean up the current question
displayNextQuestion(); // Proceed to the next question even if the answer is incorrect
}, 1000); // Wait for 1 second to allow the player to see the feedback
}
// Prepare for next question or end game
};
// Clean up for the next question
self.cleanup = function () {
answerOptions.forEach(function (option) {
option.destroy();
});
answerOptions = [];
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
// Init game with black background
zIndex: -1
});
/****
* Game Code
****/
var questions = [{
question: "What's Taylor Swift's debut album name?",
answers: [{
text: "Taylor Swift",
isCorrect: true
}, {
text: "Fearless",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}, {
text: "Red",
isCorrect: false
}]
}, {
question: "What is Taylor Swift's birth date?",
answers: [{
text: "December 13, 1989",
isCorrect: true
}, {
text: "January 31, 1989",
isCorrect: false
}, {
text: "November 23, 1989",
isCorrect: false
}, {
text: "October 10, 1989",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's first single?",
answers: [{
text: "Tim McGraw",
isCorrect: true
}, {
text: "Love Story",
isCorrect: false
}, {
text: "You Belong with Me",
isCorrect: false
}, {
text: "Our Song",
isCorrect: false
}]
}];
var currentQuestionIndex = 0;
var scoreTxt;
function displayNextQuestion() {
if (currentQuestionIndex < questions.length) {
var questionData = questions[currentQuestionIndex];
var question = game.addChild(new Question());
question.init(questionData.question, questionData.answers);
currentQuestionIndex++;
} else {
// No more questions, show game over
LK.showGameOver();
}
}
function setupGame() {
// Display score
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 1024; // Center horizontally
scoreTxt.y = 50; // Position from top
LK.gui.top.addChild(scoreTxt);
// Start with the first question
displayNextQuestion();
}
// Update score display
game.update = function () {
scoreTxt.setText('Score: ' + LK.getScore());
};
setupGame();