User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'monkeyAsset = self.attachAsset(newAssetId, {' Line Number: 96
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'monkeyAsset = self.attachAsset(newAssetId, {' Line Number: 96
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'monkeyAsset = self.attachAsset(newAssetId, {' Line Number: 94
Code edit (13 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of null (reading 'trajectory')' in or related to this line: 'coconutChoosen.trajectory.isStarted = true;' Line Number: 138
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'anchorX')' in or related to this line: 'monkeyAsset = self.attachAsset(newAssetId, {' Line Number: 69
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot set properties of undefined (setting 'anchorX')' in or related to this line: 'monkey.monkeyAsset.anchorX = 0.91;' Line Number: 421
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: initialAsset is not defined' in or related to this line: 'self.removeChild(initialAsset);' Line Number: 66
Code edit (1 edits merged)
Please save this source code
Code edit (8 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'x')' in or related to this line: 'monkey.sprite.x = 2048 / 2;' Line Number: 246
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (12 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: oconutsInTree is not defined' in or related to this line: 'oconutsInTree.push(newCoconut);' Line Number: 338
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (19 edits merged)
Please save this source code
/****
* Classes
****/
// Class for the Coconut
var Coconut = Container.expand(function () {
var self = Container.call(this);
var coconutGraphics = self.attachAsset('coconut', {
anchorX: 0.5,
anchorY: 0.5
});
self.isFalling = false;
self.isBouncing = false;
self.trajectory = new Trajectory();
self.update = function () {
if (self.trajectory.typeTrajectory == 0) {
self.trajectory.updateLinear();
}
if (self.trajectory.typeTrajectory == 1) {
self.trajectory.updateParabol();
}
self.y = self.trajectory.y;
self.x = self.trajectory.x;
self.isFalling = self.trajectory.isFalling;
if (self.y > 2732) {
self.destroy();
}
}; //fin update
});
//<Assets used in the game will automatically appear here>
// Class for the Monkey
var Monkey = Container.expand(function () {
var self = Container.call(this);
var monkeyGraphics = self.attachAsset('monkey', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Monkey logic can be added here
};
});
// Class for the Parasol
var Parasol = Container.expand(function () {
var self = Container.call(this);
var parasolGraphics = self.attachAsset('parasol', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Parasol logic can be added here
};
});
//Class for the trajectory of the coconut
//Gestion d'une trajectoire parabolique de sommet ((X1 + X2)/2, Yh)
//et de point de départ (X1, Y0) et d'arrivée (X2, Y0) avec Y0 > Yh et X1 < X2 et Yh != Y0 != 0 et X1 != X2 != 0
//la fonction de mise à jour calcule la position du point suivant en fonction de speed .
//L'equation de la trajectoire est de la forme y = a(x - X1)(x - X2) + Y0 avec a = 4(Y0 - Yh)/((X1 - X2)(X1 - X2))
//Pour la trajectoire linéaire, le point de départ est (X1, Yh) et le point d'arrivée est (X1, Y0)
//Le temps de parcours est totalTicks que se soit pour la trajectoire linéaire ou parabolique
var Trajectory = Container.expand(function () {
var self = Container.call(this);
var trajectoryGraphics = self.attachAsset('trajectory', {
anchorX: 0.5,
anchorY: 0.5
});
self.typeTrajectory = 0; //0: linear, 1: parabolic
self.X1 = 0;
self.X2 = 0;
self.Y0 = 0;
self.Yh = 0;
self.a = 0;
self.x = 0;
self.y = 0;
self.totalTicks = 0; //Nombre total de ticks pour passer de X1 à X2
self.distPerTick = 0; //Distance parcourue par tick
self.isStarted = false; //Indique si la trajectoire a commencé
self.setParameters = function (typeTrajectory, X1, X2, Y0, Yh, totalTicks) {
//Initialisation des paramètres de la trajectoire
self.typeTrajectory = typeTrajectory;
self.X1 = X1;
self.X2 = X2;
self.Y0 = Y0;
self.Yh = Yh;
self.totalTicks = totalTicks;
self.isStarted = false;
//scoreTest = "typeTrajectory: " + typeTrajectory + " X1: " + self.X1 + " X2: " + self.X2 + " Y0: " + self.Y0 + " Yh: " + self.Yh + " totalTicks: " + self.totalTicks;
}; //fin setParameters
self.updateLinear = function () {
// Trajectory logic can be added here
var lastY = self.y;
if (self.typeTrajectory == 0 && !self.isStarted) {
self.x = self.X1;
self.y = self.Yh;
distPerTick = (self.Y0 - self.Yh) / self.totalTicks;
self.isStarted = true;
} else {
if (self.y < self.Y0) {
self.y += distPerTick;
}
}
self.isFalling = lastY < self.y;
}; //fin updateLinear
self.updateParabol = function () {
//scoreTest += 1;
// Trajectory logic can be added here
var lastY = self.y;
if (self.typeTrajectory == 1 && !self.isStarted) {
self.x = self.X1;
self.y = self.Y0;
self.a = 4 * (self.Y0 - self.Yh) / ((self.X1 - self.X2) * (self.X1 - self.X2));
self.distPerTick = (self.X2 - self.X1) / self.totalTicks;
self.isStarted = true;
//scoreTest = "x= " + self.x + " y= " + self.y + " a= " + self.a + " distPerTick= " + self.distPerTick;
} else {
self.x += self.distPerTick;
self.y = self.a * (self.x - self.X1) * (self.x - self.X2) + self.Y0;
//scoreTest = "x= " + self.x + " y= " + self.y + " a= " + self.a + " distPerTick= " + self.distPerTick;
}
self.isFalling = lastY < self.y;
}; //fin updateParabol
});
/****
* Initialize Game
****/
//fin class Trajectory
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
/****
* GAME DESCRIPTION:
* Game Principle:
* NAME: COCO MONKEY V1.0 by Dalhem 2024
* -There is beach where people are enjoying the sun by the shadow of palm trees.
* -Above them, three palm trees are growing coconuts.
* -In the trees, a monkey is throwing coconuts from the top of the trees, thus three points of departure.
* -The player controls a guy who holding a parasol to protect everyone from the coconuts.
* -The coconuts are falling at a regular speed, falling in straight lines.
* -If a coconut touches the parasol, the player earns points.
* -If a coconut touches the ground, the player loses a life.
* -The game is over when the player has no more lives.
* Game Areas:
* The screen is divided into 3 main zones in the following descending order:
1. Score area: displays the player's score.
2. The main game area: where the game takes place, it is divided into 3 zones corresponding to the 3 points of departure of the coconuts :
* The left zone: the coconuts fall from the left tree.
* The center zone: the coconuts fall from the center tree.
* The right zone: the coconuts fall from the right tree.
3. Options area (or advertisement).
* Game Progression:
* At the beginning of the game, the player has 0 points.
* The player has 3 lives.
* The player can move the parasol by dragging it.
* The monkey throws coconuts at a regular interval randomly from the top of the trees.
* When a coconut touches the parasol, it bounces up following a parabolic trajectory.
* The parabolic trajectory depends on the angle of the parasol:
* The more the parasol is inclined, the more the coconut will bounce back :
* If the parasol is in the left zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus outside the screen.
* to the right, thus towards the center zone.
* to the extrem right, thus towards the right zone.
* If the parasol is in the center zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus towards the left zone.
* to the extrem left, thus outside the screen.
* to the right, thus towards the right zone.
* to the extrem right, thus outside the screen.
* If the parasol is in the right zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus towards the center zone.
* to the extrem left, thus towards the left zone.
* to the right, thus outside the screen.
* the parasol has three zones of influence:
* The left zone: the coconut will bounce to the extrem left.
* The center zone: the coconut will randomly bounce to the left or to the right.
* The right zone: the coconut will bounce to the extrem right.
****/
/****
* Game zones and backgrounds
****/
var ScoreZone = {
x: 0,
y: 0,
width: game.width,
height: 200 * game.height / 2732
};
var MainZone = {
x: 0,
y: ScoreZone.height,
width: game.width,
height: game.height - 2 * ScoreZone.height
};
var OptionsZone = {
x: 0,
y: game.height - ScoreZone.height,
width: game.width,
height: 200 * game.height / 2732
};
/****
* Autres
****/
var monkey = game.addChild(new Monkey());
monkey.x = 2048 / 2;
monkey.y = 200;
var parasol = game.addChild(new Parasol());
parasol.x = 2048 / 2;
parasol.y = OptionsZone.y - 400;
var coconuts = [];
var nombreCoconuts = 1;
var sommetParaboleHaute = ScoreZone.height + 200;
var sommetParaboleBasse = ScoreZone.height + 400;
var nbTicksToBounceHight = 500;
var nbTicksTobounceLow = 500;
var score = 0;
var scoreTest = 0;
var startPoints = [XL = game.width / 4, XC = game.width / 2, XR = 3 * game.width / 4]; // Define the startPoints array
/****
* Score
****/
var livesLeft = 3; //Nombre de vies restantes
var livesLeftGraphics = []; //Tableau des sprites 'life' représentant les vies restantes
var scoreTestText = new Text2('0', {
size: 30,
fill: "#000000",
anchorX: 0.5,
anchorY: 0
});
LK.gui.topLeft.addChild(scoreTestText);
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
anchorX: 0.5,
anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
parasol.x = x;
//parasol.y = y;
parasol.y = OptionsZone.y - 400;
};
game.move = function (x, y, obj) {
parasol.x = x;
//parasol.y = y;
parasol.y = OptionsZone.y - 400;
};
/****
* Functions
****/
function updateScoreTest(nouveauScore) {
scoreTestText.setText(nouveauScore);
} //fin updateScoreTest
function updateScore(nouveauScore) {
scoreTxt.setText(nouveauScore);
} //fin updateScore
function setLivesLeft() {
//Affiche le nombre de vies restantes (sprite 'life' ) dans le coin supérieur gauche
//Commence par effacer les vies restantes s'il y en a
if (livesLeft != livesLeftGraphics.length) {
for (var i = livesLeftGraphics.length - 1; i >= livesLeft; i--) {
if (livesLeftGraphics[i]) {
game.removeChild(livesLeftGraphics[i]);
livesLeftGraphics[i].destroy();
livesLeftGraphics.splice(i, 1);
}
}
}
//Crée des sprites 'life' si nécessaire (si livesLeft > livesLeftGraphics.length)
if (livesLeft > livesLeftGraphics.length) {
for (var i = livesLeftGraphics.length; i < livesLeft; i++) {
var lifeGraphics = LK.getAsset('life', {
anchorX: 0.5,
anchorY: 0,
scaleX: 1,
scaleY: 1,
x: ScoreZone.x + 100 + 100 * i,
y: ScoreZone.y + 100
});
livesLeftGraphics.push(lifeGraphics);
}
}
//Affiche les sprites 'life' dans le coin supérieur gauche
for (var i = 0; i < livesLeft; i++) {
game.addChild(livesLeftGraphics[i]);
}
} //fin setLivesLeft
//Fonction chooseNextStartPoint: choisit aléatoirement un point de départ d'un coconut
//Renvoie un objet Coconut
function chooseNextStartPoint() {
var newCoconut = new Coconut();
// Choisir aléatoirement un point de départ
var randomIndex = Math.floor(Math.random() * startPoints.length);
var typeTrajectory = 0;
newCoconut.isFalling = true;
var X1 = startPoints[randomIndex];
var X2 = X1;
var Y0 = OptionsZone.y;
var Yh = monkey.y;
var totalTicks = 500;
newCoconut.trajectory.setParameters(typeTrajectory, X1, X2, Y0, Yh, totalTicks);
coconuts.push(newCoconut);
return newCoconut;
} //fin chooseNextStartPoint
//Fonction checkPosition : donne la position XL, XC, ou XR la plus proche de la position x du parasol
function checkPosition(x) {
var deltaX = Math.abs(x - XL);
var deltaY = Math.abs(x - XC);
var deltaZ = Math.abs(x - XR);
var Xreturn = -1;
if (deltaX <= deltaY && deltaX <= deltaZ) {
Xreturn = XL;
}
if (deltaY <= deltaX && deltaY <= deltaZ) {
Xreturn = XC;
}
if (deltaZ <= deltaX && deltaZ <= deltaY) {
Xreturn = XR;
}
return Xreturn;
} //fin checkPosition
//Fonction zoneIntersectParasol: renvoie la zone du parasol qui intersecte le coconut (PL=parasol.width/3, PC=2*PL, PR=3*PL)
function zoneIntersectParasol(coconut) {
var zone = -1;
var deltaX = Math.abs(coconut.x - parasol.x);
if (deltaX <= parasol.width / 3) {
zone = 0;
}
if (deltaX > parasol.width / 3 && deltaX <= 2 * parasol.width / 3) {
zone = 1;
}
if (deltaX > 2 * parasol.width / 3) {
zone = 2;
}
return zone;
} //fin zoneIntersectParasol
//Fonction changeTrajectory: change la trajectoire d'un coconut en fonction de la position du parasol en XL, XC ou XR (position de startPoints)
//Si la position du parasol est en XL le coconut rebondit aléatoirement :
// - vers la gauche, donc vers l'extérieur de l'écran
// - vers la droite, donc vers la zone centrale avec une parabole de somment sommetParaboleHaute
// - vers l'extrême droite, donc vers la zone de droite avec une parabole de somment sommetParaboleBasse
//Si la position du parasol est en XC le coconut rebondit aléatoirement :
// - vers la gauche, donc vers la zone de gauche avec une parabole de somment sommetParaboleHaute
// - vers l'extrême gauche, donc vers l'extérieur de l'écran
// - vers la droite, donc vers la zone de droite avec une parabole de somment sommetParaboleHaute
// - vers l'extrême droite, donc vers l'extérieur de l'écran
//Si la position du parasol est en XR le coconut rebondit aléatoirement :
// - vers la droite, donc vers l'extérieur de l'écran
// - vers la gauche, donc vers la zone centrale avec une parabole de somment sommetParaboleHaute
// - vers l'extrême gauche, donc vers la zone de gauche avec une parabole de somment sommetParaboleBasse
function changeTrajectory(coconut) {
var randomIndex = Math.floor(Math.random() * 3);
var Xposition = checkPosition(parasol.x);
var deltaX = Math.abs(coconut.x - Xposition);
if (Xposition == XL) {
if (randomIndex == 0) {
//vers la gauche, sortie de l'écran
coconut.trajectory.setParameters(1, coconut.x, -XL - deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
if (randomIndex == 1) {
//vers le centre, parabole haute
coconut.trajectory.setParameters(1, coconut.x, XC - deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
if (randomIndex == 2) {
//vers l'extrême droite, parabole basse
coconut.trajectory.setParameters(1, coconut.x, XR - deltaX, coconut.y, sommetParaboleBasse, nbTicksTobounceLow);
}
}
if (Xposition == XC) {
//randomIndex = Math.floor(Math.random() * 4);
randomIndex = 2;
if (randomIndex == 0) {
//vers la gauche, parabole haute
coconut.trajectory.setParameters(1, coconut.x, XL + deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
if (randomIndex == 1) {
//vers la gauche, sortie d'ecran
coconut.trajectory.setParameters(1, coconut.x, -XL + deltaX, coconut.y, sommetParaboleBasse, nbTicksTobounceLow);
}
if (randomIndex == 2) {
//Vers la droite, parabole haute
coconut.trajectory.setParameters(1, coconut.x, XR - deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
if (randomIndex == 3) {
//Vers la droite, sortie d'ecran
coconut.trajectory.setParameters(1, coconut.x, XR + 2 * XL - deltaX, coconut.y, sommetParaboleBasse, nbTicksTobounceLow);
}
}
if (Xposition == XR) {
if (randomIndex == 0) {
//vers la droite, sortie de l'écran
coconut.trajectory.setParameters(1, coconut.x, XR + 2 * XL - deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
if (randomIndex == 1) {
//vers le centre, parabole haute
coconut.trajectory.setParameters(1, coconut.x, XC + deltaX, coconut.y, sommetParaboleHaute, nbTicksToBounceHight);
}
//beta.frvr.ai/creator/P1fQaUz3am#
https: if (randomIndex == 2) {
//vers l'extrême gauche, parabole basse
coconut.trajectory.setParameters(1, coconut.x, XL + deltaX, coconut.y, sommetParaboleBasse, nbTicksTobounceLow);
}
}
} //fin changeTrajectory
/****
* Main loop
****/
game.update = function () {
//Mise à jour score
//scoreTest = livesLeft;
updateScoreTest(scoreTest);
updateScore(score);
//Autres actions à effectuer sans urgence
if (LK.ticks % 60 == 0) {
//Afficher les coeurs
setLivesLeft();
// Créer un nouveau coco
if (coconuts.length < nombreCoconuts) {
var newCoconut = chooseNextStartPoint();
game.addChild(newCoconut);
}
}
//Check if a coconut has touched the parasol et changement de trajectoire
for (var i = coconuts.length - 1; i >= 0; i--) {
if (coconuts[i].intersects(parasol) && coconuts[i].isFalling) {
scoreTest = "Zone contact = " + zoneIntersectParasol(coconuts[i]);
score += 1;
coconuts[i].isFalling = false;
var deltaX = Math.abs(coconuts[i].x - XL);
//coconuts[i].trajectory.setParameters(1, coconuts[i].x, XC - deltaX, coconuts[i].y, sommetParaboleHaute, 500);
changeTrajectory(coconuts[i]);
coconuts[i].isBouncing = true;
}
}
//Mise à jour des trajectoires des coconuts
for (var i = coconuts.length - 1; i >= 0; i--) {
coconuts[i].update();
}
//Vérifier si un coco a touché le sol
for (var i = coconuts.length - 1; i >= 0; i--) {
if (coconuts[i].y >= OptionsZone.y && (coconuts[i].x < 0 || coconuts[i].x > game.width)) {
coconuts[i].destroy();
coconuts.splice(i, 1);
} else if (coconuts[i].y >= OptionsZone.y && (coconuts[i].isFalling || coconuts[i].isBouncing)) {
LK.effects.flashScreen(0xff0000, 1000);
coconuts[i].destroy();
coconuts.splice(i, 1);
livesLeft -= 1;
if (livesLeft == 0) {
LK.showGameOver();
}
}
}
};