User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of null (reading 'destroy')' in or related to this line: 'questionPanel.destroy();' Line Number: 374
User prompt
Please fix the bug: 'TypeError: Cannot set properties of undefined (setting 'fill')' in or related to this line: 'qTxt.style.fill = "#d83318"; // rouge vif pour la question' Line Number: 357
User prompt
Mets le texte des questions debiles et des réponses en rouge pour que ça se voit mieux
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading '0')' in or related to this line: 'tween(obj.children[0], {' Line Number: 283
Code edit (1 edits merged)
Please save this source code
User prompt
Money aux arbres
Initial prompt
Crée le jeu "Money aux arbres", Ce jeu a un peu trop prix au serieu l'expression "argent ne pousse pas aux arbres", car dans ce jeu de logique ! L'argent pousse aux arbres ! Le gameplay est simple ; Le jeu se présente sous forme de fond vert, avec des arbres qui apparaissent aléatoirement dans le jeu, Il y a de l'argent aux arbres!, le joueur doit se déplacer dans cette petite forêt de money en appuyant sur son écran (son personnage se déplace petit a petit qu'il appuie sur son écran, en petits sauts de courte distance a la fois), quand le joueur entre en collision avec un arbre, il doit répondre à 3 questions débiles de culture générale qui prêtent a rire, s'il sélectionne la bonne réponse, il collecte l'argent de l'arbre (aléatoirement entre 50 € et 100€ , c'est virtuel biensur), Le joueur collecte d'Argents aux arbres, plus il monte en niveau! Il lui faut collectionné 500 € pour monter au niveau 2, et a chaque niveau il lui faut le double pour monter au niveau supérieur !
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerAsset = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1
});
// For jump animation
self.isJumping = false;
return self;
});
// Tree class
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeAsset = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 1
});
// Add a money icon on the tree
var moneyAsset = self.attachAsset('money', {
anchorX: 0.5,
anchorY: 0.5,
y: -treeAsset.height * 0.7
});
// Used to know if this tree has been collected
self.collected = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1e5631 // deep forest green
});
/****
* Game Code
****/
/****
* Questions
****/
// Player asset: blue box
// Money asset: yellow box
// Tree asset: green ellipse
// Game constants
var QUESTIONS = [{
q: "Combien de bananes faut-il pour faire un vélo ?",
a: ["Aucune, c'est absurde !", "42", "3, si elles sont très grandes"],
c: 0
}, {
q: "Quel animal est le plus susceptible de devenir président ?",
a: ["Le lama", "Le poisson rouge", "Le pigeon voyageur"],
c: 2
}, {
q: "Si un arbre tombe dans une forêt et que personne ne l’entend, fait-il du bruit ?",
a: ["Oui, mais il chuchote", "Non, il chante", "Seulement les jours pairs"],
c: 0
}, {
q: "Quelle est la couleur du cheval vert d’Henri IV ?",
a: ["Vert", "Bleu", "Invisible"],
c: 0
}, {
q: "Combien de lunes autour de la Terre un mardi ?",
a: ["Une", "Trois", "Aucune, elles sont en vacances"],
c: 0
}, {
q: "Quel est le cri du billet de banque ?",
a: ["Chling !", "Miaou", "Bip bip"],
c: 0
}, {
q: "Si tu plantes une pièce de 1€, que poussera-t-il ?",
a: ["Un arbre à billets", "Un distributeur", "Un cactus doré"],
c: 0
}, {
q: "Quel est le sport préféré des arbres ?",
a: ["Le tronc-volant", "Le basket-feuille", "Le branché-ball"],
c: 1
}, {
q: "Combien de temps met un escargot pour faire le tour d’un arbre ?",
a: ["Le temps qu’il veut", "Jamais, il s’arrête pour dormir", "42 jours"],
c: 0
}, {
q: "Quel est le prénom du plus riche des arbres ?",
a: ["Billet", "Feuille", "Racine"],
c: 1
}];
var BOARD_WIDTH = 2048;
var BOARD_HEIGHT = 2732;
var GROUND_Y = 2300; // y position of the ground
var TREE_MIN_X = 200;
var TREE_MAX_X = BOARD_WIDTH - 200;
var TREE_MIN_Y = 800;
var TREE_MAX_Y = GROUND_Y - 200;
var TREE_COUNT = 5;
// Game state
var trees = [];
var player = null;
var score = 0;
var level = 1;
var moneyNeeded = 500;
var questionIndex = 0;
var currentTree = null;
var isQuestionActive = false;
var questionPanel = null;
var answerButtons = [];
var collectedMoney = 0;
var jumpTarget = null;
var canJump = true;
// GUI
var scoreTxt = new Text2('0 €', {
size: 120,
fill: 0xFFF700
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var levelTxt = new Text2('Niveau 1', {
size: 80,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 130;
// Helper: random int
function randInt(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
// Helper: pick random question (avoid repeats in a session)
function pickRandomQuestions(n) {
var used = {};
var arr = [];
while (arr.length < n) {
var idx = randInt(0, QUESTIONS.length - 1);
if (!used[idx]) {
arr.push(QUESTIONS[idx]);
used[idx] = true;
}
}
return arr;
}
// Place trees randomly
function spawnTrees() {
// Remove old trees
for (var i = 0; i < trees.length; i++) {
trees[i].destroy();
}
trees = [];
for (var i = 0; i < TREE_COUNT; i++) {
var t = new Tree();
// Random position, not too close to edges or each other
var tries = 0;
var ok = false;
while (!ok && tries < 20) {
t.x = randInt(TREE_MIN_X, TREE_MAX_X);
t.y = randInt(TREE_MIN_Y, TREE_MAX_Y);
ok = true;
for (var j = 0; j < trees.length; j++) {
var dx = t.x - trees[j].x;
var dy = t.y - trees[j].y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 400) ok = false;
}
tries++;
}
trees.push(t);
game.addChild(t);
}
}
// Place player at a random tree
function placePlayer() {
var idx = randInt(0, trees.length - 1);
var t = trees[idx];
player.x = t.x;
player.y = t.y - 10;
}
// Find nearest tree to (x, y) not already collected
function findNearestTree(x, y) {
var minDist = 99999;
var nearest = null;
for (var i = 0; i < trees.length; i++) {
if (trees[i].collected) continue;
var dx = trees[i].x - x;
var dy = trees[i].y - y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < minDist) {
minDist = dist;
nearest = trees[i];
}
}
return nearest;
}
// Show question panel
function showQuestions(tree, onFinish) {
isQuestionActive = true;
questionPanel = new Container();
// Panel background
var panelBg = LK.getAsset('tree', {
width: 1200,
height: 900,
color: 0x222222,
anchorX: 0.5,
anchorY: 0.5
});
panelBg.alpha = 0.95;
questionPanel.addChild(panelBg);
questionPanel.x = BOARD_WIDTH / 2;
questionPanel.y = BOARD_HEIGHT / 2;
// Pick 3 random questions
var qs = pickRandomQuestions(3);
questionIndex = 0;
var correctCount = 0;
var moneyWon = 0;
// Question text
var qTxt = new Text2('', {
size: 70,
fill: "#fff"
});
qTxt.anchor.set(0.5, 0);
qTxt.y = -350;
questionPanel.addChild(qTxt);
// Answer buttons
answerButtons = [];
for (var i = 0; i < 3; i++) {
var btn = new Container();
var btnBg = LK.getAsset('money', {
width: 900,
height: 120,
color: 0x3cb371,
anchorX: 0.5,
anchorY: 0.5
});
btn.addChild(btnBg);
var btnTxt = new Text2('', {
size: 55,
fill: "#222"
});
btnTxt.anchor.set(0.5, 0.5);
btn.addChild(btnTxt);
btn.y = -100 + i * 180;
btn.buttonIndex = i;
btn.down = function (x, y, obj) {
if (!isQuestionActive) return;
var idx = obj.buttonIndex;
var q = qs[questionIndex];
if (idx === q.c) {
correctCount++;
// Animate button green
if (obj.children && obj.children[0]) {
tween(obj.children[0], {
tint: 0x83de44
}, {
duration: 200,
onFinish: function onFinish() {
if (obj.children && obj.children[0]) {
tween(obj.children[0], {
tint: 0x3cb371
}, {
duration: 200
});
}
}
});
}
} else {
// Animate button red
if (obj.children && obj.children[0]) {
tween(obj.children[0], {
tint: 0xd83318
}, {
duration: 200,
onFinish: function onFinish() {
if (obj.children && obj.children[0]) {
tween(obj.children[0], {
tint: 0x3cb371
}, {
duration: 200
});
}
}
});
}
}
// Next question or finish
LK.setTimeout(function () {
questionIndex++;
if (questionIndex < 3) {
setQuestion();
} else {
// Done: reward if at least 1 correct
if (correctCount > 0) {
moneyWon = randInt(50, 100) * correctCount;
collectedMoney = moneyWon;
score += moneyWon;
LK.setScore(score);
scoreTxt.setText(score + " €");
// Animate money
var moneyAnim = LK.getAsset('money', {
width: 200,
height: 100,
color: 0xffe066,
anchorX: 0.5,
anchorY: 0.5
});
moneyAnim.x = 0;
moneyAnim.y = 250;
questionPanel.addChild(moneyAnim);
var moneyText = new Text2("+" + moneyWon + " €", {
size: 70,
fill: "#222"
});
moneyText.anchor.set(0.5, 0.5);
moneyAnim.addChild(moneyText);
tween(moneyAnim, {
y: 100,
alpha: 0
}, {
duration: 900,
onFinish: function onFinish() {
moneyAnim.destroy();
finishPanel();
}
});
} else {
finishPanel();
}
}
}, 400);
};
answerButtons.push(btn);
questionPanel.addChild(btn);
}
// Set question/answers
function setQuestion() {
var q = qs[questionIndex];
qTxt.setText(q.q);
if (qTxt && typeof qTxt.setStyle === "function") {
qTxt.setStyle({
fill: 0xD83318
}); // rouge vif pour la question
}
for (var i = 0; i < 3; i++) {
answerButtons[i].children[1].setText(q.a[i]);
if (answerButtons[i].children[1] && typeof answerButtons[i].children[1].setStyle === "function") {
answerButtons[i].children[1].setStyle({
fill: 0xD83318
}); // rouge vif pour les réponses
}
}
}
// Remove panel and call onFinish
function finishPanel() {
isQuestionActive = false;
if (questionPanel) {
questionPanel.destroy();
questionPanel = null;
}
answerButtons = [];
if (onFinish) onFinish(correctCount > 0, collectedMoney);
}
setQuestion();
game.addChild(questionPanel);
}
// Level up logic
function checkLevelUp() {
if (score >= moneyNeeded) {
level++;
moneyNeeded *= 2;
levelTxt.setText("Niveau " + level);
// Flash effect
LK.effects.flashScreen(0x83de44, 800);
// Respawn trees, reset player
spawnTrees();
placePlayer();
}
}
// Initialize game
function startGame() {
score = 0;
level = 1;
moneyNeeded = 500;
scoreTxt.setText("0 €");
levelTxt.setText("Niveau 1");
spawnTrees();
if (player) player.destroy();
player = new Player();
game.addChild(player);
placePlayer();
canJump = true;
isQuestionActive = false;
if (questionPanel) {
questionPanel.destroy();
questionPanel = null;
}
}
// Handle tap: jump to nearest uncollected tree
game.down = function (x, y, obj) {
if (isQuestionActive || !canJump) return;
// Find nearest uncollected tree not at current position
var nearest = null;
var minDist = 99999;
for (var i = 0; i < trees.length; i++) {
if (trees[i].collected) continue;
var dx = trees[i].x - player.x;
var dy = trees[i].y - player.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 50 && dist < minDist) {
minDist = dist;
nearest = trees[i];
}
}
if (!nearest) return;
canJump = false;
player.isJumping = true;
jumpTarget = {
x: nearest.x,
y: nearest.y - 10
};
// Animate jump
var startY = player.y;
var midY = startY - 180;
tween(player, {
y: midY
}, {
duration: 180,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(player, {
y: jumpTarget.y
}, {
duration: 180,
easing: tween.easeIn,
onFinish: function onFinish() {
player.x = jumpTarget.x;
player.y = jumpTarget.y;
player.isJumping = false;
canJump = true;
// Check collision with tree
for (var i = 0; i < trees.length; i++) {
var t = trees[i];
if (!t.collected && Math.abs(player.x - t.x) < 80 && Math.abs(player.y - (t.y - 10)) < 80) {
t.collected = true;
// Animate tree
tween(t, {
alpha: 0.3
}, {
duration: 400
});
// Show questions
showQuestions(t, function (success, money) {
if (success) {
// Animate money to score
LK.effects.flashObject(scoreTxt, 0xffe066, 600);
checkLevelUp();
}
// If all trees collected, respawn
var allCollected = true;
for (var j = 0; j < trees.length; j++) {
if (!trees[j].collected) allCollected = false;
}
if (allCollected) {
spawnTrees();
placePlayer();
}
});
break;
}
}
}
});
}
});
};
// Prevent drag
game.move = function (x, y, obj) {
// No drag, only tap to jump
};
// Main update loop
game.update = function () {
// Nothing needed here for now
};
// Start game
startGame(); ===================================================================
--- original.js
+++ change.js
@@ -357,10 +357,12 @@
}
// Remove panel and call onFinish
function finishPanel() {
isQuestionActive = false;
- questionPanel.destroy();
- questionPanel = null;
+ if (questionPanel) {
+ questionPanel.destroy();
+ questionPanel = null;
+ }
answerButtons = [];
if (onFinish) onFinish(correctCount > 0, collectedMoney);
}
setQuestion();