User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var gameCoords = game.toLocal(obj.parent.toGlobal({' Line Number: 186
User prompt
Fait en sorte que le nombre de pourcentage de dévoilement de l'image soit affiché, et que le joueur puisse gratter sur téléphone mobile
Code edit (1 edits merged)
Please save this source code
User prompt
Sadokuna
Initial prompt
Bonjour ! Je suis Clemylia ! J'ai une idée de jeu! Je veux créer "Sadokuna", Un jeu kawaii de grattage de fonds d'écrans Le but du jeu est simple : Il y a un fond rose pailette, multicolore, violet clair ou turquoise, Et le joueur doit gratter dessus pour retirer toute la couleur, et relever la magnifique image de fille d'animé manga kawaii présente en dessous, En un temps limité. Le jeu possède 28 niveaux, Dans chaque niveau, le joueur gratte un fond d'écran, pour réveler la merveille qui se cache en dessus, Le temps que le joueur a pour gratter est limité, Et le temps est de plus en plus serrée, Si le joueur n'y arrive pas il recommence au niveau 1, ce qui le force a encore plus recommencer pour se surpasser Le dernier niveau (28), doit avoir le chronomètre a 2 Secondes seulement ! Le temps doit être affiché et le chronomètre être fonctionnel (avec une minute au niveau 1 et baissant de 2 secondes a chaque nouveau niveau), Le joueur doit dévoile au moins 85% de l'image pour gagner le niveau. fait en sorte qu'il y est une fille d'anime différente à chaque niveau, Et laisse moi mettre mes propres assets de fond, et de fille d'anime ! Voilà!
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var ScratchOverlay = Container.expand(function () {
var self = Container.call(this);
self.revealPercentage = 0;
self.scratched = {};
var overlay = self.attachAsset('overlay1', {
anchorX: 0,
anchorY: 0
});
self.setOverlayColor = function (colorId) {
overlay.destroy();
var colorMap = {
1: 'overlay1',
2: 'overlay2',
3: 'overlay3',
4: 'overlay4'
};
overlay = self.attachAsset(colorMap[colorId], {
anchorX: 0,
anchorY: 0
});
};
self.scratchArea = function (x, y, brushSize) {
var brushRadius = brushSize / 2;
var startX = Math.max(0, Math.floor(x - brushRadius));
var startY = Math.max(0, Math.floor(y - brushRadius));
var endX = Math.min(2048, Math.ceil(x + brushRadius));
var endY = Math.min(2732, Math.ceil(y + brushRadius));
for (var px = startX; px < endX; px++) {
for (var py = startY; py < endY; py++) {
var key = px + ',' + py;
if (!self.scratched[key]) {
var dist = Math.sqrt((px - x) * (px - x) + (py - y) * (py - y));
if (dist <= brushRadius) {
self.scratched[key] = true;
}
}
}
}
self.updateRevealPercentage();
};
self.updateRevealPercentage = function () {
var totalPixels = 2048 * 2732;
var scratchedCount = Object.keys(self.scratched).length;
self.revealPercentage = scratchedCount / totalPixels * 100;
};
self.getRevealPercentage = function () {
return self.revealPercentage;
};
self.resetScratch = function () {
self.scratched = {};
self.revealPercentage = 0;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF
});
/****
* Game Code
****/
var currentLevel = storage.currentLevel || 1;
var totalLevels = 28;
var baseTime = 60;
var timeDecrement = 2;
var revealThreshold = 85;
var girlAssets = ['girl1', 'girl2', 'girl3', 'girl4', 'girl5', 'girl6', 'girl7', 'girl8', 'girl9', 'girl10', 'girl11', 'girl12', 'girl13', 'girl14', 'girl15', 'girl16', 'girl17', 'girl18', 'girl19', 'girl20', 'girl21', 'girl22', 'girl23', 'girl24', 'girl25', 'girl26', 'girl27', 'girl28'];
var overlayColors = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
var timeRemaining = baseTime - (currentLevel - 1) * timeDecrement;
var isScrubbing = false;
var girlImage = game.addChild(LK.getAsset(girlAssets[currentLevel - 1], {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
}));
var scratchOverlay = game.addChild(new ScratchOverlay());
scratchOverlay.setOverlayColor(overlayColors[currentLevel - 1]);
var timerText = new Text2(timeRemaining.toString(), {
size: 120,
fill: 0x000000
});
timerText.anchor.set(0.5, 0.5);
LK.gui.top.addChild(timerText);
var levelText = new Text2('Level ' + currentLevel + '/' + totalLevels, {
size: 80,
fill: 0x000000
});
levelText.anchor.set(0.5, 0);
LK.gui.top.addChild(levelText);
var revealText = new Text2('0%', {
size: 100,
fill: 0x000000
});
revealText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(revealText);
var timerInterval = LK.setInterval(function () {
timeRemaining--;
timerText.setText(Math.max(0, timeRemaining).toString());
if (timeRemaining <= 0) {
LK.clearInterval(timerInterval);
storage.currentLevel = 1;
LK.effects.flashScreen(0xFF0000, 500);
LK.showGameOver();
}
}, 1000);
game.move = function (x, y, obj) {
if (isScrubbing) {
var gameCoords = {
x: x,
y: y
};
scratchOverlay.scratchArea(gameCoords.x, gameCoords.y, 80);
var revealPercent = Math.floor(scratchOverlay.getRevealPercentage());
revealText.setText(revealPercent + '%');
if (revealPercent >= revealThreshold) {
LK.clearInterval(timerInterval);
isScrubbing = false;
LK.getSound('reveal').play();
if (currentLevel >= totalLevels) {
storage.currentLevel = 1;
LK.setTimeout(function () {
LK.showYouWin();
}, 500);
} else {
storage.currentLevel = currentLevel + 1;
LK.setTimeout(function () {
location.reload();
}, 500);
}
}
}
};
game.down = function (x, y, obj) {
isScrubbing = true;
var gameCoords = {
x: x,
y: y
};
scratchOverlay.scratchArea(gameCoords.x, gameCoords.y, 80);
var revealPercent = Math.floor(scratchOverlay.getRevealPercentage());
revealText.setText(revealPercent + '%');
};
game.up = function (x, y, obj) {
isScrubbing = false;
};
game.update = function () {
// Game update logic handled by event listeners and timer
}; ===================================================================
--- original.js
+++ change.js
@@ -118,12 +118,12 @@
}
}, 1000);
game.move = function (x, y, obj) {
if (isScrubbing) {
- var gameCoords = game.toLocal(obj.parent.toGlobal({
+ var gameCoords = {
x: x,
y: y
- }));
+ };
scratchOverlay.scratchArea(gameCoords.x, gameCoords.y, 80);
var revealPercent = Math.floor(scratchOverlay.getRevealPercentage());
revealText.setText(revealPercent + '%');
if (revealPercent >= revealThreshold) {
@@ -145,12 +145,12 @@
}
};
game.down = function (x, y, obj) {
isScrubbing = true;
- var gameCoords = game.toLocal(obj.parent.toGlobal({
+ var gameCoords = {
x: x,
y: y
- }));
+ };
scratchOverlay.scratchArea(gameCoords.x, gameCoords.y, 80);
var revealPercent = Math.floor(scratchOverlay.getRevealPercentage());
revealText.setText(revealPercent + '%');
};