User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'fill')' in or related to this line: 'realmTxt.style.fill = "#663399";' Line Number: 226
Code edit (1 edits merged)
Please save this source code
User prompt
Shadow Runner
Initial prompt
Shadow Runner is an endless action-adventure game where players control a mysterious runner who exists between light and darkness. The player must navigate through a shifting world where platforms, traps, and enemies appear depending on whether the runner is in the Light Realm or the Shadow Realm.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Enemy = Container.expand(function (type, x, y) {
var self = Container.call(this);
self.type = type;
self.isVisible = false;
var enemyGraphics = self.attachAsset(type === 'light' ? 'lightEnemy' : 'shadowEnemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.updateVisibility = function () {
if (self.type === 'light') {
self.isVisible = currentRealm === 'light';
} else {
self.isVisible = currentRealm === 'shadow';
}
enemyGraphics.alpha = self.isVisible ? 1.0 : 0.3;
};
self.update = function () {
self.x -= gameSpeed;
self.updateVisibility();
if (self.isVisible && runner.intersects(self)) {
LK.getSound('hit').play();
gameOver();
}
if (self.x < -100) {
self.destroy();
for (var i = enemies.length - 1; i >= 0; i--) {
if (enemies[i] === self) {
enemies.splice(i, 1);
break;
}
}
}
};
return self;
});
var Platform = Container.expand(function (type, x, y) {
var self = Container.call(this);
self.type = type;
self.isVisible = false;
var platformGraphics = self.attachAsset(type === 'light' ? 'lightPlatform' : 'shadowPlatform', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.updateVisibility = function () {
if (self.type === 'light') {
self.isVisible = currentRealm === 'light';
} else {
self.isVisible = currentRealm === 'shadow';
}
platformGraphics.alpha = self.isVisible ? 1.0 : 0.3;
};
self.update = function () {
self.x -= gameSpeed;
self.updateVisibility();
if (self.x < -200) {
self.destroy();
for (var i = platforms.length - 1; i >= 0; i--) {
if (platforms[i] === self) {
platforms.splice(i, 1);
break;
}
}
}
};
return self;
});
var PowerUp = Container.expand(function (x, y) {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.collected = false;
self.update = function () {
self.x -= gameSpeed;
self.rotation += 0.1;
if (!self.collected && runner.intersects(self)) {
self.collected = true;
LK.getSound('collect').play();
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
self.destroy();
for (var i = powerups.length - 1; i >= 0; i--) {
if (powerups[i] === self) {
powerups.splice(i, 1);
break;
}
}
}
if (self.x < -50) {
self.destroy();
for (var i = powerups.length - 1; i >= 0; i--) {
if (powerups[i] === self) {
powerups.splice(i, 1);
break;
}
}
}
};
return self;
});
var Runner = Container.expand(function () {
var self = Container.call(this);
var runnerGraphics = self.attachAsset('runner', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.gravity = 0.8;
self.jumpPower = -18;
self.onGround = false;
self.lastGrounded = false;
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
self.y += self.velocityY;
// Check platform collisions
self.onGround = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (platform.isVisible && self.intersects(platform)) {
if (self.velocityY > 0 && self.y - 80 < platform.y) {
self.y = platform.y;
self.velocityY = 0;
self.onGround = true;
break;
}
}
}
// Check if fell off screen
if (self.y > 2732 + 100) {
gameOver();
}
self.lastGrounded = self.onGround;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var currentRealm = 'light';
var gameSpeed = 8;
var runner;
var platforms = [];
var enemies = [];
var powerups = [];
var spawnTimer = 0;
var gameRunning = true;
var distance = 0;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 150;
scoreTxt.y = 50;
LK.gui.topLeft.addChild(scoreTxt);
var realmTxt = new Text2('Light Realm', {
size: 50,
fill: 0xFFD700
});
realmTxt.anchor.set(0.5, 0);
realmTxt.x = 0;
realmTxt.y = 50;
LK.gui.top.addChild(realmTxt);
var distanceTxt = new Text2('Distance: 0', {
size: 40,
fill: 0xFFFFFF
});
distanceTxt.anchor.set(1, 0);
distanceTxt.x = -20;
distanceTxt.y = 50;
LK.gui.topRight.addChild(distanceTxt);
// Initialize runner
runner = game.addChild(new Runner());
runner.x = 300;
runner.y = 1500;
// Create initial platforms
for (var i = 0; i < 10; i++) {
var platformType = Math.random() < 0.5 ? 'light' : 'shadow';
var platform = game.addChild(new Platform(platformType, 400 + i * 300, 1500 + Math.random() * 400 - 200));
platforms.push(platform);
}
function switchRealm() {
if (currentRealm === 'light') {
currentRealm = 'shadow';
game.setBackgroundColor(0x2c1810);
realmTxt.setText('Shadow Realm');
realmTxt.style.fill = "#663399";
} else {
currentRealm = 'light';
game.setBackgroundColor(0x87ceeb);
realmTxt.setText('Light Realm');
realmTxt.style.fill = "#ffd700";
}
LK.getSound('realmSwitch').play();
LK.effects.flashScreen(currentRealm === 'light' ? 0xffd700 : 0x663399, 200);
}
function jump() {
if (runner.onGround) {
runner.velocityY = runner.jumpPower;
LK.getSound('jump').play();
}
}
function spawnPlatform() {
var platformType = Math.random() < 0.5 ? 'light' : 'shadow';
var platform = game.addChild(new Platform(platformType, 2200, 1200 + Math.random() * 800));
platforms.push(platform);
}
function spawnEnemy() {
if (Math.random() < 0.3) {
var enemyType = Math.random() < 0.5 ? 'light' : 'shadow';
var enemy = game.addChild(new Enemy(enemyType, 2200, 1400 + Math.random() * 600));
enemies.push(enemy);
}
}
function spawnPowerUp() {
if (Math.random() < 0.15) {
var powerup = game.addChild(new PowerUp(2200, 1300 + Math.random() * 700));
powerups.push(powerup);
}
}
function gameOver() {
if (gameRunning) {
gameRunning = false;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Event handlers
game.down = function (x, y, obj) {
if (gameRunning) {
if (y < 1366) {
switchRealm();
} else {
jump();
}
}
};
// Main game loop
game.update = function () {
if (!gameRunning) return;
// Update distance and score
distance += gameSpeed;
if (LK.ticks % 10 === 0) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
distanceTxt.setText('Distance: ' + Math.floor(distance / 10));
}
// Increase difficulty over time
if (LK.ticks % 600 === 0) {
gameSpeed += 0.5;
}
// Spawn new elements
spawnTimer++;
if (spawnTimer >= 60) {
spawnPlatform();
spawnEnemy();
spawnPowerUp();
spawnTimer = 0;
}
// Update all platforms visibility
for (var i = 0; i < platforms.length; i++) {
platforms[i].updateVisibility();
}
// Update all enemies visibility
for (var i = 0; i < enemies.length; i++) {
enemies[i].updateVisibility();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,293 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Enemy = Container.expand(function (type, x, y) {
+ var self = Container.call(this);
+ self.type = type;
+ self.isVisible = false;
+ var enemyGraphics = self.attachAsset(type === 'light' ? 'lightEnemy' : 'shadowEnemy', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.x = x;
+ self.y = y;
+ self.updateVisibility = function () {
+ if (self.type === 'light') {
+ self.isVisible = currentRealm === 'light';
+ } else {
+ self.isVisible = currentRealm === 'shadow';
+ }
+ enemyGraphics.alpha = self.isVisible ? 1.0 : 0.3;
+ };
+ self.update = function () {
+ self.x -= gameSpeed;
+ self.updateVisibility();
+ if (self.isVisible && runner.intersects(self)) {
+ LK.getSound('hit').play();
+ gameOver();
+ }
+ if (self.x < -100) {
+ self.destroy();
+ for (var i = enemies.length - 1; i >= 0; i--) {
+ if (enemies[i] === self) {
+ enemies.splice(i, 1);
+ break;
+ }
+ }
+ }
+ };
+ return self;
+});
+var Platform = Container.expand(function (type, x, y) {
+ var self = Container.call(this);
+ self.type = type;
+ self.isVisible = false;
+ var platformGraphics = self.attachAsset(type === 'light' ? 'lightPlatform' : 'shadowPlatform', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.x = x;
+ self.y = y;
+ self.updateVisibility = function () {
+ if (self.type === 'light') {
+ self.isVisible = currentRealm === 'light';
+ } else {
+ self.isVisible = currentRealm === 'shadow';
+ }
+ platformGraphics.alpha = self.isVisible ? 1.0 : 0.3;
+ };
+ self.update = function () {
+ self.x -= gameSpeed;
+ self.updateVisibility();
+ if (self.x < -200) {
+ self.destroy();
+ for (var i = platforms.length - 1; i >= 0; i--) {
+ if (platforms[i] === self) {
+ platforms.splice(i, 1);
+ break;
+ }
+ }
+ }
+ };
+ return self;
+});
+var PowerUp = Container.expand(function (x, y) {
+ var self = Container.call(this);
+ var powerupGraphics = self.attachAsset('powerup', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.x = x;
+ self.y = y;
+ self.collected = false;
+ self.update = function () {
+ self.x -= gameSpeed;
+ self.rotation += 0.1;
+ if (!self.collected && runner.intersects(self)) {
+ self.collected = true;
+ LK.getSound('collect').play();
+ LK.setScore(LK.getScore() + 10);
+ scoreTxt.setText(LK.getScore());
+ self.destroy();
+ for (var i = powerups.length - 1; i >= 0; i--) {
+ if (powerups[i] === self) {
+ powerups.splice(i, 1);
+ break;
+ }
+ }
+ }
+ if (self.x < -50) {
+ self.destroy();
+ for (var i = powerups.length - 1; i >= 0; i--) {
+ if (powerups[i] === self) {
+ powerups.splice(i, 1);
+ break;
+ }
+ }
+ }
+ };
+ return self;
+});
+var Runner = Container.expand(function () {
+ var self = Container.call(this);
+ var runnerGraphics = self.attachAsset('runner', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.velocityY = 0;
+ self.gravity = 0.8;
+ self.jumpPower = -18;
+ self.onGround = false;
+ self.lastGrounded = false;
+ self.update = function () {
+ // Apply gravity
+ self.velocityY += self.gravity;
+ self.y += self.velocityY;
+ // Check platform collisions
+ self.onGround = false;
+ for (var i = 0; i < platforms.length; i++) {
+ var platform = platforms[i];
+ if (platform.isVisible && self.intersects(platform)) {
+ if (self.velocityY > 0 && self.y - 80 < platform.y) {
+ self.y = platform.y;
+ self.velocityY = 0;
+ self.onGround = true;
+ break;
+ }
+ }
+ }
+ // Check if fell off screen
+ if (self.y > 2732 + 100) {
+ gameOver();
+ }
+ self.lastGrounded = self.onGround;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87ceeb
+});
+
+/****
+* Game Code
+****/
+var currentRealm = 'light';
+var gameSpeed = 8;
+var runner;
+var platforms = [];
+var enemies = [];
+var powerups = [];
+var spawnTimer = 0;
+var gameRunning = true;
+var distance = 0;
+// UI Elements
+var scoreTxt = new Text2('Score: 0', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0, 0);
+scoreTxt.x = 150;
+scoreTxt.y = 50;
+LK.gui.topLeft.addChild(scoreTxt);
+var realmTxt = new Text2('Light Realm', {
+ size: 50,
+ fill: 0xFFD700
+});
+realmTxt.anchor.set(0.5, 0);
+realmTxt.x = 0;
+realmTxt.y = 50;
+LK.gui.top.addChild(realmTxt);
+var distanceTxt = new Text2('Distance: 0', {
+ size: 40,
+ fill: 0xFFFFFF
+});
+distanceTxt.anchor.set(1, 0);
+distanceTxt.x = -20;
+distanceTxt.y = 50;
+LK.gui.topRight.addChild(distanceTxt);
+// Initialize runner
+runner = game.addChild(new Runner());
+runner.x = 300;
+runner.y = 1500;
+// Create initial platforms
+for (var i = 0; i < 10; i++) {
+ var platformType = Math.random() < 0.5 ? 'light' : 'shadow';
+ var platform = game.addChild(new Platform(platformType, 400 + i * 300, 1500 + Math.random() * 400 - 200));
+ platforms.push(platform);
+}
+function switchRealm() {
+ if (currentRealm === 'light') {
+ currentRealm = 'shadow';
+ game.setBackgroundColor(0x2c1810);
+ realmTxt.setText('Shadow Realm');
+ realmTxt.style.fill = "#663399";
+ } else {
+ currentRealm = 'light';
+ game.setBackgroundColor(0x87ceeb);
+ realmTxt.setText('Light Realm');
+ realmTxt.style.fill = "#ffd700";
+ }
+ LK.getSound('realmSwitch').play();
+ LK.effects.flashScreen(currentRealm === 'light' ? 0xffd700 : 0x663399, 200);
+}
+function jump() {
+ if (runner.onGround) {
+ runner.velocityY = runner.jumpPower;
+ LK.getSound('jump').play();
+ }
+}
+function spawnPlatform() {
+ var platformType = Math.random() < 0.5 ? 'light' : 'shadow';
+ var platform = game.addChild(new Platform(platformType, 2200, 1200 + Math.random() * 800));
+ platforms.push(platform);
+}
+function spawnEnemy() {
+ if (Math.random() < 0.3) {
+ var enemyType = Math.random() < 0.5 ? 'light' : 'shadow';
+ var enemy = game.addChild(new Enemy(enemyType, 2200, 1400 + Math.random() * 600));
+ enemies.push(enemy);
+ }
+}
+function spawnPowerUp() {
+ if (Math.random() < 0.15) {
+ var powerup = game.addChild(new PowerUp(2200, 1300 + Math.random() * 700));
+ powerups.push(powerup);
+ }
+}
+function gameOver() {
+ if (gameRunning) {
+ gameRunning = false;
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+}
+// Event handlers
+game.down = function (x, y, obj) {
+ if (gameRunning) {
+ if (y < 1366) {
+ switchRealm();
+ } else {
+ jump();
+ }
+ }
+};
+// Main game loop
+game.update = function () {
+ if (!gameRunning) return;
+ // Update distance and score
+ distance += gameSpeed;
+ if (LK.ticks % 10 === 0) {
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ distanceTxt.setText('Distance: ' + Math.floor(distance / 10));
+ }
+ // Increase difficulty over time
+ if (LK.ticks % 600 === 0) {
+ gameSpeed += 0.5;
+ }
+ // Spawn new elements
+ spawnTimer++;
+ if (spawnTimer >= 60) {
+ spawnPlatform();
+ spawnEnemy();
+ spawnPowerUp();
+ spawnTimer = 0;
+ }
+ // Update all platforms visibility
+ for (var i = 0; i < platforms.length; i++) {
+ platforms[i].updateVisibility();
+ }
+ // Update all enemies visibility
+ for (var i = 0; i < enemies.length; i++) {
+ enemies[i].updateVisibility();
+ }
+};
\ No newline at end of file