User prompt
Please fix the bug: 'TypeError: LK.showLeaderboard is not a function' in or related to this line: 'LK.showLeaderboard();' Line Number: 187
Code edit (4 edits merged)
Please save this source code
User prompt
add leaderboard in the gameover screen
User prompt
remove leaderboard button
User prompt
place the leaderboard button to the top right
User prompt
add a button to show leaderboard
User prompt
acorn give 10 points
User prompt
increase score by 1 point each seconds
Code edit (6 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught ReferenceError: treeTrunk is not defined' in or related to this line: 'treeTrunk.x = 2048 / 2;' Line Number: 120
Code edit (1 edits merged)
Please save this source code
User prompt
the score displayed should be updated when the score change
/****
* Classes
****/
var Acorn = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('acorn', {
anchorX: 0.5,
anchorY: 0
});
self.blinking = false;
self.blink = function () {
self.blinking = !self.blinking;
self.alpha = self.blinking ? 0.5 : 1;
};
LK.setInterval(self.blink, 500);
self.update = function () {
self.y += platformSpeed;
if (self.y > 2732) {
self.y = -self.height;
self.x = Math.random() * 2048;
}
};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.lastPlatform = -1;
self.speedY = 0;
self.gravity = 0.5;
self.jumpForce = -25;
self.jump = function () {
self.speedY = self.jumpForce;
self.onPlatform = false;
};
self.update = function () {
self.y += self.speedY;
self.speedY += self.gravity;
// Prevent hero from falling below the ground
if (self.y > 2732 - 25) {
self.y = 2732 - 25;
self.speedY = 0;
}
// Make the player land on platforms
for (var i = 0; i < obstacles.length; i++) {
if (self.intersects(obstacles[i])) {
if (self.y < obstacles[i].y) {
self.y = obstacles[i].y - obstacles[i].height / 2 - self.height / 2;
self.speedY = 0;
self.onPlatform = true;
} else {
self.onPlatform = false;
}
if (self.y > obstacles[i].y) {
self.speedY = 10;
}
}
}
};
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
self.update = function () {
self.y += platformSpeed;
};
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
});
var TreeTrunk = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('treeTrunk', {
anchorX: 0.5,
anchorY: 0
});
self.update = function () {
self.y += platformSpeed;
if (self.y >= 2732) {
self.y = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Create static platforms
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.x = pos.x;
});
var obstacles = [];
var nbPlatform = 10;
var gap = 2732 / (nbPlatform - 2);
var platformSpeed = 1;
var treeTrunk1 = game.addChild(new TreeTrunk());
treeTrunk1.x = 2048 / 2;
treeTrunk1.y = 0;
var treeTrunk2 = game.addChild(new TreeTrunk());
treeTrunk2.x = 2048 / 2;
treeTrunk2.y = -treeTrunk2.height;
for (var i = 0; i < nbPlatform; i++) {
var platform = new Obstacle();
if (i < 2) {
platform.width = 2732;
platform.x = 2048 / 2;
} else {
platform.width = (Math.random() * (0.3 - 0.1) + 0.1) * 2048;
platform.x = Math.random() * (2048 - platform.width);
}
platform.y = 2732 - i * gap;
platform.height = 40;
obstacles.push(game.addChild(platform));
}
var lastPlatformSpawned = nbPlatform - 1;
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 / 2;
var acorn = game.addChild(new Acorn());
acorn.x = Math.random() * 2048;
acorn.y = -acorn.height;
// Handle touch to make the hero jump
game.on('down', function () {
if (hero.onPlatform) {
hero.jump();
}
});
LK.on('tick', function () {
hero.update();
acorn.update();
if (hero.intersects(acorn)) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
acorn.y = -acorn.height;
acorn.x = Math.random() * 2048;
}
// Game tick
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (obstacles[i].y > 2732) {
obstacles[i].y = obstacles[lastPlatformSpawned].height - gap;
obstacles[i].x = Math.random() * (2048 - obstacles[i].width);
obstacles[i].width = (Math.random() * (0.3 - 0.1) + 0.1) * 2048;
platformSpeed += 0.1;
lastPlatformSpawned = i;
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speedY = platformSpeed;
}
}
}
treeTrunk1.update();
treeTrunk2.update();
// Game over when hero touches bottom of the screen
if (hero.y >= 2732 - hero.height) {
LK.showGameOver();
}
});
a little squirrel with sun glasses and earring. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d flat leaf green horizontal platform. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
acorn. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.