/**** * Classes ****/ // Define a simple Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.y += self.speed; }; }); // Define a simple Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Enemy update logic }; }); //<Assets used in the game will automatically appear here> // Define a simple Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Player update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Create main menu text var mainMenuText = new Text2('Main Menu', { size: 150, fill: "#ffffff" }); mainMenuText.anchor.set(0.5, 0); LK.gui.center.addChild(mainMenuText); // Create start game button var startButton = new Text2('Start Game', { size: 100, fill: "#ffffff" }); startButton.anchor.set(0.5, 0); startButton.y = 200; LK.gui.center.addChild(startButton); // Handle game down events game.down = function (x, y, obj) { if (startButton.intersects(obj)) { // Start game logic } };
===================================================================
--- original.js
+++ change.js
@@ -1,115 +1,70 @@
-/****
+/****
* Classes
-****/
+****/
// Define a simple Bullet class
var Bullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.attachAsset('bullet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = -5;
- self.update = function () {
- self.y += self.speed;
- };
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -5;
+ self.update = function () {
+ self.y += self.speed;
+ };
});
// Define a simple Enemy class
var Enemy = Container.expand(function () {
- var self = Container.call(this);
- var enemyGraphics = self.attachAsset('enemy', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.update = function () {
- // Enemy update logic
- };
+ var self = Container.call(this);
+ var enemyGraphics = self.attachAsset('enemy', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Enemy update logic
+ };
});
//<Assets used in the game will automatically appear here>
// Define a simple Player class
var Player = Container.expand(function () {
- var self = Container.call(this);
- var playerGraphics = self.attachAsset('player', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.update = function () {
- // Player update logic
- };
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Player update logic
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
-// Initialize player
-var player = game.addChild(new Player());
-player.x = 2048 / 2;
-player.y = 2732 - 200;
-// Initialize enemies array
-var enemies = [];
-// Initialize bullets array
-var bullets = [];
-// Create score text
-var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+****/
+// Create main menu text
+var mainMenuText = new Text2('Main Menu', {
+ size: 150,
+ fill: "#ffffff"
});
-scoreTxt.anchor.set(0.5, 0);
-LK.gui.top.addChild(scoreTxt);
-// Handle game move events
-function handleMove(x, y, obj) {
- player.x = x;
- player.y = y;
-}
-game.move = handleMove;
+mainMenuText.anchor.set(0.5, 0);
+LK.gui.center.addChild(mainMenuText);
+// Create start game button
+var startButton = new Text2('Start Game', {
+ size: 100,
+ fill: "#ffffff"
+});
+startButton.anchor.set(0.5, 0);
+startButton.y = 200;
+LK.gui.center.addChild(startButton);
// Handle game down events
game.down = function (x, y, obj) {
- handleMove(x, y, obj);
-};
-// Handle game up events
-game.up = function (x, y, obj) {
- // No specific action on up event
-};
-// Game update logic
-game.update = function () {
- // Update player
- player.update();
- // Update enemies
- for (var i = enemies.length - 1; i >= 0; i--) {
- enemies[i].update();
- if (enemies[i].intersects(player)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
- // Update bullets
- for (var j = bullets.length - 1; j >= 0; j--) {
- bullets[j].update();
- if (bullets[j].y < -50) {
- bullets[j].destroy();
- bullets.splice(j, 1);
- }
- }
- // Fire bullet
- if (LK.ticks % 30 == 0) {
- var newBullet = new Bullet();
- newBullet.x = player.x;
- newBullet.y = player.y;
- bullets.push(newBullet);
- game.addChild(newBullet);
- }
-};
-// Create enemies at intervals
-var enemyInterval = LK.setInterval(function () {
- var newEnemy = new Enemy();
- newEnemy.x = Math.random() * 2048;
- newEnemy.y = -50;
- enemies.push(newEnemy);
- game.addChild(newEnemy);
-}, 2000);
\ No newline at end of file
+ if (startButton.intersects(obj)) {
+ // Start game logic
+ }
+};
\ No newline at end of file