Code edit (2 edits merged)
Please save this source code
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: 95
Code edit (5 edits merged)
Please save this source code
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: 95
Code edit (6 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: ticksBeforeNextPosition is not defined' in or related to this line: 'scoreTest = "actualPIx: " + self.actualPositionIndex + " goalPIx: " + self.goalPositionIndex + " assetsToUse: " + self.assetsToUse + " seqIxToUse: " + self.sequenceIndexToUse + "\nticksBeforeNextPosition: " + ticksBeforeNextPosition;' Line Number: 114
Code edit (1 edits merged)
Please save this source code
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: 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
===================================================================
--- original.js
+++ change.js
@@ -38,18 +38,32 @@
var monkeyAsset = self.attachAsset('monkeyToLeft', {
anchorX: 0.5,
anchorY: 0.5
});
- self.assetSequenceToRight = ['monkeyToRight', 'monkeyInterRight', 'monkeyToRight', 'monkeyInterRight', 'monkeyToRight'];
- self.assetSequenceToLeft = ['monkeyToLeft', 'monkeyInterLeft', 'monkeyToLeft', 'monkeyInterLeft', 'monkeyToLeft'];
- self.assetSequenceToUse = [];
- self.assetSequenceActualPosition = 0;
- self.assetSequenceGoalPosition = 0;
+ self.assetsToRight = ['monkeyToRight', 'monkeyInterRight'];
+ self.assetsToLeft = ['monkeyToLeft', 'monkeyInterLeft'];
+ self.sequenceIndexToCenter = [0, 1, 0];
+ self.sequenceIndexToExtreme = [0, 1, 0, 1, 0];
+ self.assetsToUse = [];
+ self.sequenceIndexToUse = []; //Séquence des index des assets du monkey a utiliser
+ self.sequenceIndex = 0; //Index de la séquence des assets du monkey
+ self.actualPositionIndex = 0; //Positions de depart : 0 (XL), 1 (XC), 2 (XR) index du tableau startPoints
+ self.goalPositionIndex = 0; //Positions d'arrivée : 0 (XL), 1 (XC), 2 (XR) index du tableau startPoints
self.ticksToGoToGoalPosition = 0;
self.ticksBeforeNextPosition = 0;
self.isGoingToRight = false;
self.isGoingToLeft = false;
self.isMoving = false;
+ self.coconutChoosen = null;
+ //Function setMoveToGoalPosition : initialise les paramètres pour se déplacer à la position d'arrivée
+ self.setMoveToGoalPosition = function (coconut) {
+ if (coconut == null) {
+ return;
+ }
+ self.coconutChoosen = coconut;
+ self.goalPositionIndex = checkIndexPosition(coconut);
+ self.actualPositionIndex = checkIndexPosition(self);
+ }; //fin setMoveToGoalPosition
// Function to change the asset
self.changeAsset = function (newAssetId, options) {
// Remove the existing asset
self.removeChild(monkeyAsset);
@@ -60,41 +74,51 @@
anchorY: options && options.anchorY !== undefined ? options.anchorY : 0.5
});
};
self.update = function () {
- if (!self.isMoving && self.assetSequenceActualPosition != self.assetSequenceGoalPosition) {
- if (self.assetSequenceActualPosition < self.assetSequenceGoalPosition) {
+ if (!self.isMoving && self.actualPositionIndex != self.goalPositionIndex) {
+ if (self.actualPosition < self.goalPosition) {
self.isGoingToRight = true;
self.isGoingToLeft = false;
- self.assetSequenceToUse = self.assetSequenceToRight;
- self.ticksBeforeNextPosition = self.ticksToGoToGoalPosition / (self.assetSequenceGoalPosition - self.assetSequenceActualPosition);
+ self.assetsToUse = self.assetsToRight;
} else {
self.isGoingToRight = false;
self.isGoingToLeft = true;
- self.assetSequenceToUse = self.assetSequenceToLeft;
- self.ticksBeforeNextPosition = self.ticksToGoToGoalPosition / (self.assetSequenceActualPosition - self.assetSequenceGoalPosition);
+ self.assetsToUse = self.assetsToLeft;
}
+ self.sequenceIndexToUse = Math.abs(self.actualPositionIndex - self.goalPositionIndex) == 1 ? self.sequenceIndexToCenter : self.sequenceIndexToExtreme;
+ self.ticksBeforeNextPosition = self.ticksToGoToGoalPosition / self.sequenceIndexToUse.length;
self.isMoving = true;
} else if (self.isMoving) {
if (self.isGoingToRight) {
if (self.ticksBeforeNextPosition <= 0) {
- self.assetSequenceActualPosition += 1;
- self.changeAsset(self.assetSequenceToUse[self.assetSequenceActualPosition]);
+ self.changeAsset(self.assetsToUse[self.sequenceIndexToUse[self.sequenceIndex]]);
+ self.sequenceIndex += 1;
} else {
self.ticksBeforeNextPosition -= 1;
}
} else if (self.isGoingToLeft) {
if (self.ticksBeforeNextPosition <= 0) {
- self.assetSequenceActualPosition -= 1;
- self.changeAsset(self.assetSequenceToUse[self.assetSequenceActualPosition]);
+ self.changeAsset(self.assetsToUse[self.sequenceIndexToUse[self.sequenceIndex]]);
+ self.sequenceIndex += 1;
} else {
self.ticksBeforeNextPosition -= 1;
}
}
}
- if (self.assetSequenceActualPosition == self.assetSequenceGoalPosition) {
- //Le monkey est arrivé à la position voulue
+ if (checkIndexPosition(self) == self.goalPositionIndex) {
+ //Le monkey est arrivé à la position voulue, on remet les paramètres à zéro
self.isMoving = false;
+ self.actualPositionIndex = self.goalPositionIndex;
+ self.isGoingToRight = false;
+ self.isGoingToLeft = false;
+ self.sequenceIndex = 0;
+ self.assetsToUse = [];
+ self.sequenceIndexToUse = [];
+ if (self.coconutChoosen) {
+ self.coconutChoosen.trajectory.isStarted = true;
+ self.coconutChoosen = null;
+ }
}
}; //fin update
});
// Class for the Parasol
@@ -470,8 +494,25 @@
Xreturn = XR;
}
return Xreturn;
} //fin checkPosition
+//Fonction checkIndexPosition : donne l'index de la position XL, XC, ou XR la plus proche de la position x d'un objet
+function checkIndexPosition(obj) {
+ var deltaX = Math.abs(obj.x - XL);
+ var deltaY = Math.abs(obj.x - XC);
+ var deltaZ = Math.abs(obj.x - XR);
+ var Xreturn = -1;
+ if (deltaX <= obj.width / 2) {
+ Xreturn = 0;
+ }
+ if (deltaY <= obj.width / 2) {
+ Xreturn = 1;
+ }
+ if (deltaZ <= obj.width / 2) {
+ Xreturn = 2;
+ }
+ return Xreturn;
+} //fin checkIndexPosition
//Fonction zoneIntersectParasol: renvoie la zone du parasol qui intersecte le coconut (0 : zone de gauche, 1 : zone centrale de gauche, 2 : zone centrale de droite, 3 : zone de droite)
//Sachant que l'ancrage du parasol est en (0.5, 0.5) et que l'ancrage du coconut est en (0.5, 0.5)
//La zone 0 est la zone de gauche, la zone 1 est la zone centrale, la zone 2 est la zone de droite
function zoneIntersectParasol(coconut) {
@@ -575,16 +616,10 @@
* Main loop
****/
game.update = function () {
//Mise à jour score
- //scoreTest = livesLeft;
- //scoreTest = "Coconuts restantes: " + coconuts.length;
updateScoreTest(scoreTest);
updateScore(score);
- //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 >= groundLevel / 2 && (coconuts[i].x < 0 || coconuts[i].x > game.width)) {
//Si le coco est sorti de l'écran sur les côtés
@@ -631,14 +666,14 @@
// Récupérer un coconut dans les arbres et le faire tomber si l'heure est venue
if (coconutsInTree.length > 0 && nbTicksLeftBeforeNextThrow <= 0) {
if (coconutChoosen == null) {
coconutChoosen = chooseNextCoconut();
+ //coconutChoosen.trajectory.isStarted = true;
+ //game.addChild(coconutChoosen);
} else {
- coconutChoosen.trajectory.isStarted = true;
- game.addChild(coconutChoosen);
+ nbTicksLeftBeforeNextThrow = nbTicksBeforeThrow;
coconutChoosen = null;
}
- nbTicksLeftBeforeNextThrow = nbTicksBeforeThrow;
} else {
nbTicksLeftBeforeNextThrow -= 60;
}
}