/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Problem = Container.expand(function (level) {
var self = Container.call(this);
var problemBg = self.attachAsset('problemBackground', {
anchorX: 0.5,
anchorY: 0.5
});
problemBg.alpha = 0.9;
self.level = level;
self.problemData = generateProblem(level);
var problemText = new Text2(self.problemData.question, {
size: 80,
fill: 0x000000
});
problemText.anchor.set(0.5, 0.5);
self.addChild(problemText);
return self;
});
var SymbolButton = Container.expand(function (symbol, isCorrect) {
var self = Container.call(this);
var buttonBg = self.attachAsset('symbolButton', {
anchorX: 0.5,
anchorY: 0.5
});
var symbolText = new Text2(symbol, {
size: 60,
fill: 0xFFFFFF
});
symbolText.anchor.set(0.5, 0.5);
self.addChild(symbolText);
self.symbol = symbol;
self.isCorrect = isCorrect;
self.isPressed = false;
self.down = function (x, y, obj) {
if (gameState !== 'playing') return;
self.isPressed = true;
buttonBg.removeChild();
var pressedBg = self.attachAsset('symbolButtonPressed', {
anchorX: 0.5,
anchorY: 0.5
});
if (self.isCorrect) {
handleCorrectAnswer();
} else {
handleIncorrectAnswer();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var gameState = 'playing';
var currentLevel = 1;
var score = 0;
var currentProblem = null;
var symbolButtons = [];
// Generate random numbers using digits 0-9
function generateRandomNumber(minDigits, maxDigits) {
var digits = Math.floor(Math.random() * (maxDigits - minDigits + 1)) + minDigits;
var number = 0;
for (var i = 0; i < digits; i++) {
var digit = Math.floor(Math.random() * 10); // 0-9
if (i === 0 && digits > 1 && digit === 0) {
digit = Math.floor(Math.random() * 9) + 1; // First digit can't be 0 for multi-digit numbers
}
number = number * 10 + digit;
}
return number;
}
// Mathematical symbols and operators
var mathSymbols = {
basic: ['+', '-', '×', '÷'],
comparison: ['=', '≠', '<', '>', '≤', '≥'],
advanced: ['±', '∓', '^', '√', '%', '∞', '≈', '≡'],
geometry: ['∠', '∟', '∥', '∦', '⟂', '≅', '~', '△', '°'],
logic: ['∧', '∨', '¬', '∀', '∃', '⇒', '⇔'],
sets: ['∈', '∉', '⊆', '⊂', '⊄', '⊇', '⊃', 'Ø', '∩'],
calculus: ['∫', '∬', '∭', '∮', '∇', 'Σ', '∏'],
numbers: ['N', 'Z', 'Q', 'R', 'C', 'π', 'ε']
};
// Generate random mathematical problems
function generateArithmeticProblem(level) {
var operations = ['+', '-', '×', '÷'];
var operation = operations[Math.floor(Math.random() * operations.length)];
var maxSize = Math.min(level + 1, 4); // Increase number size with level
var a = generateRandomNumber(1, maxSize);
var b = generateRandomNumber(1, maxSize);
// Ensure division results in whole numbers
if (operation === '÷') {
b = Math.max(1, b);
a = b * generateRandomNumber(1, 2); // Make a divisible by b
}
// Ensure subtraction doesn't go negative
if (operation === '-' && b > a) {
var temp = a;
a = b;
b = temp;
}
var correctAnswer;
switch (operation) {
case '+':
correctAnswer = a + b;
break;
case '-':
correctAnswer = a - b;
break;
case '×':
correctAnswer = a * b;
break;
case '÷':
correctAnswer = a / b;
break;
}
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: a + " " + operation + " " + b + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
}
function generateComparisonProblem(level) {
var a = generateRandomNumber(1, Math.min(level + 1, 3));
var b = generateRandomNumber(1, Math.min(level + 1, 3));
var correctSymbol;
if (a > b) correctSymbol = '>';else if (a < b) correctSymbol = '<';else correctSymbol = '=';
var symbols = ['>', '<', '=', '≠'];
var wrongSymbols = symbols.filter(function (s) {
return s !== correctSymbol;
});
var allAnswers = [correctSymbol].concat(wrongSymbols);
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: a + " ? " + b,
answers: allAnswers,
correct: allAnswers.indexOf(correctSymbol)
};
}
function generateAdvancedProblem(level) {
var problemType = Math.floor(Math.random() * 3);
if (problemType === 0) {
// Powers
var base = generateRandomNumber(1, 1) + 1; // 2-9
var exponent = generateRandomNumber(1, 1) + 1; // 2-9
var correctAnswer = Math.pow(base, exponent);
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: base + "^" + exponent + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
} else if (problemType === 1) {
// Square roots
var perfectSquares = [4, 9, 16, 25, 36, 49, 64, 81, 100];
var square = perfectSquares[Math.floor(Math.random() * perfectSquares.length)];
var correctAnswer = Math.sqrt(square);
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 6) - 3);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 3) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 6) - 3);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 3) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: "√" + square + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
} else {
// Percentages
var whole = generateRandomNumber(2, 3);
var percent = [10, 20, 25, 50, 75][Math.floor(Math.random() * 5)];
var correctAnswer = whole * percent / 100;
if (correctAnswer !== Math.floor(correctAnswer)) {
correctAnswer = correctAnswer.toFixed(1);
}
correctAnswer = correctAnswer.toString();
// Generate three random wrong answers
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = whole * (percent + (Math.floor(Math.random() * 20) - 10)) / 100;
if (wrong <= 0) wrong = whole * (percent + Math.floor(Math.random() * 10) + 5) / 100;
if (wrong !== Math.floor(wrong)) {
wrong = wrong.toFixed(1);
}
wrong = wrong.toString();
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = whole * (percent + (Math.floor(Math.random() * 20) - 10)) / 100;
if (wrong <= 0) wrong = whole * (percent + Math.floor(Math.random() * 10) + 5) / 100;
if (wrong !== Math.floor(wrong)) {
wrong = wrong.toFixed(1);
}
wrong = wrong.toString();
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer].concat(wrongAnswers);
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: percent + "% of " + whole + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer)
};
}
}
function generateProblem(level) {
if (level <= 2) {
return generateArithmeticProblem(level);
} else if (level <= 4) {
return Math.random() < 0.7 ? generateArithmeticProblem(level) : generateComparisonProblem(level);
} else {
var rand = Math.random();
if (rand < 0.5) return generateArithmeticProblem(level);else if (rand < 0.8) return generateComparisonProblem(level);else return generateAdvancedProblem(level);
}
}
function createNewProblem() {
if (currentProblem) {
currentProblem.destroy();
}
// Clear existing symbol buttons
for (var i = 0; i < symbolButtons.length; i++) {
symbolButtons[i].destroy();
}
symbolButtons = [];
currentProblem = game.addChild(new Problem(currentLevel));
currentProblem.x = 1024;
currentProblem.y = 800;
// Create symbol buttons
var problemData = currentProblem.problemData;
var buttonY = 1400;
var buttonSpacing = 200;
var startX = 1024 - buttonSpacing * 1.5;
for (var i = 0; i < problemData.answers.length; i++) {
var button = game.addChild(new SymbolButton(problemData.answers[i], i === problemData.correct));
button.x = startX + i * buttonSpacing;
button.y = buttonY;
symbolButtons.push(button);
}
}
function handleCorrectAnswer() {
gameState = 'answering';
LK.getSound('correct').play();
// Flash green
var flash = game.addChild(LK.getAsset('correctFlash', {
alpha: 0.3
}));
tween(flash, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
flash.destroy();
}
});
// Update score
score += 4;
LK.setScore(score);
updateScoreDisplay();
// Advance level every 10 correct answers
if (score % 40 === 0) {
currentLevel++;
}
// Create new problem after delay
LK.setTimeout(function () {
gameState = 'playing';
createNewProblem();
}, 1000);
}
function handleIncorrectAnswer() {
gameState = 'answering';
LK.getSound('incorrect').play();
// Flash red
var flash = game.addChild(LK.getAsset('incorrectFlash', {
alpha: 0.3
}));
tween(flash, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
flash.destroy();
}
});
// Show correct answer
showCorrectAnswer();
// Game over after delay
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
function showCorrectAnswer() {
var correctText = new Text2("Correct answer: " + currentProblem.problemData.answers[currentProblem.problemData.correct], {
size: 60,
fill: 0xFFFFFF
});
correctText.anchor.set(0.5, 0.5);
correctText.x = 1024;
correctText.y = 1800;
game.addChild(correctText);
}
// UI Setup
var scoreText = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var levelText = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelText.anchor.set(1, 0);
LK.gui.topRight.addChild(levelText);
function updateScoreDisplay() {
scoreText.setText('Score: ' + score);
levelText.setText('Level: ' + currentLevel);
}
// Initialize first problem
createNewProblem();
game.update = function () {
if (gameState !== 'playing') return;
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Problem = Container.expand(function (level) {
var self = Container.call(this);
var problemBg = self.attachAsset('problemBackground', {
anchorX: 0.5,
anchorY: 0.5
});
problemBg.alpha = 0.9;
self.level = level;
self.problemData = generateProblem(level);
var problemText = new Text2(self.problemData.question, {
size: 80,
fill: 0x000000
});
problemText.anchor.set(0.5, 0.5);
self.addChild(problemText);
return self;
});
var SymbolButton = Container.expand(function (symbol, isCorrect) {
var self = Container.call(this);
var buttonBg = self.attachAsset('symbolButton', {
anchorX: 0.5,
anchorY: 0.5
});
var symbolText = new Text2(symbol, {
size: 60,
fill: 0xFFFFFF
});
symbolText.anchor.set(0.5, 0.5);
self.addChild(symbolText);
self.symbol = symbol;
self.isCorrect = isCorrect;
self.isPressed = false;
self.down = function (x, y, obj) {
if (gameState !== 'playing') return;
self.isPressed = true;
buttonBg.removeChild();
var pressedBg = self.attachAsset('symbolButtonPressed', {
anchorX: 0.5,
anchorY: 0.5
});
if (self.isCorrect) {
handleCorrectAnswer();
} else {
handleIncorrectAnswer();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var gameState = 'playing';
var currentLevel = 1;
var score = 0;
var currentProblem = null;
var symbolButtons = [];
// Generate random numbers using digits 0-9
function generateRandomNumber(minDigits, maxDigits) {
var digits = Math.floor(Math.random() * (maxDigits - minDigits + 1)) + minDigits;
var number = 0;
for (var i = 0; i < digits; i++) {
var digit = Math.floor(Math.random() * 10); // 0-9
if (i === 0 && digits > 1 && digit === 0) {
digit = Math.floor(Math.random() * 9) + 1; // First digit can't be 0 for multi-digit numbers
}
number = number * 10 + digit;
}
return number;
}
// Mathematical symbols and operators
var mathSymbols = {
basic: ['+', '-', '×', '÷'],
comparison: ['=', '≠', '<', '>', '≤', '≥'],
advanced: ['±', '∓', '^', '√', '%', '∞', '≈', '≡'],
geometry: ['∠', '∟', '∥', '∦', '⟂', '≅', '~', '△', '°'],
logic: ['∧', '∨', '¬', '∀', '∃', '⇒', '⇔'],
sets: ['∈', '∉', '⊆', '⊂', '⊄', '⊇', '⊃', 'Ø', '∩'],
calculus: ['∫', '∬', '∭', '∮', '∇', 'Σ', '∏'],
numbers: ['N', 'Z', 'Q', 'R', 'C', 'π', 'ε']
};
// Generate random mathematical problems
function generateArithmeticProblem(level) {
var operations = ['+', '-', '×', '÷'];
var operation = operations[Math.floor(Math.random() * operations.length)];
var maxSize = Math.min(level + 1, 4); // Increase number size with level
var a = generateRandomNumber(1, maxSize);
var b = generateRandomNumber(1, maxSize);
// Ensure division results in whole numbers
if (operation === '÷') {
b = Math.max(1, b);
a = b * generateRandomNumber(1, 2); // Make a divisible by b
}
// Ensure subtraction doesn't go negative
if (operation === '-' && b > a) {
var temp = a;
a = b;
b = temp;
}
var correctAnswer;
switch (operation) {
case '+':
correctAnswer = a + b;
break;
case '-':
correctAnswer = a - b;
break;
case '×':
correctAnswer = a * b;
break;
case '÷':
correctAnswer = a / b;
break;
}
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: a + " " + operation + " " + b + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
}
function generateComparisonProblem(level) {
var a = generateRandomNumber(1, Math.min(level + 1, 3));
var b = generateRandomNumber(1, Math.min(level + 1, 3));
var correctSymbol;
if (a > b) correctSymbol = '>';else if (a < b) correctSymbol = '<';else correctSymbol = '=';
var symbols = ['>', '<', '=', '≠'];
var wrongSymbols = symbols.filter(function (s) {
return s !== correctSymbol;
});
var allAnswers = [correctSymbol].concat(wrongSymbols);
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: a + " ? " + b,
answers: allAnswers,
correct: allAnswers.indexOf(correctSymbol)
};
}
function generateAdvancedProblem(level) {
var problemType = Math.floor(Math.random() * 3);
if (problemType === 0) {
// Powers
var base = generateRandomNumber(1, 1) + 1; // 2-9
var exponent = generateRandomNumber(1, 1) + 1; // 2-9
var correctAnswer = Math.pow(base, exponent);
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 10) - 5);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 5) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: base + "^" + exponent + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
} else if (problemType === 1) {
// Square roots
var perfectSquares = [4, 9, 16, 25, 36, 49, 64, 81, 100];
var square = perfectSquares[Math.floor(Math.random() * perfectSquares.length)];
var correctAnswer = Math.sqrt(square);
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = correctAnswer + (Math.floor(Math.random() * 6) - 3);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 3) + 1;
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = correctAnswer + (Math.floor(Math.random() * 6) - 3);
if (wrong <= 0) wrong = correctAnswer + Math.floor(Math.random() * 3) + 1;
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer.toString()].concat(wrongAnswers.map(function (x) {
return x.toString();
}));
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: "√" + square + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer.toString())
};
} else {
// Percentages
var whole = generateRandomNumber(2, 3);
var percent = [10, 20, 25, 50, 75][Math.floor(Math.random() * 5)];
var correctAnswer = whole * percent / 100;
if (correctAnswer !== Math.floor(correctAnswer)) {
correctAnswer = correctAnswer.toFixed(1);
}
correctAnswer = correctAnswer.toString();
// Generate three random wrong answers
var wrongAnswers = [];
for (var i = 0; i < 3; i++) {
var wrong = whole * (percent + (Math.floor(Math.random() * 20) - 10)) / 100;
if (wrong <= 0) wrong = whole * (percent + Math.floor(Math.random() * 10) + 5) / 100;
if (wrong !== Math.floor(wrong)) {
wrong = wrong.toFixed(1);
}
wrong = wrong.toString();
while (wrongAnswers.indexOf(wrong) !== -1 || wrong === correctAnswer) {
wrong = whole * (percent + (Math.floor(Math.random() * 20) - 10)) / 100;
if (wrong <= 0) wrong = whole * (percent + Math.floor(Math.random() * 10) + 5) / 100;
if (wrong !== Math.floor(wrong)) {
wrong = wrong.toFixed(1);
}
wrong = wrong.toString();
}
wrongAnswers.push(wrong);
}
var allAnswers = [correctAnswer].concat(wrongAnswers);
// Shuffle answers
for (var i = allAnswers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = allAnswers[i];
allAnswers[i] = allAnswers[j];
allAnswers[j] = temp;
}
return {
question: percent + "% of " + whole + " = ?",
answers: allAnswers,
correct: allAnswers.indexOf(correctAnswer)
};
}
}
function generateProblem(level) {
if (level <= 2) {
return generateArithmeticProblem(level);
} else if (level <= 4) {
return Math.random() < 0.7 ? generateArithmeticProblem(level) : generateComparisonProblem(level);
} else {
var rand = Math.random();
if (rand < 0.5) return generateArithmeticProblem(level);else if (rand < 0.8) return generateComparisonProblem(level);else return generateAdvancedProblem(level);
}
}
function createNewProblem() {
if (currentProblem) {
currentProblem.destroy();
}
// Clear existing symbol buttons
for (var i = 0; i < symbolButtons.length; i++) {
symbolButtons[i].destroy();
}
symbolButtons = [];
currentProblem = game.addChild(new Problem(currentLevel));
currentProblem.x = 1024;
currentProblem.y = 800;
// Create symbol buttons
var problemData = currentProblem.problemData;
var buttonY = 1400;
var buttonSpacing = 200;
var startX = 1024 - buttonSpacing * 1.5;
for (var i = 0; i < problemData.answers.length; i++) {
var button = game.addChild(new SymbolButton(problemData.answers[i], i === problemData.correct));
button.x = startX + i * buttonSpacing;
button.y = buttonY;
symbolButtons.push(button);
}
}
function handleCorrectAnswer() {
gameState = 'answering';
LK.getSound('correct').play();
// Flash green
var flash = game.addChild(LK.getAsset('correctFlash', {
alpha: 0.3
}));
tween(flash, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
flash.destroy();
}
});
// Update score
score += 4;
LK.setScore(score);
updateScoreDisplay();
// Advance level every 10 correct answers
if (score % 40 === 0) {
currentLevel++;
}
// Create new problem after delay
LK.setTimeout(function () {
gameState = 'playing';
createNewProblem();
}, 1000);
}
function handleIncorrectAnswer() {
gameState = 'answering';
LK.getSound('incorrect').play();
// Flash red
var flash = game.addChild(LK.getAsset('incorrectFlash', {
alpha: 0.3
}));
tween(flash, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
flash.destroy();
}
});
// Show correct answer
showCorrectAnswer();
// Game over after delay
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
function showCorrectAnswer() {
var correctText = new Text2("Correct answer: " + currentProblem.problemData.answers[currentProblem.problemData.correct], {
size: 60,
fill: 0xFFFFFF
});
correctText.anchor.set(0.5, 0.5);
correctText.x = 1024;
correctText.y = 1800;
game.addChild(correctText);
}
// UI Setup
var scoreText = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var levelText = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelText.anchor.set(1, 0);
LK.gui.topRight.addChild(levelText);
function updateScoreDisplay() {
scoreText.setText('Score: ' + score);
levelText.setText('Level: ' + currentLevel);
}
// Initialize first problem
createNewProblem();
game.update = function () {
if (gameState !== 'playing') return;
};