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: 227
Code edit (1 edits merged)
Please save this source code
User prompt
Mine Layer
Initial prompt
I want a game that you place down landmine in
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.pathIndex = 0;
self.lastY = 0;
self.alive = true;
self.update = function () {
if (!self.alive) return;
if (self.pathIndex < enemyPath.length) {
var targetPoint = enemyPath[self.pathIndex];
var dx = targetPoint.x - self.x;
var dy = targetPoint.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.pathIndex++;
if (self.pathIndex >= enemyPath.length) {
self.alive = false;
self.lastY = self.y;
return;
}
} else {
var moveX = dx / distance * self.speed;
var moveY = dy / distance * self.speed;
self.x += moveX;
self.y += moveY;
self.lastY = self.y;
}
}
};
return self;
});
var Mine = Container.expand(function () {
var self = Container.call(this);
var mineGraphics = self.attachAsset('mine', {
anchorX: 0.5,
anchorY: 0.5
});
self.active = true;
self.explosionRadius = 150;
self.update = function () {
if (self.active) {
tween(mineGraphics, {
rotation: mineGraphics.rotation + 0.05
}, {
duration: 16
});
}
};
self.detonate = function () {
self.active = false;
LK.effects.flashObject(self, 0xf39c12, 300);
LK.getSound('explosion').play();
var explosion = game.addChild(LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
}));
explosion.x = self.x;
explosion.y = self.y;
explosion.alpha = 0.8;
tween(explosion, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
explosion.destroy();
}
});
tween(self, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var gridSize = 80;
var gridRows = Math.floor(2732 / gridSize);
var gridCols = Math.floor(2048 / gridSize);
var maxMines = 15;
var minesRemaining = maxMines;
var currentWave = 1;
var enemiesPerWave = 3;
var waveSpawnDelay = 1500;
var score = 0;
var gameActive = true;
var mines = [];
var enemies = [];
var explosions = [];
var waveInProgress = false;
var spawnTimer = null;
var enemiesSpawned = 0;
var enemyPath = [{
x: 2048 / 2,
y: 0
}, {
x: 2048 / 2,
y: 500
}, {
x: 300,
y: 700
}, {
x: 1748,
y: 900
}, {
x: 2048 / 2,
y: 1200
}, {
x: 400,
y: 1500
}, {
x: 1648,
y: 1800
}, {
x: 2048 / 2,
y: 2100
}, {
x: 2048 / 2,
y: 2732
}];
var goalZone = game.addChild(LK.getAsset('goalZone', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.6
}));
goalZone.x = enemyPath[enemyPath.length - 1].x;
goalZone.y = enemyPath[enemyPath.length - 1].y;
var scoreText = new Text2('Score: 0', {
size: 80,
fill: '#ffffff'
});
scoreText.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreText);
var waveText = new Text2('Wave: 1', {
size: 80,
fill: '#ffffff'
});
waveText.anchor.set(0, 0);
LK.gui.top.addChild(waveText);
var mineText = new Text2('Mines: 15', {
size: 70,
fill: '#ffffff'
});
mineText.anchor.set(0, 0);
LK.gui.topRight.addChild(mineText);
function updateUI() {
scoreText.setText('Score: ' + score);
waveText.setText('Wave: ' + currentWave);
mineText.setText('Mines: ' + minesRemaining);
}
function spawnEnemyWave() {
if (!gameActive) return;
waveInProgress = true;
enemiesSpawned = 0;
var enemyCount = Math.min(enemiesPerWave + Math.floor(currentWave / 2), 10);
spawnTimer = LK.setInterval(function () {
if (enemiesSpawned >= enemyCount) {
LK.clearInterval(spawnTimer);
spawnTimer = null;
LK.setTimeout(function () {
if (gameActive && enemies.length === 0) {
currentWave++;
enemiesPerWave = 3 + currentWave;
spawnEnemyWave();
}
}, 2000);
return;
}
var enemy = new Enemy();
enemy.x = enemyPath[0].x;
enemy.y = enemyPath[0].y;
enemy.speed = 1 + currentWave * 0.3;
game.addChild(enemy);
enemies.push(enemy);
enemiesSpawned++;
}, waveSpawnDelay);
}
game.down = function (x, y, obj) {
if (!gameActive || minesRemaining <= 0) return;
var gameCoords = game.toLocal(obj.parent.toGlobal({
x: x,
y: y
}));
var gridX = Math.floor(gameCoords.x / gridSize);
var gridY = Math.floor(gameCoords.y / gridSize);
var mineX = gridX * gridSize + gridSize / 2;
var mineY = gridY * gridSize + gridSize / 2;
var alreadyExists = false;
for (var i = 0; i < mines.length; i++) {
var dist = Math.sqrt(Math.pow(mines[i].x - mineX, 2) + Math.pow(mines[i].y - mineY, 2));
if (dist < 40) {
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
var mine = new Mine();
mine.x = mineX;
mine.y = mineY;
game.addChild(mine);
mines.push(mine);
minesRemaining--;
LK.getSound('minePlace').play();
updateUI();
}
};
game.update = function () {
if (!gameActive) return;
for (var i = mines.length - 1; i >= 0; i--) {
mines[i].update();
}
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
enemy.update();
if (!enemy.alive) {
var reachedGoal = enemy.pathIndex >= enemyPath.length;
if (reachedGoal) {
LK.effects.flashScreen(0xff0000, 500);
gameActive = false;
LK.getSound('enemyReach').play();
LK.showGameOver();
} else {
enemy.destroy();
enemies.splice(i, 1);
}
continue;
}
var mineDetonated = false;
for (var j = mines.length - 1; j >= 0; j--) {
var mine = mines[j];
if (!mine.active) continue;
var dx = mine.x - enemy.x;
var dy = mine.y - enemy.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < mine.explosionRadius) {
mine.detonate();
mines.splice(j, 1);
for (var k = enemies.length - 1; k >= 0; k--) {
var otherEnemy = enemies[k];
if (!otherEnemy.alive) continue;
var edx = mine.x - otherEnemy.x;
var edy = mine.y - otherEnemy.y;
var edist = Math.sqrt(edx * edx + edy * edy);
if (edist < mine.explosionRadius) {
otherEnemy.alive = false;
otherEnemy.destroy();
score += 10;
updateUI();
enemies.splice(k, 1);
}
}
mineDetonated = true;
break;
}
}
if (mineDetonated) {
enemy.alive = false;
enemy.destroy();
enemies.splice(i, 1);
score += 10;
updateUI();
}
}
if (!waveInProgress && enemies.length === 0) {
spawnEnemyWave();
}
};
LK.playMusic('bgmusic');
spawnEnemyWave();
updateUI(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,298 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Enemy = Container.expand(function () {
+ var self = Container.call(this);
+ var enemyGraphics = self.attachAsset('enemy', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2;
+ self.pathIndex = 0;
+ self.lastY = 0;
+ self.alive = true;
+ self.update = function () {
+ if (!self.alive) return;
+ if (self.pathIndex < enemyPath.length) {
+ var targetPoint = enemyPath[self.pathIndex];
+ var dx = targetPoint.x - self.x;
+ var dy = targetPoint.y - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < self.speed) {
+ self.pathIndex++;
+ if (self.pathIndex >= enemyPath.length) {
+ self.alive = false;
+ self.lastY = self.y;
+ return;
+ }
+ } else {
+ var moveX = dx / distance * self.speed;
+ var moveY = dy / distance * self.speed;
+ self.x += moveX;
+ self.y += moveY;
+ self.lastY = self.y;
+ }
+ }
+ };
+ return self;
+});
+var Mine = Container.expand(function () {
+ var self = Container.call(this);
+ var mineGraphics = self.attachAsset('mine', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.active = true;
+ self.explosionRadius = 150;
+ self.update = function () {
+ if (self.active) {
+ tween(mineGraphics, {
+ rotation: mineGraphics.rotation + 0.05
+ }, {
+ duration: 16
+ });
+ }
+ };
+ self.detonate = function () {
+ self.active = false;
+ LK.effects.flashObject(self, 0xf39c12, 300);
+ LK.getSound('explosion').play();
+ var explosion = game.addChild(LK.getAsset('explosion', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ }));
+ explosion.x = self.x;
+ explosion.y = self.y;
+ explosion.alpha = 0.8;
+ tween(explosion, {
+ alpha: 0,
+ scaleX: 2,
+ scaleY: 2
+ }, {
+ duration: 300,
+ onFinish: function onFinish() {
+ explosion.destroy();
+ }
+ });
+ tween(self, {
+ alpha: 0
+ }, {
+ duration: 300,
+ onFinish: function onFinish() {
+ self.destroy();
+ }
+ });
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a1a1a
+});
+
+/****
+* Game Code
+****/
+var gridSize = 80;
+var gridRows = Math.floor(2732 / gridSize);
+var gridCols = Math.floor(2048 / gridSize);
+var maxMines = 15;
+var minesRemaining = maxMines;
+var currentWave = 1;
+var enemiesPerWave = 3;
+var waveSpawnDelay = 1500;
+var score = 0;
+var gameActive = true;
+var mines = [];
+var enemies = [];
+var explosions = [];
+var waveInProgress = false;
+var spawnTimer = null;
+var enemiesSpawned = 0;
+var enemyPath = [{
+ x: 2048 / 2,
+ y: 0
+}, {
+ x: 2048 / 2,
+ y: 500
+}, {
+ x: 300,
+ y: 700
+}, {
+ x: 1748,
+ y: 900
+}, {
+ x: 2048 / 2,
+ y: 1200
+}, {
+ x: 400,
+ y: 1500
+}, {
+ x: 1648,
+ y: 1800
+}, {
+ x: 2048 / 2,
+ y: 2100
+}, {
+ x: 2048 / 2,
+ y: 2732
+}];
+var goalZone = game.addChild(LK.getAsset('goalZone', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.6
+}));
+goalZone.x = enemyPath[enemyPath.length - 1].x;
+goalZone.y = enemyPath[enemyPath.length - 1].y;
+var scoreText = new Text2('Score: 0', {
+ size: 80,
+ fill: '#ffffff'
+});
+scoreText.anchor.set(0, 0);
+LK.gui.topRight.addChild(scoreText);
+var waveText = new Text2('Wave: 1', {
+ size: 80,
+ fill: '#ffffff'
+});
+waveText.anchor.set(0, 0);
+LK.gui.top.addChild(waveText);
+var mineText = new Text2('Mines: 15', {
+ size: 70,
+ fill: '#ffffff'
+});
+mineText.anchor.set(0, 0);
+LK.gui.topRight.addChild(mineText);
+function updateUI() {
+ scoreText.setText('Score: ' + score);
+ waveText.setText('Wave: ' + currentWave);
+ mineText.setText('Mines: ' + minesRemaining);
+}
+function spawnEnemyWave() {
+ if (!gameActive) return;
+ waveInProgress = true;
+ enemiesSpawned = 0;
+ var enemyCount = Math.min(enemiesPerWave + Math.floor(currentWave / 2), 10);
+ spawnTimer = LK.setInterval(function () {
+ if (enemiesSpawned >= enemyCount) {
+ LK.clearInterval(spawnTimer);
+ spawnTimer = null;
+ LK.setTimeout(function () {
+ if (gameActive && enemies.length === 0) {
+ currentWave++;
+ enemiesPerWave = 3 + currentWave;
+ spawnEnemyWave();
+ }
+ }, 2000);
+ return;
+ }
+ var enemy = new Enemy();
+ enemy.x = enemyPath[0].x;
+ enemy.y = enemyPath[0].y;
+ enemy.speed = 1 + currentWave * 0.3;
+ game.addChild(enemy);
+ enemies.push(enemy);
+ enemiesSpawned++;
+ }, waveSpawnDelay);
+}
+game.down = function (x, y, obj) {
+ if (!gameActive || minesRemaining <= 0) return;
+ var gameCoords = game.toLocal(obj.parent.toGlobal({
+ x: x,
+ y: y
+ }));
+ var gridX = Math.floor(gameCoords.x / gridSize);
+ var gridY = Math.floor(gameCoords.y / gridSize);
+ var mineX = gridX * gridSize + gridSize / 2;
+ var mineY = gridY * gridSize + gridSize / 2;
+ var alreadyExists = false;
+ for (var i = 0; i < mines.length; i++) {
+ var dist = Math.sqrt(Math.pow(mines[i].x - mineX, 2) + Math.pow(mines[i].y - mineY, 2));
+ if (dist < 40) {
+ alreadyExists = true;
+ break;
+ }
+ }
+ if (!alreadyExists) {
+ var mine = new Mine();
+ mine.x = mineX;
+ mine.y = mineY;
+ game.addChild(mine);
+ mines.push(mine);
+ minesRemaining--;
+ LK.getSound('minePlace').play();
+ updateUI();
+ }
+};
+game.update = function () {
+ if (!gameActive) return;
+ for (var i = mines.length - 1; i >= 0; i--) {
+ mines[i].update();
+ }
+ for (var i = enemies.length - 1; i >= 0; i--) {
+ var enemy = enemies[i];
+ enemy.update();
+ if (!enemy.alive) {
+ var reachedGoal = enemy.pathIndex >= enemyPath.length;
+ if (reachedGoal) {
+ LK.effects.flashScreen(0xff0000, 500);
+ gameActive = false;
+ LK.getSound('enemyReach').play();
+ LK.showGameOver();
+ } else {
+ enemy.destroy();
+ enemies.splice(i, 1);
+ }
+ continue;
+ }
+ var mineDetonated = false;
+ for (var j = mines.length - 1; j >= 0; j--) {
+ var mine = mines[j];
+ if (!mine.active) continue;
+ var dx = mine.x - enemy.x;
+ var dy = mine.y - enemy.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < mine.explosionRadius) {
+ mine.detonate();
+ mines.splice(j, 1);
+ for (var k = enemies.length - 1; k >= 0; k--) {
+ var otherEnemy = enemies[k];
+ if (!otherEnemy.alive) continue;
+ var edx = mine.x - otherEnemy.x;
+ var edy = mine.y - otherEnemy.y;
+ var edist = Math.sqrt(edx * edx + edy * edy);
+ if (edist < mine.explosionRadius) {
+ otherEnemy.alive = false;
+ otherEnemy.destroy();
+ score += 10;
+ updateUI();
+ enemies.splice(k, 1);
+ }
+ }
+ mineDetonated = true;
+ break;
+ }
+ }
+ if (mineDetonated) {
+ enemy.alive = false;
+ enemy.destroy();
+ enemies.splice(i, 1);
+ score += 10;
+ updateUI();
+ }
+ }
+ if (!waveInProgress && enemies.length === 0) {
+ spawnEnemyWave();
+ }
+};
+LK.playMusic('bgmusic');
+spawnEnemyWave();
+updateUI();
\ No newline at end of file
landmine. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
landmine . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
triangle. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
alien. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat