/****
* Classes
****/
// Hurdle class
var Hurdle = Container.expand(function () {
var self = Container.call(this);
var hurdleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.0 // Anchor at the top for easier sky alignment
});
self.speedY = 5;
self.move = function () {
self.y += self.speedY;
};
self.isOffScreen = function () {
return self.y > game.height;
};
});
// Assets are automatically created based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0 // Anchor at the bottom center for easier ground alignment
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpPower = -15;
self.grounded = false;
self.update = function () {
// Apply gravity
if (!self.grounded) {
self.speedY += self.gravity;
self.y += self.speedY;
}
// Check for ground
if (self.y >= game.height - playerGraphics.height) {
self.y = game.height - playerGraphics.height;
self.grounded = true;
self.speedY = 0;
}
};
self.jump = function () {
if (self.grounded) {
self.speedY = self.jumpPower;
self.grounded = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate the sky
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 200;
player.y = game.height - 100; // Start position near the bottom of the screen
var obstacles = [];
var spawnObstacleTimer = 0;
var spawnObstacleInterval = 120; // Frames until next obstacle spawns
game.on('down', function (obj) {
player.jump();
});
LK.on('tick', function () {
player.update();
// Handle obstacle movement and cleanup
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (obstacles[i].isOffScreen()) {
obstacles[i].destroy();
obstacles.splice(i, 1);
} else if (player.intersects(obstacles[i])) {
// Game over logic
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn hurdles
spawnObstacleTimer++;
if (spawnObstacleTimer >= spawnObstacleInterval) {
var hurdle = game.addChild(new Hurdle());
hurdle.x = game.width / 2; // Position at the middle of the screen
hurdle.y = 0; // Position at the top of the screen
obstacles.push(hurdle);
spawnObstacleTimer = 0;
}
}); ===================================================================
--- original.js
+++ change.js
@@ -85,9 +85,9 @@
// Spawn hurdles
spawnObstacleTimer++;
if (spawnObstacleTimer >= spawnObstacleInterval) {
var hurdle = game.addChild(new Hurdle());
- hurdle.x = Math.random() * game.width; // Random position along the width of the screen
+ hurdle.x = game.width / 2; // Position at the middle of the screen
hurdle.y = 0; // Position at the top of the screen
obstacles.push(hurdle);
spawnObstacleTimer = 0;
}