/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.collected = false; self.update = function () { // Rotate coin for visual effect coinGraphics.rotation += 0.1; }; return self; }); var Flag = Container.expand(function () { var self = Container.call(this); var flagGraphics = self.attachAsset('flag', { anchorX: 0.5, anchorY: 1.0 }); return self; }); var Ground = Container.expand(function (width) { var self = Container.call(this); var groundGraphics = self.attachAsset('ground', { anchorX: 0.5, anchorY: 0.5, width: width || 400 }); return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 1.0 }); return self; }); var Platform = Container.expand(function (width, color) { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 0.5, width: width || 300, tint: color || 0x795548 }); return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 1.0 }); self.velocityY = 0; self.isGrounded = false; self.canDoubleJump = false; self.moveSpeed = 3; self.jumpPower = -25; self.gravity = 0.8; self.maxFallSpeed = 15; self.jump = function () { if (self.isGrounded) { self.velocityY = self.jumpPower; self.isGrounded = false; self.canDoubleJump = true; LK.getSound('jump').play(); } else if (self.canDoubleJump) { self.velocityY = self.jumpPower; self.canDoubleJump = false; LK.getSound('jump').play(); } }; self.update = function () { // Apply gravity if (!self.isGrounded) { self.velocityY += self.gravity; if (self.velocityY > self.maxFallSpeed) { self.velocityY = self.maxFallSpeed; } } // Move horizontally self.x += self.moveSpeed; // Apply vertical movement self.y += self.velocityY; // Check ground collision self.isGrounded = false; for (var i = 0; i < platforms.length; i++) { var platform = platforms[i]; if (self.intersects(platform) && self.velocityY >= 0) { self.y = platform.y - platform.height / 2; self.velocityY = 0; self.isGrounded = true; self.canDoubleJump = false; break; } } // Check ground platforms for (var i = 0; i < groundPlatforms.length; i++) { var ground = groundPlatforms[i]; if (self.intersects(ground) && self.velocityY >= 0) { self.y = ground.y - ground.height / 2; self.velocityY = 0; self.isGrounded = true; self.canDoubleJump = false; break; } } // Death condition - fell too far if (self.y > 2732 + 200) { LK.showGameOver(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game variables var currentLevel = storage.currentLevel || 1; var totalLevels = 5; var levelComplete = false; var cameraX = 0; var levelWidth = 6000; // Game objects arrays var platforms = []; var groundPlatforms = []; var obstacles = []; var coins = []; var collectedCoins = 0; // Create player var player = game.addChild(new Player()); var flag; // UI Elements var levelTxt = new Text2('Level ' + currentLevel, { size: 80, fill: 0xFFFFFF }); levelTxt.anchor.set(0.5, 0); LK.gui.top.addChild(levelTxt); var scoreTxt = new Text2('Coins: 0', { size: 60, fill: 0xFFD700 }); scoreTxt.anchor.set(0, 0); scoreTxt.x = 150; scoreTxt.y = 100; LK.gui.topLeft.addChild(scoreTxt); // Level generation function function generateLevel(level) { // Clear existing level objects for (var i = platforms.length - 1; i >= 0; i--) { platforms[i].destroy(); } for (var i = groundPlatforms.length - 1; i >= 0; i--) { groundPlatforms[i].destroy(); } for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].destroy(); } for (var i = coins.length - 1; i >= 0; i--) { coins[i].destroy(); } if (flag) flag.destroy(); platforms = []; groundPlatforms = []; obstacles = []; coins = []; collectedCoins = 0; levelComplete = false; // Reset player position player.x = 200; player.y = 2000; cameraX = 0; // Generate ground platforms for (var x = 0; x < levelWidth; x += 350) { var ground = game.addChild(new Ground(400)); ground.x = x; ground.y = 2200; groundPlatforms.push(ground); } // Generate level-specific content if (level == 1) { // Level 1: Basic platforms and coins var platformPositions = [{ x: 800, y: 1900 }, { x: 1300, y: 1700 }, { x: 1800, y: 1500 }, { x: 2400, y: 1600 }, { x: 2900, y: 1400 }, { x: 3500, y: 1300 }]; for (var i = 0; i < platformPositions.length; i++) { var platform = game.addChild(new Platform(250)); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); // Add coin above each platform var coin = game.addChild(new Coin()); coin.x = platform.x; coin.y = platform.y - 100; coins.push(coin); } } else if (level == 2) { // Level 2: More complex layout with obstacles var platformPositions = [{ x: 700, y: 1800 }, { x: 1100, y: 1600 }, { x: 1600, y: 1400 }, { x: 2200, y: 1500 }, { x: 2700, y: 1300 }, { x: 3200, y: 1600 }, { x: 3800, y: 1200 }, { x: 4300, y: 1400 }]; for (var i = 0; i < platformPositions.length; i++) { var platform = game.addChild(new Platform(200)); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); } // Add obstacles var obstaclePositions = [{ x: 1400, y: 2200 }, { x: 2500, y: 2200 }, { x: 3600, y: 2200 }]; for (var i = 0; i < obstaclePositions.length; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = obstaclePositions[i].x; obstacle.y = obstaclePositions[i].y; obstacles.push(obstacle); } // Add coins var coinPositions = [{ x: 900, y: 1700 }, { x: 1400, y: 1300 }, { x: 2000, y: 1350 }, { x: 2900, y: 1200 }, { x: 4100, y: 1300 }]; for (var i = 0; i < coinPositions.length; i++) { var coin = game.addChild(new Coin()); coin.x = coinPositions[i].x; coin.y = coinPositions[i].y; coins.push(coin); } } else if (level == 3) { // Level 3: Challenging jumps and more obstacles var platformPositions = [{ x: 600, y: 1900 }, { x: 1000, y: 1500 }, { x: 1500, y: 1200 }, { x: 2100, y: 1400 }, { x: 2600, y: 1100 }, { x: 3200, y: 1300 }, { x: 3800, y: 1000 }, { x: 4400, y: 1200 }, { x: 5000, y: 1400 }]; for (var i = 0; i < platformPositions.length; i++) { var platform = game.addChild(new Platform(180)); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); } // More obstacles var obstaclePositions = [{ x: 1300, y: 2200 }, { x: 2300, y: 2200 }, { x: 3500, y: 2200 }, { x: 4700, y: 2200 }]; for (var i = 0; i < obstaclePositions.length; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = obstaclePositions[i].x; obstacle.y = obstaclePositions[i].y; obstacles.push(obstacle); } // Coins in tricky spots var coinPositions = [{ x: 800, y: 1800 }, { x: 1250, y: 1100 }, { x: 1800, y: 1050 }, { x: 2850, y: 1000 }, { x: 4200, y: 1100 }, { x: 5200, y: 1300 }]; for (var i = 0; i < coinPositions.length; i++) { var coin = game.addChild(new Coin()); coin.x = coinPositions[i].x; coin.y = coinPositions[i].y; coins.push(coin); } } else if (level == 4) { // Level 4: Advanced platforming var platformPositions = [{ x: 500, y: 1800 }, { x: 850, y: 1400 }, { x: 1300, y: 1100 }, { x: 1800, y: 1300 }, { x: 2300, y: 1000 }, { x: 2800, y: 1200 }, { x: 3400, y: 900 }, { x: 4000, y: 1100 }, { x: 4600, y: 800 }, { x: 5200, y: 1000 }]; for (var i = 0; i < platformPositions.length; i++) { var platform = game.addChild(new Platform(150)); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); } // Many obstacles var obstaclePositions = [{ x: 1100, y: 2200 }, { x: 2100, y: 2200 }, { x: 3100, y: 2200 }, { x: 4300, y: 2200 }, { x: 5400, y: 2200 }]; for (var i = 0; i < obstaclePositions.length; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = obstaclePositions[i].x; obstacle.y = obstaclePositions[i].y; obstacles.push(obstacle); } // Premium coin placement var coinPositions = [{ x: 700, y: 1300 }, { x: 1100, y: 1000 }, { x: 1600, y: 1200 }, { x: 2600, y: 900 }, { x: 3700, y: 800 }, { x: 4800, y: 700 }, { x: 5400, y: 900 }]; for (var i = 0; i < coinPositions.length; i++) { var coin = game.addChild(new Coin()); coin.x = coinPositions[i].x; coin.y = coinPositions[i].y; coins.push(coin); } } else if (level == 5) { // Level 5: Master level var platformPositions = [{ x: 400, y: 1700 }, { x: 700, y: 1300 }, { x: 1100, y: 1000 }, { x: 1600, y: 1200 }, { x: 2100, y: 900 }, { x: 2600, y: 1100 }, { x: 3200, y: 800 }, { x: 3800, y: 1000 }, { x: 4400, y: 700 }, { x: 5000, y: 900 }, { x: 5600, y: 1100 }]; for (var i = 0; i < platformPositions.length; i++) { var platform = game.addChild(new Platform(120)); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); } // Maximum obstacles var obstaclePositions = [{ x: 900, y: 2200 }, { x: 1800, y: 2200 }, { x: 2900, y: 2200 }, { x: 4100, y: 2200 }, { x: 5300, y: 2200 }]; for (var i = 0; i < obstaclePositions.length; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = obstaclePositions[i].x; obstacle.y = obstaclePositions[i].y; obstacles.push(obstacle); } // Final challenge coins var coinPositions = [{ x: 550, y: 1200 }, { x: 900, y: 900 }, { x: 1350, y: 1100 }, { x: 2350, y: 800 }, { x: 3500, y: 700 }, { x: 4700, y: 600 }, { x: 5800, y: 1000 }]; for (var i = 0; i < coinPositions.length; i++) { var coin = game.addChild(new Coin()); coin.x = coinPositions[i].x; coin.y = coinPositions[i].y; coins.push(coin); } } // Add flag at the end of level flag = game.addChild(new Flag()); flag.x = levelWidth - 300; flag.y = 2200; } // Initialize first level generateLevel(currentLevel); // Touch controls game.down = function (x, y, obj) { if (!levelComplete) { player.jump(); } }; // Camera follow function function updateCamera() { var targetX = player.x - 1024; // Center player horizontally if (targetX < 0) targetX = 0; if (targetX > levelWidth - 2048) targetX = levelWidth - 2048; cameraX = targetX; game.x = -cameraX; } // Game update loop game.update = function () { if (levelComplete) return; // Update camera updateCamera(); // Check obstacle collisions for (var i = 0; i < obstacles.length; i++) { if (player.intersects(obstacles[i])) { LK.showGameOver(); return; } } // Check coin collection for (var i = coins.length - 1; i >= 0; i--) { var coin = coins[i]; if (!coin.collected && player.intersects(coin)) { coin.collected = true; coin.alpha = 0; collectedCoins++; LK.setScore(LK.getScore() + 10); scoreTxt.setText('Coins: ' + collectedCoins); LK.getSound('coin').play(); } } // Check level completion if (player.intersects(flag) && !levelComplete) { levelComplete = true; LK.getSound('complete').play(); if (currentLevel >= totalLevels) { // Game completed LK.showYouWin(); } else { // Next level currentLevel++; storage.currentLevel = currentLevel; LK.setTimeout(function () { levelTxt.setText('Level ' + currentLevel); generateLevel(currentLevel); }, 1000); } } // Check if player moved backwards too much (optional fail condition) if (player.x < cameraX - 200) { LK.showGameOver(); } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.update = function () {
// Rotate coin for visual effect
coinGraphics.rotation += 0.1;
};
return self;
});
var Flag = Container.expand(function () {
var self = Container.call(this);
var flagGraphics = self.attachAsset('flag', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Ground = Container.expand(function (width) {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0.5,
anchorY: 0.5,
width: width || 400
});
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Platform = Container.expand(function (width, color) {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: width || 300,
tint: color || 0x795548
});
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.isGrounded = false;
self.canDoubleJump = false;
self.moveSpeed = 3;
self.jumpPower = -25;
self.gravity = 0.8;
self.maxFallSpeed = 15;
self.jump = function () {
if (self.isGrounded) {
self.velocityY = self.jumpPower;
self.isGrounded = false;
self.canDoubleJump = true;
LK.getSound('jump').play();
} else if (self.canDoubleJump) {
self.velocityY = self.jumpPower;
self.canDoubleJump = false;
LK.getSound('jump').play();
}
};
self.update = function () {
// Apply gravity
if (!self.isGrounded) {
self.velocityY += self.gravity;
if (self.velocityY > self.maxFallSpeed) {
self.velocityY = self.maxFallSpeed;
}
}
// Move horizontally
self.x += self.moveSpeed;
// Apply vertical movement
self.y += self.velocityY;
// Check ground collision
self.isGrounded = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.intersects(platform) && self.velocityY >= 0) {
self.y = platform.y - platform.height / 2;
self.velocityY = 0;
self.isGrounded = true;
self.canDoubleJump = false;
break;
}
}
// Check ground platforms
for (var i = 0; i < groundPlatforms.length; i++) {
var ground = groundPlatforms[i];
if (self.intersects(ground) && self.velocityY >= 0) {
self.y = ground.y - ground.height / 2;
self.velocityY = 0;
self.isGrounded = true;
self.canDoubleJump = false;
break;
}
}
// Death condition - fell too far
if (self.y > 2732 + 200) {
LK.showGameOver();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var currentLevel = storage.currentLevel || 1;
var totalLevels = 5;
var levelComplete = false;
var cameraX = 0;
var levelWidth = 6000;
// Game objects arrays
var platforms = [];
var groundPlatforms = [];
var obstacles = [];
var coins = [];
var collectedCoins = 0;
// Create player
var player = game.addChild(new Player());
var flag;
// UI Elements
var levelTxt = new Text2('Level ' + currentLevel, {
size: 80,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(levelTxt);
var scoreTxt = new Text2('Coins: 0', {
size: 60,
fill: 0xFFD700
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 150;
scoreTxt.y = 100;
LK.gui.topLeft.addChild(scoreTxt);
// Level generation function
function generateLevel(level) {
// Clear existing level objects
for (var i = platforms.length - 1; i >= 0; i--) {
platforms[i].destroy();
}
for (var i = groundPlatforms.length - 1; i >= 0; i--) {
groundPlatforms[i].destroy();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].destroy();
}
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].destroy();
}
if (flag) flag.destroy();
platforms = [];
groundPlatforms = [];
obstacles = [];
coins = [];
collectedCoins = 0;
levelComplete = false;
// Reset player position
player.x = 200;
player.y = 2000;
cameraX = 0;
// Generate ground platforms
for (var x = 0; x < levelWidth; x += 350) {
var ground = game.addChild(new Ground(400));
ground.x = x;
ground.y = 2200;
groundPlatforms.push(ground);
}
// Generate level-specific content
if (level == 1) {
// Level 1: Basic platforms and coins
var platformPositions = [{
x: 800,
y: 1900
}, {
x: 1300,
y: 1700
}, {
x: 1800,
y: 1500
}, {
x: 2400,
y: 1600
}, {
x: 2900,
y: 1400
}, {
x: 3500,
y: 1300
}];
for (var i = 0; i < platformPositions.length; i++) {
var platform = game.addChild(new Platform(250));
platform.x = platformPositions[i].x;
platform.y = platformPositions[i].y;
platforms.push(platform);
// Add coin above each platform
var coin = game.addChild(new Coin());
coin.x = platform.x;
coin.y = platform.y - 100;
coins.push(coin);
}
} else if (level == 2) {
// Level 2: More complex layout with obstacles
var platformPositions = [{
x: 700,
y: 1800
}, {
x: 1100,
y: 1600
}, {
x: 1600,
y: 1400
}, {
x: 2200,
y: 1500
}, {
x: 2700,
y: 1300
}, {
x: 3200,
y: 1600
}, {
x: 3800,
y: 1200
}, {
x: 4300,
y: 1400
}];
for (var i = 0; i < platformPositions.length; i++) {
var platform = game.addChild(new Platform(200));
platform.x = platformPositions[i].x;
platform.y = platformPositions[i].y;
platforms.push(platform);
}
// Add obstacles
var obstaclePositions = [{
x: 1400,
y: 2200
}, {
x: 2500,
y: 2200
}, {
x: 3600,
y: 2200
}];
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = obstaclePositions[i].x;
obstacle.y = obstaclePositions[i].y;
obstacles.push(obstacle);
}
// Add coins
var coinPositions = [{
x: 900,
y: 1700
}, {
x: 1400,
y: 1300
}, {
x: 2000,
y: 1350
}, {
x: 2900,
y: 1200
}, {
x: 4100,
y: 1300
}];
for (var i = 0; i < coinPositions.length; i++) {
var coin = game.addChild(new Coin());
coin.x = coinPositions[i].x;
coin.y = coinPositions[i].y;
coins.push(coin);
}
} else if (level == 3) {
// Level 3: Challenging jumps and more obstacles
var platformPositions = [{
x: 600,
y: 1900
}, {
x: 1000,
y: 1500
}, {
x: 1500,
y: 1200
}, {
x: 2100,
y: 1400
}, {
x: 2600,
y: 1100
}, {
x: 3200,
y: 1300
}, {
x: 3800,
y: 1000
}, {
x: 4400,
y: 1200
}, {
x: 5000,
y: 1400
}];
for (var i = 0; i < platformPositions.length; i++) {
var platform = game.addChild(new Platform(180));
platform.x = platformPositions[i].x;
platform.y = platformPositions[i].y;
platforms.push(platform);
}
// More obstacles
var obstaclePositions = [{
x: 1300,
y: 2200
}, {
x: 2300,
y: 2200
}, {
x: 3500,
y: 2200
}, {
x: 4700,
y: 2200
}];
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = obstaclePositions[i].x;
obstacle.y = obstaclePositions[i].y;
obstacles.push(obstacle);
}
// Coins in tricky spots
var coinPositions = [{
x: 800,
y: 1800
}, {
x: 1250,
y: 1100
}, {
x: 1800,
y: 1050
}, {
x: 2850,
y: 1000
}, {
x: 4200,
y: 1100
}, {
x: 5200,
y: 1300
}];
for (var i = 0; i < coinPositions.length; i++) {
var coin = game.addChild(new Coin());
coin.x = coinPositions[i].x;
coin.y = coinPositions[i].y;
coins.push(coin);
}
} else if (level == 4) {
// Level 4: Advanced platforming
var platformPositions = [{
x: 500,
y: 1800
}, {
x: 850,
y: 1400
}, {
x: 1300,
y: 1100
}, {
x: 1800,
y: 1300
}, {
x: 2300,
y: 1000
}, {
x: 2800,
y: 1200
}, {
x: 3400,
y: 900
}, {
x: 4000,
y: 1100
}, {
x: 4600,
y: 800
}, {
x: 5200,
y: 1000
}];
for (var i = 0; i < platformPositions.length; i++) {
var platform = game.addChild(new Platform(150));
platform.x = platformPositions[i].x;
platform.y = platformPositions[i].y;
platforms.push(platform);
}
// Many obstacles
var obstaclePositions = [{
x: 1100,
y: 2200
}, {
x: 2100,
y: 2200
}, {
x: 3100,
y: 2200
}, {
x: 4300,
y: 2200
}, {
x: 5400,
y: 2200
}];
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = obstaclePositions[i].x;
obstacle.y = obstaclePositions[i].y;
obstacles.push(obstacle);
}
// Premium coin placement
var coinPositions = [{
x: 700,
y: 1300
}, {
x: 1100,
y: 1000
}, {
x: 1600,
y: 1200
}, {
x: 2600,
y: 900
}, {
x: 3700,
y: 800
}, {
x: 4800,
y: 700
}, {
x: 5400,
y: 900
}];
for (var i = 0; i < coinPositions.length; i++) {
var coin = game.addChild(new Coin());
coin.x = coinPositions[i].x;
coin.y = coinPositions[i].y;
coins.push(coin);
}
} else if (level == 5) {
// Level 5: Master level
var platformPositions = [{
x: 400,
y: 1700
}, {
x: 700,
y: 1300
}, {
x: 1100,
y: 1000
}, {
x: 1600,
y: 1200
}, {
x: 2100,
y: 900
}, {
x: 2600,
y: 1100
}, {
x: 3200,
y: 800
}, {
x: 3800,
y: 1000
}, {
x: 4400,
y: 700
}, {
x: 5000,
y: 900
}, {
x: 5600,
y: 1100
}];
for (var i = 0; i < platformPositions.length; i++) {
var platform = game.addChild(new Platform(120));
platform.x = platformPositions[i].x;
platform.y = platformPositions[i].y;
platforms.push(platform);
}
// Maximum obstacles
var obstaclePositions = [{
x: 900,
y: 2200
}, {
x: 1800,
y: 2200
}, {
x: 2900,
y: 2200
}, {
x: 4100,
y: 2200
}, {
x: 5300,
y: 2200
}];
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = obstaclePositions[i].x;
obstacle.y = obstaclePositions[i].y;
obstacles.push(obstacle);
}
// Final challenge coins
var coinPositions = [{
x: 550,
y: 1200
}, {
x: 900,
y: 900
}, {
x: 1350,
y: 1100
}, {
x: 2350,
y: 800
}, {
x: 3500,
y: 700
}, {
x: 4700,
y: 600
}, {
x: 5800,
y: 1000
}];
for (var i = 0; i < coinPositions.length; i++) {
var coin = game.addChild(new Coin());
coin.x = coinPositions[i].x;
coin.y = coinPositions[i].y;
coins.push(coin);
}
}
// Add flag at the end of level
flag = game.addChild(new Flag());
flag.x = levelWidth - 300;
flag.y = 2200;
}
// Initialize first level
generateLevel(currentLevel);
// Touch controls
game.down = function (x, y, obj) {
if (!levelComplete) {
player.jump();
}
};
// Camera follow function
function updateCamera() {
var targetX = player.x - 1024; // Center player horizontally
if (targetX < 0) targetX = 0;
if (targetX > levelWidth - 2048) targetX = levelWidth - 2048;
cameraX = targetX;
game.x = -cameraX;
}
// Game update loop
game.update = function () {
if (levelComplete) return;
// Update camera
updateCamera();
// Check obstacle collisions
for (var i = 0; i < obstacles.length; i++) {
if (player.intersects(obstacles[i])) {
LK.showGameOver();
return;
}
}
// Check coin collection
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
if (!coin.collected && player.intersects(coin)) {
coin.collected = true;
coin.alpha = 0;
collectedCoins++;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Coins: ' + collectedCoins);
LK.getSound('coin').play();
}
}
// Check level completion
if (player.intersects(flag) && !levelComplete) {
levelComplete = true;
LK.getSound('complete').play();
if (currentLevel >= totalLevels) {
// Game completed
LK.showYouWin();
} else {
// Next level
currentLevel++;
storage.currentLevel = currentLevel;
LK.setTimeout(function () {
levelTxt.setText('Level ' + currentLevel);
generateLevel(currentLevel);
}, 1000);
}
}
// Check if player moved backwards too much (optional fail condition)
if (player.x < cameraX - 200) {
LK.showGameOver();
}
};