User prompt
Obstacles should spawn and be repositioned always over y = 1400
Code edit (2 edits merged)
Please save this source code
User prompt
If ball interects with top of a obstacle is should roll on top of it
User prompt
Obstacles shouls spawn one at a time
User prompt
First obstacle should spawn in the bottom of the screen and next ones should always have a stair patter even when repositioned
Code edit (1 edits merged)
Please save this source code
User prompt
First obstacle should spawn in y = 2800
User prompt
Obstacles should spawn forming a stair
User prompt
When dot collides with obstacle just stop dot in that position
User prompt
Obstacles reposition shoild always be 1000 y pixels from the previous and next obstacle
User prompt
Obstacles should have 1000 pixela y of separation amongst each other
User prompt
Dot should spawn in y = 1800
User prompt
Firs obstacle y shoild be 1400
User prompt
Remove random y from obstacle initial position
User prompt
Obstacles y will always be less than 1400
User prompt
Obstacle y will be 1000 time more than the rpevious obstacle
User prompt
When a obstacle is repositioned it should be 1000 pixela y from the previous one
Code edit (3 edits merged)
Please save this source code
User prompt
Obstacles always have to be 1000 y pixels away from each other
User prompt
Obstacles should be 1000 pixels in y appart from each other
Code edit (1 edits merged)
Please save this source code
User prompt
Obstacles should only start moving downwards when dot's y is less than 1000
User prompt
When dot passes the center of the screen, obstacles should move downwards the same distance dot is moving upwarda
Initial prompt
Dot Bounce
/**** * Classes ****/ var Dot = Container.expand(function () { var self = Container.call(this); var dotGraphics = self.attachAsset('dot', { anchorX: 0.5, anchorY: 0.5 }); self.velocityY = 0; self.gravity = 0.5; self.bounce = -15; self.update = function () { self.velocityY += self.gravity; self.y += self.velocityY; if (self.y > 2732 - self.height / 2) { self.velocityY = self.bounce; } for (var i = 0; i < obstacles.length; i++) { if (self.intersectsTop(obstacles[i])) { self.velocityY = 0; self.y = obstacles[i].y - self.height / 2; } } }; self.intersectsTop = function (obstacle) { var dotBottom = self.y + self.height / 2; var obstacleTop = obstacle.y - obstacle.height / 2; var withinXBounds = self.x > obstacle.x - obstacle.width / 2 && self.x < obstacle.x + obstacle.width / 2; return dotBottom >= obstacleTop && dotBottom < obstacleTop + self.gravity && withinXBounds; }; self.bounceUp = function () { self.velocityY = self.bounce; }; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.move = function (offsetY) { self.x += self.speed; if (self.x < -self.width / 2) { self.x = 2048 + self.width / 2; self.y = Math.random() * (2732 - self.height) + self.height / 2; if (obstacles.indexOf(self) === obstacles.length - 1) { var newObstacle = game.addChild(new Obstacle()); newObstacle.x = 2048 + self.width / 2; newObstacle.y = Math.random() * (2732 - newObstacle.height) + newObstacle.height / 2; obstacles.push(newObstacle); } } if (offsetY) { self.y += offsetY; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var dot = game.addChild(new Dot()); dot.x = 2048 / 2; dot.y = 2732 / 2; var obstacles = []; var obstacle = game.addChild(new Obstacle()); obstacle.x = 2048 / 2; obstacle.y = 2732 - obstacle.height / 2; obstacles.push(obstacle); var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = 2048 / 2; scoreTxt.y = 50; LK.gui.top.addChild(scoreTxt); game.on('down', function (obj) { dot.bounceUp(); }); LK.on('tick', function () { var previousY = dot.y; dot.update(); var offsetY = previousY - dot.y; for (var i = 0; i < obstacles.length; i++) { obstacles[i].move(dot.y < 1400 && offsetY > 0 ? offsetY : 0); if (dot.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update score based on the dot's survival time var currentScore = Math.floor(LK.ticks / 60); LK.setScore(currentScore); scoreTxt.setText(LK.getScore().toString()); });
===================================================================
--- original.js
+++ change.js
@@ -15,9 +15,21 @@
self.y += self.velocityY;
if (self.y > 2732 - self.height / 2) {
self.velocityY = self.bounce;
}
+ for (var i = 0; i < obstacles.length; i++) {
+ if (self.intersectsTop(obstacles[i])) {
+ self.velocityY = 0;
+ self.y = obstacles[i].y - self.height / 2;
+ }
+ }
};
+ self.intersectsTop = function (obstacle) {
+ var dotBottom = self.y + self.height / 2;
+ var obstacleTop = obstacle.y - obstacle.height / 2;
+ var withinXBounds = self.x > obstacle.x - obstacle.width / 2 && self.x < obstacle.x + obstacle.width / 2;
+ return dotBottom >= obstacleTop && dotBottom < obstacleTop + self.gravity && withinXBounds;
+ };
self.bounceUp = function () {
self.velocityY = self.bounce;
};
});