Code edit (21 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'anchor')' in or related to this line: 'levelText.anchor.x = 0.5;' Line Number: 125
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
var BlackboardPlaceholder = Container.expand(function () {
var self = Container.call(this);
var bpGraphics = self.attachAsset('blackboardPlaceholder', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Cat = Container.expand(function () {
var self = Container.call(this);
var catGraphics = self.attachAsset('cat', {
anchorX: 0.5,
anchorY: 0.5
});
self.count = 0;
self.down = function (x, y, obj) {
//gameWon();
LK.getSound('meow').play();
self.update = function () {
if (self.y > 1550) {
self.y -= 10;
}
self.count++;
if (self.count > 90) {
self.y = 1670;
self.count = 0;
self.update = null;
}
};
};
return self;
});
var LetterOnBlackboard = Container.expand(function () {
var self = Container.call(this);
self.letter = '';
var blackboardText = new Text2(self.letter, {
size: 120,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10
});
blackboardText.anchor.set(0.5, 0.5);
self.addChild(blackboardText);
self.setLetter = function (letter) {
self.letter = letter;
blackboardText.setText(letter);
};
return self;
});
//<Assets used in the game will automatically appear here>
// Class for Letter Tiles
var LetterTile = Container.expand(function () {
var self = Container.call(this);
self.letter = '';
var tileGraphics = self.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
var letterText = new Text2(self.letter, {
size: 150,
fill: "#000000",
weight: 300
});
letterText.anchor.set(0.5, 0.5);
self.addChild(letterText);
self.setLetter = function (letter) {
self.letter = letter;
letterText.setText(letter);
};
self.clickAddRemove = true;
self.clickable = true;
self.down = function (x, y, obj) {
if (self.clickAddRemove) {
LK.getSound('tap').play();
writeLetterOnBlackboard(self.letter);
currentWord += self.letter;
self.alpha = 0.5;
self.clickAddRemove = false; // Make it unclickable
} else {
LK.getSound('tap').play();
//writeLetterOnBlackboard(self.letter);
if (removeLastLetterOnBlackBoard(self.letter)) {
self.alpha = 1;
self.clickAddRemove = true;
}
//currentWord += self.letter;
}
};
return self;
});
var LevelText = Container.expand(function () {
var self = Container.call(this);
var levelTxt = new Text2('Level 1', {
size: 100,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
weight: 800,
anchorX: 0.5
});
//levelTxt.x = 2048 / 2;
//levelTxt.y = 200;
self.addChild(levelTxt);
return self;
});
var NextLevelBackground = Container.expand(function () {
var self = Container.call(this);
var bgGrpahics = self.attachAsset('nextLevelBg', {
anchorX: 0,
anchorY: 0.5,
alpha: 0.8
});
return self;
});
var Rabbit = Container.expand(function () {
var self = Container.call(this);
var rabbitGraphics = self.attachAsset('rabbit', {
anchorX: 0.5,
anchorY: 0.5
});
self.count = 0;
self.down = function (x, y, obj) {
LK.getSound('rabbitSound').play();
self.update = function () {
if (self.y > 1550) {
self.y -= 10;
}
self.count++;
if (self.count > 90) {
self.y = 1700;
self.count = 0;
self.update = null;
}
};
};
return self;
});
var SubmitButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('submitButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2('Submit', {
size: 100,
fill: "#000000"
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
//console.log("Submit button clicked: ", currentWord);
// Add logic to handle submit action
if (levels[currentLevel].words.includes(currentWord)) {
//LK.getSound('correctWord').play();
// Remove the correct word from the levels[currentLevel].words array
levels[currentLevel].words = levels[currentLevel].words.filter(function (word) {
return word !== currentWord;
});
//console.log('correct');
var scoreIncrement = currentWord.length;
scoreIncrement *= streakMult;
streak += 1;
updateStreakText();
LK.setScore(LK.getScore() + scoreIncrement);
scoreTxt.setText('Score: ' + LK.getScore());
// Create star explosion effect
createStarExplosion(500, 400 + blackboardOffsetY);
createScoreNotice(500, 400 + blackboardOffsetY, scoreIncrement);
blackBoardArr = [];
blackboardOffsetX = 0;
blackboardOffsetY += 120;
game.children.forEach(function (child) {
if (child instanceof LetterTile) {
child.alpha = 1;
child.clickable = true;
child.clickAddRemove = true;
}
});
currentWord = "";
//console.log('length: ', levels[currentLevel].words.length);
if (levels[currentLevel].words.length == 0) {
LK.getSound('nextLevel').play();
advanceToNextLevel();
} else {
LK.getSound('correctWord').play();
}
} else {
//console.log('incorrect');
LK.getSound('wrongWord').play();
streak = 0;
updateStreakText();
// Destroy the letters on the blackboard
clearBlackBoard(false);
// Reset the letter tiles
game.children.forEach(function (child) {
if (child instanceof LetterTile) {
child.alpha = 1;
child.clickable = true;
child.clickAddRemove = true;
}
});
blackboardOffsetX = 0;
currentWord = "";
LK.setScore(LK.getScore() - 1);
scoreTxt.setText('Score: ' + LK.getScore());
}
};
return self;
});
var Teacher = Container.expand(function () {
var self = Container.call(this);
var teacherGraphics = self.attachAsset('teacher', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
function clearBlackBoard() {
var placeholdersToo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
blackBoardArr.forEach(function (child) {
child.destroy();
});
blackBoardArr = [];
if (placeholdersToo) {
placeholderArr.forEach(function (child) {
child.destroy();
});
placeholderArr = [];
}
}
var streak = 0;
var streakText = new Text2('Streak: 0', {
size: 60,
fill: "#FFFFED",
stroke: "#000000",
strokeThickness: 10,
weight: 800
});
//streakText.anchor.set(-0.5, 0);
streakText.anchor.set(0, 0);
LK.gui.topLeft.addChild(streakText);
//streakText.x += 100;
// Add event listener to streakText
streakText.down = function (x, y, obj) {
showStreakInfoWindow();
};
var streakMult = 1;
var blackBoardArr = [];
var wholeBlackBoardArr = [];
var currentWord = "";
function updateStreakText() {
var streakBonus = '';
if (streak >= 60) {
streakBonus = ' (x10)';
streakMult = 10;
} else if (streak >= 50) {
streakBonus = ' (x8)';
streakMult = 8;
} else if (streak >= 40) {
streakBonus = ' (x7)';
streakMult = 7;
} else if (streak >= 30) {
streakBonus = ' (x6)';
streakMult = 6;
} else if (streak >= 20) {
streakBonus = ' (x5)';
streakMult = 5;
} else if (streak >= 10) {
streakBonus = ' (x4)';
streakMult = 4;
} else if (streak >= 5) {
streakBonus = ' (x3)';
streakMult = 3;
} else if (streak >= 1) {
streakBonus = ' (x2)';
streakMult = 2;
} else {
streakBonus = '';
streakMult = 1;
}
streakText.setText("Streak: " + streak + streakBonus);
}
var scoreTxt = new Text2('Score: 0', {
size: 70,
fill: "#ADE6D8",
stroke: "#000000",
strokeThickness: 10,
weight: 800
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
/*
var levelTxt = new Text2('Level 1', {
size: 60,
fill: "#ADD8E6",
stroke: "#000000",
strokeThickness: 10,
weight: 800
});
levelTxt.anchor.set(1.2, 0);
LK.gui.topRight.addChild(levelTxt);
*/
var blackboardOffsetX = 0;
var blackboardOffsetY = 0;
function removeLastLetterOnBlackBoard(value) {
console.log('hm');
var letterVal = blackBoardArr[blackBoardArr.length - 1].letter;
console.log('value and letterVal:' + value + ' ' + letterVal);
if (value == letterVal) {
var letter = blackBoardArr.splice(blackBoardArr.length - 1, 1)[0];
//game.addChild(blackboardText);
//blackBoardArr.push(blackboardText);
wholeBlackBoardArr.splice(wholeBlackBoardArr.length - 1, 1); //push(blackboardText);
letter.destroy();
blackboardOffsetX -= 80;
currentWord = currentWord.substr(0, currentWord.length - 1);
return true;
} else {
return false;
}
}
function writeLetterOnBlackboard(letter) {
var blackboardText = new LetterOnBlackboard();
blackboardText.x = 400 + blackboardOffsetX;
blackboardText.y = 400 + blackboardOffsetY;
blackboardText.setLetter(letter);
game.addChild(blackboardText);
blackBoardArr.push(blackboardText);
wholeBlackBoardArr.push(blackboardText);
blackboardOffsetX += 80; // Adjust the offset value as needed
// Check if this is the first letter clicked
if (blackboardOffsetX === 80) {
//submitButton = new SubmitButton();
submitButton.x = 2048 / 2;
submitButton.y = 2400;
game.addChild(submitButton);
}
}
var submitButton = new SubmitButton();
;
// Add background graphic object covering the whole screen
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 - 50,
y: 2732 / 2 - 200
});
game.addChild(background);
/*
var levelTxt = new Text2('Level 1', {
size: 60,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
weight: 800
});
levelTxt.x = 2048 / 2;
levelTxt.y = 200;
game.addChild(levelTxt);*/
var rabbit = new Rabbit();
rabbit.x = 250;
rabbit.y = 1700;
game.addChild(rabbit);
var cat = new Cat();
cat.x = 1800;
cat.y = 1670;
game.addChild(cat);
var tables = LK.getAsset('tables', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 - 50,
y: 2732 / 2 - 200
});
game.addChild(tables);
var levelText = new LevelText();
levelText.x = 2048 / 2;
levelText.y = 200;
game.addChild(levelText);
var teacher = new Teacher();
teacher.x = 2048 / 2 + 380;
teacher.y = 2732 / 2 - 250;
game.addChild(teacher);
// Function to reset the game state for the new level
function resetGameStateForNewLevel() {
// Logic to reset the game state for the new level
letterTilesArr.forEach(function (child) {
child.destroy();
});
wholeBlackBoardArr.forEach(function (child) {
child.destroy();
});
submitButton.destroy();
blackboardOffsetX = 0;
blackboardOffsetXY = 0;
currentWord = "";
var nextLevelBackground = new NextLevelBackground();
nextLevelBackground.x = 0;
nextLevelBackground.y = 1500;
game.addChild(nextLevelBackground);
var nextLevelText = new Text2('Level ' + (currentLevel + 1), {
size: 200,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
align: 'center',
weight: 800
});
nextLevelText.anchor.set(0.5, 0.5);
nextLevelText.x = 2048 / 2;
nextLevelText.y = 1500;
game.addChild(nextLevelText);
//LK.getSound('nextLevel').play();
LK.setTimeout(function () {
nextLevelText.destroy();
nextLevelBackground.destroy();
displayLettersInLine();
}, 2000);
/*LK.setTimeout(function () {
nextLevelText.destroy();
displayLettersInLine();
}, 2000); // Pause for 2 seconds before displaying letters
*/
}
var letterTilesArr = [];
// Function to display letters in a line centered at height 2000
function displayLettersInLine() {
var startX = 2048 / 2 - letters.length * 320 / 2 + 180;
var y = 2000;
for (var i = 0; i < letters.length; i++) {
var x = startX + i * 320;
var letterTile = new LetterTile();
letterTile.setLetter(letters[i]);
letterTile.x = x;
letterTile.y = y;
game.addChild(letterTile);
letterTilesArr.push(letterTile);
}
displayBlackboardPlaceholders();
}
var placeholderArr = [];
function displayBlackboardPlaceholders() {
console.log('ok');
var words = levels[currentLevel].words;
var offsetX = 0;
var offsetY = 60;
for (var i = 0; i < words.length; i++) {
console.log('ok i');
for (var j = 0; j < words[i].length; j++) {
console.log('ok j');
var p = new BlackboardPlaceholder();
p.x = 400 + offsetX;
p.y = 400 + offsetY;
game.addChild(p);
placeholderArr.push(p);
offsetX += 80;
if (j == words[i].length - 1) {
offsetX = 0;
offsetY += 118;
}
}
}
}
// Function to advance to the next level
function advanceToNextLevel() {
if (currentLevel < levels.length - 1) {
currentLevel++;
letters = getLettersForCurrentLevel();
levelTxt.setText('Level ' + (currentLevel + 1));
clearBlackBoard();
blackboardOffsetY = 0;
resetGameStateForNewLevel();
} else {
console.log("All levels completed!");
gameWon();
}
}
function gameWon() {
letterTilesArr.forEach(function (child) {
child.destroy();
});
submitButton.destroy();
cat.y = 1580;
rabbit.y = 1550;
var congratsText = new Text2('Congratulations!\nYou found all the words!', {
size: 150,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
align: 'center',
weight: 800
});
congratsText.anchor.set(0.5, 0.5);
congratsText.x = 2048 / 2;
congratsText.y = 900;
game.addChild(congratsText);
LK.setTimeout(function () {
congratsText.destroy();
LK.stopMusic();
LK.showGameOver();
}, 2000); // Display the message for 3 seconds
}
// Define levels with different sets of letters and acceptable words
var levels3 = [{
letters: ['N', 'W', 'O'],
words: ['NOW', 'WON', 'OWN']
}, {
letters: ['A', 'E', 'T'],
words: ['TEA', 'EAT', 'ATE']
}, {
letters: ['A', 'M', 'R'],
words: ['MAR', 'ARM', 'RAM']
}, {
letters: ['A', 'T', 'P'],
words: ['APT', 'PAT', 'TAP']
}, {
letters: ['A', 'T', 'R'],
words: ['TAR', 'ART', 'RAT']
}, {
letters: ['O', 'T', 'P'],
words: ['OPT', 'TOP', 'POT']
}, {
letters: ['A', 'E', 'R'],
words: ['EAR', 'ARE', 'ERA']
}];
var levels4 = [{
letters: ['E', 'C', 'A', 'R'],
words: ['ACRE', 'CARE', 'RACE', 'ACER']
}, {
letters: ['E', 'S', 'N', 'T'],
words: ['NETS', 'TENS', 'SENT', 'NEST']
}, {
letters: ['E', 'L', 'A', 'M'],
words: ['LAME', 'MALE', 'MEAL']
}, {
letters: ['S', 'D', 'E', 'U'],
words: ['DUES', 'SUED', 'USED']
}, {
letters: ['P', 'S', 'A', 'N'],
words: ['NAPS', 'SNAP', 'PANS', 'SPAN']
}, {
letters: ['A', 'L', 'R', 'I'],
words: ['LAIR', 'LIAR', 'RAIL']
}, {
letters: ['T', 'D', 'E', 'I'],
words: ['DIET', 'EDIT', 'TIDE', 'TIED']
}];
var levels5 = [{
letters: ['T', 'E', 'L', 'A', 'R'],
words: ['ALERT', 'ALTER', 'LATER']
}, {
letters: ['E', 'S', 'M', 'L', 'I'],
words: ['LIMES', 'SMILE', 'MILES', 'SLIME']
}, {
letters: ['S', 'R', 'T', 'I', 'E'],
words: ['RITES', 'TIERS', 'TRIES', 'TIRES']
}, {
letters: ['P', 'S', 'A', 'E', 'H'],
words: ['HEAPS', 'SHAPE', 'PHASE']
}, {
letters: ['O', 'D', 'Y', 'R', 'W'],
words: ['DOWRY', 'ROWDY', 'WORDY']
}, {
letters: ['E', 'B', 'O', 'R', 'S'],
words: ['BORES', 'ROBES', 'SOBER']
}, {
letters: ['P', 'L', 'E', 'S', 'O'],
words: ['LOPES', 'POLES', 'SLOPE']
}];
// Initialize the first level's letters and show level 1 when the game starts
var currentLevel = 0;
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
shuffleArray(levels3);
shuffleArray(levels4);
shuffleArray(levels5);
var levels = levels3.concat(levels4, levels5);
var letters = getLettersForCurrentLevel();
displayLettersInLine();
// Current level index
// Function to get letters for the current level
function getLettersForCurrentLevel() {
return levels[currentLevel].letters;
}
// Update function
game.update = function () {
// Game update logic
};
function createScoreNotice(x, y, value) {
var label = new Text2('+' + value, {
size: 120,
fill: "#ADE6D8",
stroke: "#000000",
strokeThickness: 10,
weight: 800
});
label.anchor.set(0.5, 0.5);
label.x = x;
label.y = y;
game.addChild(label);
var vy = -3;
(function (label, vy) {
var lifetime = 30;
var tick = 0;
var interval = LK.setInterval(function () {
label.y += vy;
label.alpha -= 0.25 / lifetime;
tick++;
if (tick >= lifetime) {
label.destroy();
LK.clearInterval(interval);
}
}, 16);
})(label, vy);
}
function createStarExplosion(x, y) {
for (var i = 0; i < 20; i++) {
var star = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: Math.random() * 0.5 + 0.5,
scaleY: Math.random() * 0.5 + 0.5,
x: x,
y: y
});
game.addChild(star);
var angle = Math.random() * Math.PI * 2;
var speed = Math.random() * 10 + 5;
var vx = Math.cos(angle) * speed;
var vy = Math.sin(angle) * speed;
(function (star, vx, vy) {
var lifetime = 30;
var tick = 0;
var interval = LK.setInterval(function () {
star.x += vx;
star.y += vy;
star.alpha -= 1 / lifetime;
tick++;
if (tick >= lifetime) {
star.destroy();
LK.clearInterval(interval);
}
}, 16);
})(star, vx, vy);
}
}
function showStreakInfoWindow() {
// Create a container for the window
var streakInfoWindow = new Container();
// Create a background for the window
var windowBg = LK.getAsset('nextLevelBg', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
scaleX: 0.8,
scaleY: 1.7
});
windowBg.alpha = 0.8;
streakInfoWindow.addChild(windowBg);
// Create the text for the window
var infoText = new Text2("Your streak increases with each successive correct guess, but is reset to 0 on a wrong guess.\n\nExtend your streak for better score multipliers.", {
size: 60,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
align: 'center',
wordWrap: true,
wordWrapWidth: 1500
});
infoText.anchor.set(0.5, 0.5);
infoText.x = 2048 / 2;
infoText.y = 2732 / 2 - 50;
streakInfoWindow.addChild(infoText);
// Create the close button
var closeButton = new Text2("Close", {
size: 80,
fill: "#ff0000",
stroke: "#000000",
strokeThickness: 10,
align: 'center'
});
closeButton.anchor.set(0.5, 0.5);
closeButton.x = 2048 / 2;
closeButton.y = 2732 / 2 + 200;
streakInfoWindow.addChild(closeButton);
// Add event listener to close button
closeButton.down = function (x, y, obj) {
streakInfoWindow.destroy();
};
// Add the window to the game
game.addChild(streakInfoWindow);
}
A smooth, clean, blank and empty scrabble tile for a game.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A clean, warm and welcoming classroom in a school, facing the blackboard.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A small golden star. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.