User prompt
If the obstacle in the right lane,spawn another obstacle to right.if the obstacle in the left lane spawn another obstacle to left
User prompt
And add another obstacle the side of obstacles(make two obstacles in one lane)
User prompt
Make the obstacles spawn to dotted line closer
User prompt
Add tap to switch lanes logic back
User prompt
Remove the car outside logic
User prompt
Make the car go outside of the road when tapped on screen and make it to normal position again
User prompt
Remove the menu,everything crashed
User prompt
Please fix the bug: 'Cannot set properties of undefined (setting 'visible')' in or related to this line: 'menuBg.visible = true;' Line Number: 200
User prompt
Please fix the bug: 'Cannot set properties of undefined (setting 'visible')' in or related to this line: 'menuBg.visible = true;' Line Number: 199
User prompt
Add a start menu,add a backgroun image to menu and add a play button
User prompt
Also make the car 1.5x bigger
User prompt
Make obstacles bigger and centered in sideways
User prompt
Make the road 4x wide and make the car and obstacles position match the road
User prompt
Make everything in normal size again
User prompt
I just mean the make car auto centering wider,make everything normal
User prompt
Make it more wider for better visual effect
User prompt
Make the car start in the middle when its not touched
User prompt
Add Day night but not headlights
User prompt
Just remove the headlights it makes a lot of bugs
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading '0')' in or related to this line: 'self.y += car.speed;' Line Number: 293
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading '0')' in or related to this line: 'self.y += car.speed;' Line Number: 292
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading '0')' in or related to this line: 'self.y += car.speed;' Line Number: 291
User prompt
Please fix the bug: 'LK.getPolygon is not a function' in or related to this line: 'var poly = LK.getPolygon([[-baseOffset, baseY], [0, tipY], [baseOffset, baseY]], color, alpha);' Line Number: 58
User prompt
Remove the headlights,just make them two long triangles the long tip touching to cars front,and make them yellow
User prompt
Also add car headlights when its night ↪💡 Consider importing and using the following plugins: @upit/tween.v1
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carAsset = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
// Headlights: two yellow triangles (polygons), initially hidden
var headlightLeft = new Container();
var headlightRight = new Container();
// Triangle points: base at car front corners, tip forward
var carW = carAsset.width;
var carH = carAsset.height;
var tipLen = carH * 1.5;
var baseY = carH * 0.5 - 10;
var tipY = carH * 0.5 + tipLen;
var baseOffset = carW * 0.22;
var color = 0xffe066; // bright yellow
// Left triangle
var leftPoly = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: 1,
height: 1,
x: 0,
y: 0
});
leftPoly.visible = false; // Hide the ellipse, we only use the container for polygon
headlightLeft.addChild(leftPoly);
headlightLeft.drawPolygon = function (alpha) {
if (headlightLeft.polygon) {
headlightLeft.polygon.destroy();
headlightLeft.removeChild(headlightLeft.polygon);
}
// Remove previous polygon if exists
var poly = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: 1,
height: 1,
x: 0,
y: 0
});
poly.visible = false;
// Create a mask for the triangle
var mask = new Container();
mask.points = [[-baseOffset, baseY], [0, tipY], [baseOffset, baseY]];
mask.updateMask = function () {
// Remove previous children
while (mask.children.length > 0) mask.removeChild(mask.children[0]);
// Use three very thin rectangles to approximate the triangle edges
for (var i = 0; i < 3; ++i) {
var p1 = mask.points[i];
var p2 = mask.points[(i + 1) % 3];
var dx = p2[0] - p1[0];
var dy = p2[1] - p1[1];
var len = Math.sqrt(dx * dx + dy * dy);
var angle = Math.atan2(dy, dx);
var edge = LK.getAsset('centerCircle', {
anchorX: 0,
anchorY: 0.5,
width: len,
height: 2,
x: p1[0],
y: p1[1]
});
edge.rotation = angle;
edge.color = 0xffffff;
mask.addChild(edge);
}
};
mask.updateMask();
// Create a filled triangle using three rectangles and a colored base
var fill = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: baseOffset * 2,
height: tipY - baseY,
x: 0,
y: (baseY + tipY) / 2
});
fill.color = color;
fill.alpha = alpha;
fill.mask = mask;
headlightLeft.polygon = fill;
headlightLeft.addChild(mask);
headlightLeft.addChild(fill);
};
headlightLeft.x = -carW * 0.22;
headlightLeft.y = carH * 0.5 - 10;
headlightLeft.alpha = 0;
self.addChild(headlightLeft);
// Right triangle
var rightPoly = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: 1,
height: 1,
x: 0,
y: 0
});
rightPoly.visible = false;
headlightRight.addChild(rightPoly);
headlightRight.drawPolygon = function (alpha) {
if (headlightRight.polygon) {
headlightRight.polygon.destroy();
headlightRight.removeChild(headlightRight.polygon);
}
// Remove previous polygon if exists
var poly = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: 1,
height: 1,
x: 0,
y: 0
});
poly.visible = false;
// Create a mask for the triangle
var mask = new Container();
mask.points = [[baseOffset, baseY], [0, tipY], [-baseOffset, baseY]];
mask.updateMask = function () {
// Remove previous children
while (mask.children.length > 0) mask.removeChild(mask.children[0]);
// Use three very thin rectangles to approximate the triangle edges
for (var i = 0; i < 3; ++i) {
var p1 = mask.points[i];
var p2 = mask.points[(i + 1) % 3];
var dx = p2[0] - p1[0];
var dy = p2[1] - p1[1];
var len = Math.sqrt(dx * dx + dy * dy);
var angle = Math.atan2(dy, dx);
var edge = LK.getAsset('centerCircle', {
anchorX: 0,
anchorY: 0.5,
width: len,
height: 2,
x: p1[0],
y: p1[1]
});
edge.rotation = angle;
edge.color = 0xffffff;
mask.addChild(edge);
}
};
mask.updateMask();
// Create a filled triangle using three rectangles and a colored base
var fill = LK.getAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
width: baseOffset * 2,
height: tipY - baseY,
x: 0,
y: (baseY + tipY) / 2
});
fill.color = color;
fill.alpha = alpha;
fill.mask = mask;
headlightRight.polygon = fill;
headlightRight.addChild(mask);
headlightRight.addChild(fill);
};
headlightRight.x = carW * 0.22;
headlightRight.y = carH * 0.5 - 10;
headlightRight.alpha = 0;
self.addChild(headlightRight);
self.headlightLeft = headlightLeft;
self.headlightRight = headlightRight;
// Direction: 1 = up-right (45deg), -1 = up-left (135deg)
self.direction = 1;
self.speed = 32; // initial speed in px per tick
self.angleRight = Math.PI / 4; // 45deg (up-right)
self.angleLeft = 3 * Math.PI / 4; // 135deg (up-left)
carAsset.rotation = self.angleRight;
// Only allow up-left and up-right, always moving up at an angle
self.setDirection = function (dir) {
// Only allow -1 (up-left) or 1 (up-right)
if (dir !== 1 && dir !== -1) return;
if (self.direction === dir) return; // Already in this direction, do nothing
// Animate rotation
tween(carAsset, {
rotation: dir === 1 ? self.angleRight : self.angleLeft
}, {
duration: 120,
easing: tween.cubicOut
});
// Animate lane change (x position)
// Road line is centered at 2048/2, width 960
var roadLineCenter = 2048 / 2;
var roadLineWidth = 960;
var obsMargin = 80;
var obsWidth = 320;
var carTravelEdge = roadLineWidth / 2 - obsWidth / 2 - obsMargin;
var targetX = roadLineCenter + dir * carTravelEdge;
// Stop any previous x tweens
tween.stop(self, {
x: true
});
tween(self, {
x: targetX
}, {
duration: 220,
easing: tween.cubicOut
});
self.direction = dir;
};
// Move car visually left/right (simulate up-left/up-right)
self.moveForward = function () {
// Car moves sideways along the gray road line based on direction
// Road line is centered at 2048/2, width 960
var roadLineCenter = 2048 / 2;
var roadLineWidth = 960;
// Move car to the OUTER side of the grey line (roadLine), not inside it
// The roadLine is 960px wide, the road itself is 600px wide, so the outer edge is at roadLineCenter ± roadLineWidth/2
// Place car so its edge is just outside the roadLine, but still inside the road
var roadWidth = 600;
var carHalfW = self.width / 2;
// Set carTravelEdge so car's edge matches obstacle X offset (so car aligns with obstacles)
// Use the same calculation as obsXOffset in spawnObstacle
var obsMargin = 80;
var obsWidth = 320;
var carTravelEdge = roadLineWidth / 2 - obsWidth / 2 - obsMargin;
// -1 = left, 1 = right
var targetX = roadLineCenter + self.direction * carTravelEdge;
// Smoothly move car toward targetX
var moveSpeed = 0.22; // 0..1, how fast to interpolate
self.x += (targetX - self.x) * moveSpeed;
// Make car always match obstacle's rotation (90deg, PI/2 radians)
carAsset.rotation = Math.PI / 2;
self.lastX = self.x;
self.lastX = self.x;
};
// Reset to center bottom
self.resetPosition = function () {
self.x = 2048 / 2;
self.y = 2732 - 350;
self.setDirection(1);
};
return self;
});
// White dotted line class
var DottedLine = Container.expand(function () {
var self = Container.call(this);
// Each dotted line is a white rectangle (box)
var dotWidth = 32;
var dotHeight = 120;
var dotAsset = self.attachAsset('dottedLine', {
anchorX: 0.5,
anchorY: 0.5
});
// Set size and color
dotAsset.width = dotWidth;
dotAsset.height = dotHeight;
dotAsset.color = 0xffffff;
// Center on road
self.x = 2048 / 2;
// y is set on spawn
self.update = function () {
// Move down at car speed
if (typeof car !== "undefined" && car && typeof car.speed === "number") {
if (typeof car !== "undefined" && car && typeof car.speed === "number") {
self.y += car.speed;
}
}
// Remove if offscreen (handled in game.update)
};
return self;
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
// Randomly pick a color for the obstacle: green, blue, red, or black
var colors = [0x2ecc40, 0x1877f7, 0xd83318, 0x000000];
var colorIdx = Math.floor(Math.random() * colors.length);
// Use a box shape for the obstacle, colored accordingly
var obsAsset = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
obsAsset.shape = 'box';
obsAsset.width = 320;
obsAsset.height = 200;
obsAsset.color = colors[colorIdx];
// Rotate 90 degrees (PI/2 radians) so the rectangle is horizontal
obsAsset.rotation = Math.PI / 2;
// side: -1 = left, 1 = right
self.side = 1;
self.passed = false;
// setSide is no longer needed, obstacles are placed directly on the gray line in spawnObstacle
// Set y position
self.setY = function (y) {
self.y = y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x222222
});
/****
* Game Code
****/
// Road: dark gray box, 600x2732 (full height)
// Obstacle: yellow ellipse, 160x100
// Car: red box, 180x120
// Road
var road = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 0
});
game.addChild(road);
// Add a vertical gray line in the center of the road
var roadLine;
var roadLineAsset = LK.getAsset('roadLine', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 0
});
game.addChild(roadLineAsset);
// Car
// (Drifting line removed)
var car = new Car();
game.addChild(car);
car.resetPosition();
// Obstacles array
var obstacles = [];
// Score
var scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Dotted lines (white, moving down the center of the road)
var dottedLines = [];
var dottedLineHeight = 120;
var dottedLineSpacing = 320;
var numDottedLines = Math.ceil(2732 / dottedLineSpacing) + 2; // enough to cover screen + buffer
for (var i = 0; i < numDottedLines; ++i) {
var dot = new DottedLine();
// Always spawn at the top middle, spaced downwards
dot.x = 2048 / 2;
dot.y = -dottedLineHeight / 2 + i * dottedLineSpacing;
dottedLines.push(dot);
game.addChild(dot);
}
// Game state
var gameOver = false;
var passedObstacles = 0;
var ticksSinceLastObstacle = 0;
var obstacleInterval = 45; // ticks between obstacles (start)
var minObstacleInterval = 18; // minimum interval (faster)
var speedIncreaseEvery = 10; // every 10 obstacles, speed up
var carBaseSpeed = 32;
var carMaxSpeed = 72;
// Night/Day cycle variables
var isNight = false;
var lastNightDayScore = 0;
var nightOverlay = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 0
});
nightOverlay.width = 2048;
nightOverlay.height = 2732;
nightOverlay.color = 0x000000;
nightOverlay.alpha = 0; // Start transparent (day)
game.addChild(nightOverlay);
// Helper: spawn obstacle
function spawnObstacle() {
var obs = new Obstacle();
// Always spawn obstacles on the gray road line, not on the dotted line
// The gray road line is centered at 2048/2, width 960
// Place obstacles randomly left or right of the center gray line, but not overlapping the center (dotted) line
var roadLineCenter = 2048 / 2;
var roadLineWidth = 960;
var obsMargin = 80;
var obsWidth = 320;
var obsXOffset = roadLineWidth / 2 - obsWidth / 2 - obsMargin;
// Randomly left or right of the center gray line
var side = Math.random() < 0.5 ? -1 : 1;
obs.x = roadLineCenter + side * obsXOffset;
obs.y = -120;
obstacles.push(obs);
game.addChild(obs);
}
// Helper: update score
function updateScore() {
scoreTxt.setText(LK.getScore());
}
// Helper: reset game state
function resetGame() {
// Remove all obstacles
for (var i = 0; i < obstacles.length; ++i) {
obstacles[i].destroy();
}
obstacles = [];
car.resetPosition();
car.speed = carBaseSpeed;
obstacleInterval = 90;
passedObstacles = 0;
ticksSinceLastObstacle = 0;
// Reset dotted lines to initial positions
for (var i = 0; i < dottedLines.length; ++i) {
dottedLines[i].x = 2048 / 2;
dottedLines[i].y = -dottedLineHeight / 2 + i * dottedLineSpacing;
}
LK.setScore(0);
updateScore();
gameOver = false;
autoMode = false;
if (typeof updateAutoBtn === "function" && typeof autoBtn !== "undefined") updateAutoBtn();
// Reset night/day cycle
isNight = false;
lastNightDayScore = 0;
tween.stop(nightOverlay, {
alpha: true
});
nightOverlay.alpha = 0;
// Reset triangle headlights
if (car && car.headlightLeft && car.headlightRight) {
tween.stop(car.headlightLeft, {
alpha: true
});
tween.stop(car.headlightRight, {
alpha: true
});
car.headlightLeft.alpha = 0;
car.headlightRight.alpha = 0;
car.headlightLeft.drawPolygon(0);
car.headlightRight.drawPolygon(0);
}
}
// Touch/tap: switch direction (visual only)
game.down = function (x, y, obj) {
if (gameOver) return;
if (autoMode) return;
var newDir = car.direction === 1 ? -1 : 1;
car.setDirection(newDir);
};
// Main game loop
game.update = function () {
if (gameOver) return;
// Move car
if (autoMode && !gameOver) {
// Find the closest obstacle ahead of the car
var closestObs = null;
var minDist = Infinity;
for (var i = 0; i < obstacles.length; ++i) {
var obs = obstacles[i];
if (obs.y > car.y - 200 && obs.y < car.y + 400) {
// only consider obstacles in front
var dist = Math.abs(obs.y - car.y);
if (dist < minDist) {
minDist = dist;
closestObs = obs;
}
}
}
if (closestObs) {
// If the obstacle is on the same side as the car, switch direction
var carSide = car.x > 2048 / 2 ? 1 : -1;
var obsSide = closestObs.x > 2048 / 2 ? 1 : -1;
if (carSide === obsSide) {
car.setDirection(-car.direction);
}
}
}
car.moveForward();
// (Drifting line update removed)
// Keep car inside road bounds
var roadCenter = 2048 / 2;
var roadWidth = 600;
var carHalfW = car.width / 2;
if (car.x < roadCenter - roadWidth / 2 + carHalfW) {
car.x = roadCenter - roadWidth / 2 + carHalfW;
}
if (car.x > roadCenter + roadWidth / 2 - carHalfW) {
car.x = roadCenter + roadWidth / 2 - carHalfW;
}
// Move dotted lines down and handle despawn/spawn
for (var i = dottedLines.length - 1; i >= 0; --i) {
var dot = dottedLines[i];
dot.update(); // Move the dotted line at car.speed
// If offscreen at bottom, despawn and spawn a new one at the top
if (dot.y > 2732 + dottedLineHeight / 2) {
// Remove from game and array
dot.destroy();
dottedLines.splice(i, 1);
// Find minimum y among all remaining dots
var minY = Infinity;
for (var j = 0; j < dottedLines.length; ++j) {
if (dottedLines[j].y < minY) minY = dottedLines[j].y;
}
// Spawn new dot at top middle, spaced above the highest
var newDot = new DottedLine();
newDot.x = 2048 / 2;
// If there are no dots left, just place at top
if (dottedLines.length === 0) {
newDot.y = -dottedLineHeight / 2;
} else {
newDot.y = minY - dottedLineSpacing;
}
dottedLines.push(newDot);
game.addChild(newDot);
}
}
// Move obstacles straight down
for (var i = obstacles.length - 1; i >= 0; --i) {
var obs = obstacles[i];
obs.y += car.speed;
// Check for collision
if (!gameOver && car.intersects(obs)) {
LK.effects.flashScreen(0xff0000, 800);
gameOver = true;
LK.showGameOver();
return;
}
// Check if passed (obstacle is below car, and not already counted)
if (!obs.passed && obs.y > car.y) {
obs.passed = true;
LK.setScore(LK.getScore() + 5);
updateScore();
passedObstacles += 1;
// Speed up every N obstacles
if (passedObstacles % speedIncreaseEvery === 0 && car.speed < carMaxSpeed) {
car.speed += 2;
if (obstacleInterval > minObstacleInterval) {
obstacleInterval -= 6;
if (obstacleInterval < minObstacleInterval) obstacleInterval = minObstacleInterval;
}
}
// --- Night/Day cycle logic ---
var score = LK.getScore();
if (Math.floor(score / 100) !== Math.floor(lastNightDayScore / 100)) {
isNight = !isNight;
lastNightDayScore = score;
// Animate overlay alpha: 0.6 for night, 0 for day
tween.stop(nightOverlay, {
alpha: true
});
tween(nightOverlay, {
alpha: isNight ? 0.6 : 0
}, {
duration: 1200,
easing: tween.cubicInOut
});
// Animate triangle headlights alpha: 0.85 for night, 0 for day
if (car && car.headlightLeft && car.headlightRight) {
tween.stop(car.headlightLeft, {
alpha: true
});
tween.stop(car.headlightRight, {
alpha: true
});
tween(car.headlightLeft, {
alpha: isNight ? 0.85 : 0
}, {
duration: 900,
easing: tween.cubicInOut,
onUpdate: function onUpdate() {
car.headlightLeft.drawPolygon(car.headlightLeft.alpha);
}
});
tween(car.headlightRight, {
alpha: isNight ? 0.85 : 0
}, {
duration: 900,
easing: tween.cubicInOut,
onUpdate: function onUpdate() {
car.headlightRight.drawPolygon(car.headlightRight.alpha);
}
});
// Draw polygons immediately for new state
car.headlightLeft.drawPolygon(isNight ? 0.85 : 0);
car.headlightRight.drawPolygon(isNight ? 0.85 : 0);
}
}
// --- End Night/Day cycle logic ---
}
// Remove if offscreen
if (obs.y > 2732 + 120 || obs.x < 0 - 200 || obs.x > 2048 + 200) {
obs.destroy();
obstacles.splice(i, 1);
}
}
// Spawn new obstacles
ticksSinceLastObstacle += 1;
if (ticksSinceLastObstacle >= obstacleInterval) {
spawnObstacle();
ticksSinceLastObstacle = 0;
}
};
// Reset game on game over
game.on('reset', function () {
resetGame();
});
// Initial state
resetGame();
// --- Auto Button UI ---
var autoMode = false;
var autoBtn = new Text2('AUTO', {
size: 90,
fill: 0xFFFFFF,
fontWeight: "bold",
background: 0x008080
});
autoBtn.anchor.set(1, 1); // bottom right
autoBtn.x = LK.gui.width - 40;
autoBtn.y = LK.gui.height - 40;
autoBtn.alpha = 0.85;
autoBtn.interactive = true;
autoBtn.buttonMode = true;
LK.gui.bottomRight.addChild(autoBtn);
// Update button appearance
function updateAutoBtn() {
if (typeof autoBtn !== "undefined" && autoBtn && typeof autoBtn.setText === "function") {
autoBtn.setText(autoMode ? "AUTO ON" : "AUTO");
autoBtn.alpha = autoMode ? 1 : 0.85;
autoBtn.fill = autoMode ? "#00ff00" : "#ffffff";
}
}
updateAutoBtn();
// Toggle auto mode on button press
autoBtn.down = function (x, y, obj) {
autoMode = !autoMode;
updateAutoBtn();
};
// --- End Auto Button UI --- ===================================================================
--- original.js
+++ change.js
@@ -265,9 +265,11 @@
// y is set on spawn
self.update = function () {
// Move down at car speed
if (typeof car !== "undefined" && car && typeof car.speed === "number") {
- self.y += car.speed;
+ if (typeof car !== "undefined" && car && typeof car.speed === "number") {
+ self.y += car.speed;
+ }
}
// Remove if offscreen (handled in game.update)
};
return self;
2d red car. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat,upper profile
A truck,2d,upper profile. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat.upper looking
A car crash effect,red,yellow and orange colors,put the outer side black line. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Coin with 10 on it . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat