/****
* 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;
}
};
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 (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 = [];
for (var i = 0; i < 5; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = 2048 / 2 + i * 450;
obstacle.y = Math.random() * (2732 - obstacle.height) + 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(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
@@ -1,84 +1,89 @@
-/****
+/****
* 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;
- }
- };
- self.bounceUp = function () {
- self.velocityY = self.bounce;
- };
+ 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;
+ }
+ };
+ 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 () {
- 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;
- }
- };
+ 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 (offsetY) {
+ self.y += offsetY;
+ }
+ };
});
-/****
+/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000
+ backgroundColor: 0x000000
});
-/****
+/****
* Game Code
****/
var dot = game.addChild(new Dot());
dot.x = 2048 / 2;
dot.y = 2732 / 2;
var obstacles = [];
for (var i = 0; i < 5; i++) {
- var obstacle = game.addChild(new Obstacle());
- obstacle.x = 2048 / 2 + i * 450;
- obstacle.y = Math.random() * (2732 - obstacle.height) + obstacle.height / 2;
- obstacles.push(obstacle);
+ var obstacle = game.addChild(new Obstacle());
+ obstacle.x = 2048 / 2 + i * 450;
+ obstacle.y = Math.random() * (2732 - obstacle.height) + obstacle.height / 2;
+ obstacles.push(obstacle);
}
var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ 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();
+ dot.bounceUp();
});
LK.on('tick', function () {
- dot.update();
- for (var i = 0; i < obstacles.length; i++) {
- obstacles[i].move();
- 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());
+ var previousY = dot.y;
+ dot.update();
+ var offsetY = previousY - dot.y;
+ for (var i = 0; i < obstacles.length; i++) {
+ obstacles[i].move(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());
});
\ No newline at end of file