User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'destroy')' in or related to this line: 'lives[self.lives].destroy(); // Remove one life image' Line Number: 135
Code edit (2 edits merged)
Please save this source code
User prompt
add a dispawn for car2 same as for car
User prompt
add new button that moves player on 200 px up and another that get it back
User prompt
add car2 class same as the car but speed = 4 and add car2 to spawnVehicle
User prompt
when pause button green all cars and buses stops when it's red they go on
Code edit (1 edits merged)
Please save this source code
User prompt
add a function to disspawn cars and buses then the player cant see them
User prompt
then the game starts show the menu with buttons start and exit
User prompt
add the main menu
User prompt
show life in left upper corner
User prompt
show lives as three small images
User prompt
show buttons
User prompt
set lines as a background image
User prompt
set lane as a background image
User prompt
if car touch another car second car get first car speed
Code edit (1 edits merged)
Please save this source code
User prompt
player starts on 3 line
User prompt
add animation when player moves
User prompt
add pause/play button
User prompt
Please fix the bug: 'Uncaught TypeError: window.addEventListener is not a function' in or related to this line: 'window.addEventListener('keydown', function (event) {' Line Number: 119
User prompt
add control by keyboard arrows
User prompt
when player touch the cars and buses player has minus one live if lives = 0 game over
User prompt
add player 3 lives and show them in upper left side
/**** * Classes ****/ // Bus class var Bus = Container.expand(function () { var self = Container.call(this); var busGraphics = self.attachAsset('bus', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Assets will be automatically created and loaded during gameplay // Car class var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Car2 class var Car2 = Container.expand(function () { var self = Container.call(this); var car2Graphics = self.attachAsset('car2', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Left Move Button class var LeftMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.moveLeft(); }; }); // Life class var Life = Container.expand(function () { var self = Container.call(this); var lifeGraphics = self.attachAsset('life', { anchorX: 0.5, anchorY: 0.5 }); }); // Pause/Play Button class var PausePlayButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.isPaused = false; self.update = function () { if (self.isPaused) { buttonGraphics.tint = 0x00ff00; // Green for play } else { buttonGraphics.tint = 0xff0000; // Red for pause } }; self.down = function () { self.isPaused = !self.isPaused; LK.setPaused(self.isPaused); }; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.lane = 2; self.lives = 3; self.moveLeft = function () { if (self.lane > 1) { self.lane--; self.x = self.lane * 512 - 256; } }; self.moveRight = function () { if (self.lane < 4) { self.lane++; self.x = self.lane * 512 - 256; } }; self.loseLife = function () { self.lives--; lives[self.lives].destroy(); // Remove one life image lives.splice(self.lives, 1); if (self.lives <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }; }); // Right Move Button class var RightMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.moveRight(); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Set lines as a background image var background = game.attachAsset('lines', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); var player = game.addChild(new Player()); var pausePlayButton = game.addChild(new PausePlayButton()); pausePlayButton.x = 2048 - 100; // Position the button at the top-right corner pausePlayButton.y = 100; var leftMoveButton = game.addChild(new LeftMoveButton()); leftMoveButton.x = 100; // Position the button at the bottom-left corner leftMoveButton.y = 2732 - 100; var rightMoveButton = game.addChild(new RightMoveButton()); rightMoveButton.x = 2048 - 100; // Position the button at the bottom-right corner rightMoveButton.y = 2732 - 100; player.x = 2 * 512 - 256; // Set player's initial x position to the third lane player.y = 2400; var cars = []; var buses = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var lives = []; for (var i = 0; i < 3; i++) { var life = game.addChild(new Life()); life.x = (i + 1) * 60; // Position the life images at the top-left corner life.y = 60; lives.push(life); } function spawnVehicle() { var lane = Math.floor(Math.random() * 4) + 1; var vehicle; if (Math.random() < 0.33) { vehicle = new Car(); cars.push(vehicle); } else if (Math.random() < 0.66) { vehicle = new Car2(); cars.push(vehicle); } else { vehicle = new Bus(); buses.push(vehicle); } vehicle.x = lane * 512 - 256; vehicle.y = -100; game.addChild(vehicle); } // Removed game's down event handler game.update = function () { for (var i = cars.length - 1; i >= 0; i--) { if (cars[i].intersects(player)) { player.loseLife(); cars[i].destroy(); cars.splice(i, 1); continue; } for (var j = i - 1; j >= 0; j--) { if (cars[i].intersects(cars[j])) { cars[j].speed = cars[i].speed; } } for (var j = buses.length - 1; j >= 0; j--) { if (cars[i].intersects(buses[j])) { cars[i].speed = buses[j].speed; } } // Despawn the car if it is off-screen if (cars[i].y > 2732) { cars[i].destroy(); cars.splice(i, 1); continue; } cars[i].update(); } for (var j = buses.length - 1; j >= 0; j--) { if (buses[j].intersects(player)) { player.loseLife(); buses[j].destroy(); buses.splice(j, 1); continue; } // Despawn the bus if it is off-screen if (buses[j].y > 2732) { buses[j].destroy(); buses.splice(j, 1); continue; } buses[j].update(); } if (LK.ticks % Math.max(10, 60 - Math.floor(score / 10)) == 0) { spawnVehicle(); score++; scoreTxt.setText(score); } };
===================================================================
--- original.js
+++ change.js
@@ -8,13 +8,10 @@
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
- self.isPaused = false;
self.update = function () {
- if (!self.isPaused) {
- self.y += self.speed;
- }
+ self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
@@ -27,13 +24,25 @@
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
- self.isPaused = false;
self.update = function () {
- if (!self.isPaused) {
- self.y += self.speed;
+ self.y += self.speed;
+ if (self.y > 2732) {
+ self.destroy();
}
+ };
+});
+// Car2 class
+var Car2 = Container.expand(function () {
+ var self = Container.call(this);
+ var car2Graphics = self.attachAsset('car2', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 4;
+ self.update = function () {
+ self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
@@ -74,14 +83,8 @@
};
self.down = function () {
self.isPaused = !self.isPaused;
LK.setPaused(self.isPaused);
- for (var i = 0; i < cars.length; i++) {
- cars[i].isPaused = self.isPaused;
- }
- for (var i = 0; i < buses.length; i++) {
- buses[i].isPaused = self.isPaused;
- }
};
});
// Player class
var Player = Container.expand(function () {
@@ -173,11 +176,14 @@
}
function spawnVehicle() {
var lane = Math.floor(Math.random() * 4) + 1;
var vehicle;
- if (Math.random() < 0.5) {
+ if (Math.random() < 0.33) {
vehicle = new Car();
cars.push(vehicle);
+ } else if (Math.random() < 0.66) {
+ vehicle = new Car2();
+ cars.push(vehicle);
} else {
vehicle = new Bus();
buses.push(vehicle);
}
delete white line
four-lane road on full image. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
left turn signal of the car. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
round car steering wheel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.