User prompt
add cooldown based colision detection
User prompt
Improve game perfromance
Code edit (6 edits merged)
Please save this source code
User prompt
add garbage collector for destroyed obstacles
Code edit (1 edits merged)
Please save this source code
User prompt
dot initial position should be 200 pixels higher
Code edit (1 edits merged)
Please save this source code
User prompt
dotparticle effect for offscree dot should only happen once
User prompt
if dot goes offscreen then dotparticle effect
Code edit (2 edits merged)
Please save this source code
User prompt
if dot goes ofscreen, destroy dot show particles and game over
User prompt
after the first time the player touches the screen an makes the dot bounce, prevent the dot from bouncing on the bottom of the screen, instead, destroy it when offscreen
Code edit (1 edits merged)
Please save this source code
User prompt
first obstacle should be in front of the obstacleorbit on the z axis
User prompt
make sure first obstacle moves over the orbitine
User prompt
first obstacle should be created before first orbitline
User prompt
create obstacle before orbitline
User prompt
move orbitline behind z axis of obstacle
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
obstacles should be on top of orbit on the z axis
User prompt
align obstacle to be in the center of the orbit considering anchor too
User prompt
add dotted orbit also for the first start on game start
User prompt
dot should not be destroyed when it touches smallobstacle
User prompt
Fix Bug: 'TypeError: obstacles[i].move is not a function' in or related to this line: 'obstacles[i].move();' Line Number: 266
/**** * Classes ****/ var ScorePopup = Container.expand(function (x, y) { var self = Container.call(this); self.x = x; self.y = y; var scoreText = new Text2('+1', { size: 100, fill: "#ffffff", anchorX: 0.5, anchorY: 0.5 }); scoreText.x = -scoreText.width / 2; scoreText.y = -scoreText.height / 2; self.addChild(scoreText); self.alpha = 1; var duration = 90; self.update = function () { duration--; self.y -= 2; self.alpha -= 1 / 90; if (duration <= 0) { self.destroy(); } }; game.addChild(self); }); var ParticleEffect = Container.expand(function (x, y) { var self = Container.call(this); self.x = x; self.y = y; var particles = []; for (var i = 0; i < 20; i++) { var particle = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); particle.scaleX = particle.scaleY = Math.random() * 0.5 + 0.5; particle.alpha = 0.7; particle.vx = (Math.random() - 0.5) * 10; particle.vy = (Math.random() - 0.5) * 10; particles.push(particle); } self.update = function () { for (var i = particles.length - 1; i >= 0; i--) { particles[i].x += particles[i].vx; particles[i].y += particles[i].vy; particles[i].alpha -= 0.02; if (particles[i].alpha <= 0) { particles[i].destroy(); particles.splice(i, 1); } } if (particles.length === 0) { self.destroy(); } }; game.addChild(self); }); var DotParticleEffect = Container.expand(function (x, y) { var self = Container.call(this); self.x = x; self.y = y; var particles = []; var angleIncrement = Math.PI * 2 / 20; var speed = 30; for (var i = 0; i < 20; i++) { var particle = self.attachAsset('dot', { anchorX: 0.5, anchorY: 0.5 }); particle.scaleX = particle.scaleY = 0.5; particle.alpha = 1; var angle = i * angleIncrement; particle.vx = Math.cos(angle) * speed; particle.vy = Math.sin(angle) * speed; particles.push(particle); } self.update = function () { for (var i = particles.length - 1; i >= 0; i--) { particles[i].x += particles[i].vx; particles[i].y += particles[i].vy; particles[i].alpha -= 0.0167; if (particles[i].alpha <= 0) { particles[i].destroy(); particles.splice(i, 1); } } if (particles.length === 0) { self.destroy(); } }; game.addChild(self); }); var Dot = Container.expand(function () { var self = Container.call(this); var dotGraphics = self.attachAsset('dot', { anchorX: 0.5, anchorY: 0.5 }); self.destroyed = false; self.collisionCooldown = 0; self.velocityY = 0; self.gravity = 0.7; self.bounce = -15; self.update = function () { var previousY = self.y; self.velocityY += self.gravity; self.y += self.velocityY; if (self.y > 2732 - self.height / 2 && !self.firstTouch) { self.velocityY = self.bounce; } else if (self.y > 2832 && !self.destroyed) { createDotParticleEffect(self.x, self.y); self.destroyed = true; self.destroy(); LK.setTimeout(function () { LK.showGameOver(); }, 1000); } self.movedDistance = self.y - previousY; if (self.collisionCooldown > 0) { self.collisionCooldown--; } }; self.bounceUp = function () { if (!self.destroyed) { self.velocityY = self.bounce; self.firstTouch = true; } }; }); var CircularObstacle = Container.expand(function (starX, starY, radius, angleSpeed, initialAngle) { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.angle = initialAngle || 0; self.radius = radius; self.angleSpeed = angleSpeed * 0.75; self.starX = starX; self.starY = starY; self.updatePosition = function () { self.x = self.starX + Math.cos(self.angle) * self.radius; self.y = self.starY + Math.sin(self.angle) * self.radius; self.angle += self.angleSpeed * 0.35; obstacleGraphics.rotation += self.angleSpeed * 0.35; }; self.moveDown = function (distance) { self.starY += distance * 2; }; self.updatePosition(); }); var Star = Container.expand(function () { var self = Container.call(this); var starGraphics = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); self.scaleDirection = 1; self.scaleSpeed = 0.005; self.minScale = 1; self.maxScale = 1.2; self.moveDown = function (distance) { self.y += distance * 2; if (self.y > 2732 - self.height / 2) { self.y = -self.height / 2; } }; self.updateScale = function () { if (self.scaleDirection === 1 && starGraphics.scale.x < self.maxScale) { starGraphics.scale.x += self.scaleSpeed; starGraphics.scale.y += self.scaleSpeed; } else if (self.scaleDirection === -1 && starGraphics.scale.x > self.minScale) { starGraphics.scale.x -= self.scaleSpeed; starGraphics.scale.y -= self.scaleSpeed; } if (starGraphics.scale.x >= self.maxScale || starGraphics.scale.x <= self.minScale) { self.scaleDirection *= -1; } }; }); var OrbitLine = Container.expand(function (starX, starY, radius) { var self = Container.call(this); self.starX = starX; self.starY = starY; self.radius = radius; var segments = 50; var angleIncrement = Math.PI * 2 / segments; for (var i = 0; i < segments; i++) { var angle = i * angleIncrement; var dot = self.attachAsset('smallObstacle', { x: starX + Math.cos(angle) * radius, y: starY + Math.sin(angle) * radius, anchorX: 0.5, anchorY: 0.5 }); if (i % 2 === 0) { dot.alpha = 0; } } self.moveDown = function (distance) { self.y += distance * 2; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ function createParticleEffect(x, y) { var effect = new ParticleEffect(x, y); LK.on('tick', function () { effect.update(); }); } function createDotParticleEffect(x, y) { var effect = new DotParticleEffect(x, y); LK.on('tick', function () { effect.update(); }); } var dot = game.addChild(new Dot()); dot.x = 2048 / 2; dot.y = 2732 - dot.height / 2; var obstacles = []; function updateObstacles() { var obstacleCount = 5 + LK.getScore(); while (obstacles.length < obstacleCount) { var obstacle = game.addChild(new Obstacle()); obstacle.x = obstacle.direction === 1 ? -obstacle.width / 2 : 2048 + obstacle.width / 2; obstacle.y = Math.random() * (2732 - obstacle.height) + obstacle.height / 2; obstacles.push(obstacle); } } var scoreTxt = new Text2('', { size: 200, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = 600; LK.gui.top.addChild(scoreTxt); LK.gui.top.addChild(scoreTxt); game.on('down', function (obj) { if (dot) { dot.bounceUp(); } }); var stars = []; var star = game.addChild(new Star()); star.x = 2048 / 2; star.y = 2732 / 2; stars.push(star); // Add dotted orbit for the first star var orbitLine = game.addChild(new OrbitLine(star.x, star.y, 300)); obstacles.push(orbitLine); function spawnInitialObstacles() { var starX = 2048 / 2; var starY = 2732 / 2; var radius = 300; var angleSpeed = 0.05; for (var i = 0; i < 20; i++) { var obstacle = game.addChild(new CircularObstacle(starX, starY, radius, angleSpeed)); obstacles.push(obstacle); } } spawnInitialObstacles(); LK.on('tick', function () { dot.update(); for (var i = 0; i < obstacles.length; i++) { if (obstacles[i] instanceof CircularObstacle) { obstacles[i].updatePosition(); } else { if (typeof obstacles[i].move === 'function') { obstacles[i].move(); } } if (dot.y < 1500 && dot.movedDistance < 0) { obstacles[i].moveDown(-dot.movedDistance); } if (self.collisionCooldown === 0 && dot.intersects(obstacles[i]) && !(obstacles[i] instanceof OrbitLine)) { dot.collisionCooldown = 60; // 1 second cooldown at 60FPS if (!dot.destroyed) { createDotParticleEffect(dot.x, dot.y); dot.destroyed = true; } dot.destroy(); LK.setTimeout(function () { LK.showGameOver(); }, 1000); } else if (obstacles[i].y > 2732 + 4000) { obstacles[i].destroy(); obstacles.splice(i, 1); } } for (var j = stars.length - 1; j >= 0; j--) { stars[j].updateScale(); if (dot.intersects(stars[j])) { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore().toString()); scoreTxt.alpha = 0; var originalY = scoreTxt.y; LK.on('tick', function updateScoreTextFadeIn() { scoreTxt.alpha += 0.05; scoreTxt.y = originalY - (1 - scoreTxt.alpha) * 50; if (scoreTxt.alpha >= 1) { scoreTxt.alpha = 1; LK.off('tick', updateScoreTextFadeIn); } }); var oldStarY = stars[j].y; createParticleEffect(stars[j].x, stars[j].y); var scorePopup = new ScorePopup(stars[j].x, stars[j].y); LK.on('tick', function () { scorePopup.update(); }); stars[j].destroy(); stars.splice(j, 1); var newStar = game.addChild(new Star()); newStar.x = 2048 / 2; var additionalYOffset = LK.getScore() > 1 ? (LK.getScore() - 1) * 200 : 0; newStar.y = oldStarY - 2000 - additionalYOffset; stars.push(newStar); // Add a cumulative number of CircularObstacles at random positions around the new star var cumulativeObstacles = 1 + LK.getScore(); for (var k = 0; k < cumulativeObstacles; k++) { var randomAngle = Math.random() * Math.PI * 2; // Random angle in radians var radiusOffset = k * 100; var circularObstacle = game.addChild(new CircularObstacle(newStar.x, newStar.y, 300 + radiusOffset, 0.05 * (k % 2 === 0 ? 1 : -1), randomAngle)); obstacles.push(circularObstacle); } // Add orbit line after creating all obstacles var orbitLine = game.addChild(new OrbitLine(newStar.x, newStar.y, 300 + radiusOffset)); obstacles.push(orbitLine); } else if (dot.y < 1500 && dot.movedDistance < 0) { stars[j].moveDown(-dot.movedDistance); } } });
===================================================================
--- original.js
+++ change.js
@@ -99,8 +99,9 @@
anchorX: 0.5,
anchorY: 0.5
});
self.destroyed = false;
+ self.collisionCooldown = 0;
self.velocityY = 0;
self.gravity = 0.7;
self.bounce = -15;
self.update = function () {
@@ -117,8 +118,11 @@
LK.showGameOver();
}, 1000);
}
self.movedDistance = self.y - previousY;
+ if (self.collisionCooldown > 0) {
+ self.collisionCooldown--;
+ }
};
self.bounceUp = function () {
if (!self.destroyed) {
self.velocityY = self.bounce;
@@ -212,20 +216,30 @@
* Game Code
****/
function createParticleEffect(x, y) {
var effect = new ParticleEffect(x, y);
- // The tick event listener is now handled globally for all ParticleEffect instances.
+ LK.on('tick', function () {
+ effect.update();
+ });
}
function createDotParticleEffect(x, y) {
var effect = new DotParticleEffect(x, y);
- // The tick event listener is now handled globally for all DotParticleEffect instances.
+ LK.on('tick', function () {
+ effect.update();
+ });
}
var dot = game.addChild(new Dot());
dot.x = 2048 / 2;
dot.y = 2732 - dot.height / 2;
var obstacles = [];
function updateObstacles() {
- // This function has been optimized to remove unnecessary code
+ var obstacleCount = 5 + LK.getScore();
+ while (obstacles.length < obstacleCount) {
+ var obstacle = game.addChild(new Obstacle());
+ obstacle.x = obstacle.direction === 1 ? -obstacle.width / 2 : 2048 + obstacle.width / 2;
+ obstacle.y = Math.random() * (2732 - obstacle.height) + obstacle.height / 2;
+ obstacles.push(obstacle);
+ }
}
var scoreTxt = new Text2('', {
size: 200,
fill: "#ffffff"
@@ -270,9 +284,10 @@
}
if (dot.y < 1500 && dot.movedDistance < 0) {
obstacles[i].moveDown(-dot.movedDistance);
}
- if (dot.intersects(obstacles[i]) && !(obstacles[i] instanceof OrbitLine)) {
+ if (self.collisionCooldown === 0 && dot.intersects(obstacles[i]) && !(obstacles[i] instanceof OrbitLine)) {
+ dot.collisionCooldown = 60; // 1 second cooldown at 60FPS
if (!dot.destroyed) {
createDotParticleEffect(dot.x, dot.y);
dot.destroyed = true;
}
@@ -291,9 +306,16 @@
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
scoreTxt.alpha = 0;
var originalY = scoreTxt.y;
- // The score text fade-in is now handled within the Star class update method.
+ LK.on('tick', function updateScoreTextFadeIn() {
+ scoreTxt.alpha += 0.05;
+ scoreTxt.y = originalY - (1 - scoreTxt.alpha) * 50;
+ if (scoreTxt.alpha >= 1) {
+ scoreTxt.alpha = 1;
+ LK.off('tick', updateScoreTextFadeIn);
+ }
+ });
var oldStarY = stars[j].y;
createParticleEffect(stars[j].x, stars[j].y);
var scorePopup = new ScorePopup(stars[j].x, stars[j].y);
LK.on('tick', function () {