/**** * Classes ****/ // Background class var Background = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.attachAsset('background', { anchorX: 0.0, anchorY: 0.0 }); self.speed = -1; self.update = function () { self.x += self.speed; if (self.x <= -2048) { self.x = 2048; } }; return self; }); // Boss class var Boss = Container.expand(function () { var self = Container.call(this); var bossGraphics = self.attachAsset('boss', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -50) { self.destroy(); } }; return self; }); // Foreground class var Foreground = Container.expand(function () { var self = Container.call(this); var foregroundGraphics = self.attachAsset('foreground', { anchorX: 0.0, anchorY: 0.0 }); self.speed = -2; self.update = function () { self.x += self.speed; if (self.x <= -2048) { self.x = 2048; } }; return self; }); //<Assets used in the game will automatically appear here> // Hero class var Hero = Container.expand(function () { var self = Container.call(this); self.heroIdle = self.attachAsset('hero_idle', { anchorX: 0.5, anchorY: 0.5 }); self.heroRun1 = self.attachAsset('hero_run1', { anchorX: 0.5, anchorY: 0.5 }); self.heroRun2 = self.attachAsset('hero_run2', { anchorX: 0.5, anchorY: 0.5 }); self.heroJump = self.attachAsset('hero_jump', { anchorX: 0.5, anchorY: 0.5 }); self.heroIdle.visible = true; self.heroRun1.visible = false; self.heroRun2.visible = false; self.heroJump.visible = false; self.speed = 3; self.jumpSpeed = -30; self.gravity = 1; self.isJumping = false; self.isFalling = false; self.doubleJump = false; self.isMovingLeft = false; self.isMovingRight = false; self.update = function () { if (self.isJumping) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.jumpSpeed >= 0) { self.isJumping = false; self.isFalling = true; } self.heroIdle.visible = false; self.heroRun1.visible = false; self.heroRun2.visible = false; self.heroJump.visible = true; } else if (self.isFalling) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.y >= 2732 - self.height / 2) { self.y = 2732 - self.height / 2; self.isFalling = false; self.jumpSpeed = -30; self.doubleJump = false; } self.heroIdle.visible = false; self.heroRun1.visible = false; self.heroRun2.visible = false; self.heroJump.visible = true; } else { self.heroJump.visible = false; self.heroIdle.visible = false; if (self.isMovingRight) { if (LK.ticks % 30 == 0) { if (self.heroRun1.visible) { self.heroRun1.visible = false; self.heroRun2.visible = true; } else { self.heroRun1.visible = true; self.heroRun2.visible = false; } } } else { self.heroRun1.visible = false; self.heroRun2.visible = false; self.heroIdle.visible = true; } } // Add left and right movement if (self.isMovingLeft) { self.x -= self.speed; if (self.x <= 0) { self.x = 0; } } if (self.isMovingRight) { self.x += self.speed; if (self.x >= 2048) { self.x = 2048; } } }; self.jump = function () { if (!self.isJumping && !self.isFalling) { self.isJumping = true; } else if (!self.doubleJump) { self.isJumping = true; self.doubleJump = true; self.jumpSpeed = -20; } }; return self; }); // Hurdle class var Hurdle = Container.expand(function () { var self = Container.call(this); var hurdleGraphics = self.attachAsset('hurdle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -50) { self.destroy(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game(); /**** * Game Code ****/ function spawnHurdle() { if (hurdles.length % 10 == 0 && !bossSpawned) { var boss = new Boss(); boss.x = 2048; boss.y = 2732 - boss.height / 2; // Ground level game.addChild(boss); bossSpawned = true; } else { var hurdle = new Hurdle(); hurdle.x = 2048; hurdle.y = 2732 - hurdle.height / 2; // Ground level hurdles.push(hurdle); game.addChild(hurdle); } } var background1 = game.addChild(new Background()); background1.x = 0; background1.y = 0; var background2 = game.addChild(new Background()); background2.x = 2048; background2.y = 0; var foreground1 = game.addChild(new Foreground()); foreground1.x = 0; foreground1.y = 0; var foreground2 = game.addChild(new Foreground()); foreground2.x = 2048; foreground2.y = 0; var hero = game.addChild(new Hero()); hero.x = 200; hero.y = 2732 - hero.height / 2; // Ground level var hurdles = []; var bossSpawned = false; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.down = function (x, y, obj) { if (obj) { hero.jump(); } }; game.up = function (x, y, obj) { if (obj) { hero.isMovingRight = false; } }; game.update = function () { hero.update(); for (var i = hurdles.length - 1; i >= 0; i--) { hurdles[i].update(); if (hero.intersects(hurdles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } if (LK.ticks % 120 == 0) { spawnHurdle(); } score += 1; scoreTxt.setText(score); }; background1.update(); background2.update(); foreground1.update(); foreground2.update(); game.move = function (x, y, obj) { var game_position = game.toLocal(obj.global); hero.x = game_position.x; };
/****
* Classes
****/
// Background class
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0
});
self.speed = -1;
self.update = function () {
self.x += self.speed;
if (self.x <= -2048) {
self.x = 2048;
}
};
return self;
});
// Boss class
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -50) {
self.destroy();
}
};
return self;
});
// Foreground class
var Foreground = Container.expand(function () {
var self = Container.call(this);
var foregroundGraphics = self.attachAsset('foreground', {
anchorX: 0.0,
anchorY: 0.0
});
self.speed = -2;
self.update = function () {
self.x += self.speed;
if (self.x <= -2048) {
self.x = 2048;
}
};
return self;
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
self.heroIdle = self.attachAsset('hero_idle', {
anchorX: 0.5,
anchorY: 0.5
});
self.heroRun1 = self.attachAsset('hero_run1', {
anchorX: 0.5,
anchorY: 0.5
});
self.heroRun2 = self.attachAsset('hero_run2', {
anchorX: 0.5,
anchorY: 0.5
});
self.heroJump = self.attachAsset('hero_jump', {
anchorX: 0.5,
anchorY: 0.5
});
self.heroIdle.visible = true;
self.heroRun1.visible = false;
self.heroRun2.visible = false;
self.heroJump.visible = false;
self.speed = 3;
self.jumpSpeed = -30;
self.gravity = 1;
self.isJumping = false;
self.isFalling = false;
self.doubleJump = false;
self.isMovingLeft = false;
self.isMovingRight = false;
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
if (self.jumpSpeed >= 0) {
self.isJumping = false;
self.isFalling = true;
}
self.heroIdle.visible = false;
self.heroRun1.visible = false;
self.heroRun2.visible = false;
self.heroJump.visible = true;
} else if (self.isFalling) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
if (self.y >= 2732 - self.height / 2) {
self.y = 2732 - self.height / 2;
self.isFalling = false;
self.jumpSpeed = -30;
self.doubleJump = false;
}
self.heroIdle.visible = false;
self.heroRun1.visible = false;
self.heroRun2.visible = false;
self.heroJump.visible = true;
} else {
self.heroJump.visible = false;
self.heroIdle.visible = false;
if (self.isMovingRight) {
if (LK.ticks % 30 == 0) {
if (self.heroRun1.visible) {
self.heroRun1.visible = false;
self.heroRun2.visible = true;
} else {
self.heroRun1.visible = true;
self.heroRun2.visible = false;
}
}
} else {
self.heroRun1.visible = false;
self.heroRun2.visible = false;
self.heroIdle.visible = true;
}
}
// Add left and right movement
if (self.isMovingLeft) {
self.x -= self.speed;
if (self.x <= 0) {
self.x = 0;
}
}
if (self.isMovingRight) {
self.x += self.speed;
if (self.x >= 2048) {
self.x = 2048;
}
}
};
self.jump = function () {
if (!self.isJumping && !self.isFalling) {
self.isJumping = true;
} else if (!self.doubleJump) {
self.isJumping = true;
self.doubleJump = true;
self.jumpSpeed = -20;
}
};
return self;
});
// Hurdle class
var Hurdle = Container.expand(function () {
var self = Container.call(this);
var hurdleGraphics = self.attachAsset('hurdle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -50) {
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game();
/****
* Game Code
****/
function spawnHurdle() {
if (hurdles.length % 10 == 0 && !bossSpawned) {
var boss = new Boss();
boss.x = 2048;
boss.y = 2732 - boss.height / 2; // Ground level
game.addChild(boss);
bossSpawned = true;
} else {
var hurdle = new Hurdle();
hurdle.x = 2048;
hurdle.y = 2732 - hurdle.height / 2; // Ground level
hurdles.push(hurdle);
game.addChild(hurdle);
}
}
var background1 = game.addChild(new Background());
background1.x = 0;
background1.y = 0;
var background2 = game.addChild(new Background());
background2.x = 2048;
background2.y = 0;
var foreground1 = game.addChild(new Foreground());
foreground1.x = 0;
foreground1.y = 0;
var foreground2 = game.addChild(new Foreground());
foreground2.x = 2048;
foreground2.y = 0;
var hero = game.addChild(new Hero());
hero.x = 200;
hero.y = 2732 - hero.height / 2; // Ground level
var hurdles = [];
var bossSpawned = false;
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
if (obj) {
hero.jump();
}
};
game.up = function (x, y, obj) {
if (obj) {
hero.isMovingRight = false;
}
};
game.update = function () {
hero.update();
for (var i = hurdles.length - 1; i >= 0; i--) {
hurdles[i].update();
if (hero.intersects(hurdles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (LK.ticks % 120 == 0) {
spawnHurdle();
}
score += 1;
scoreTxt.setText(score);
};
background1.update();
background2.update();
foreground1.update();
foreground2.update();
game.move = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
hero.x = game_position.x;
};