===================================================================
--- original.js
+++ change.js
@@ -1,120 +1,143 @@
-/****
+/****
* Classes
-****/
-// Asteroid class
-var Asteroid = Container.expand(function () {
- var self = Container.call(this);
- var asteroidGraphics = self.attachAsset('asteroid', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 5;
- self.update = function () {
- self.y += self.speed;
- if (self.y > 2732 + asteroidGraphics.height) {
- self.destroy();
- asteroids.splice(asteroids.indexOf(self), 1);
- }
- };
+****/
+// Enemy class
+var Enemy = Container.expand(function () {
+ var self = Container.call(this);
+ var enemyGraphics = self.attachAsset('enemy', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.update = function () {
+ self.y += self.speed;
+ if (self.y > 2732 + enemyGraphics.height) {
+ self.destroy();
+ enemies.splice(enemies.indexOf(self), 1);
+ }
+ };
});
-// Hero Bullet class
-var HeroBullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.attachAsset('heroBullet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = -15;
- self.update = function () {
- self.y += self.speed;
- if (self.y < -bulletGraphics.height) {
- self.destroy();
- heroBullets.splice(heroBullets.indexOf(self), 1);
- }
- };
-});
//<Assets used in the game will automatically appear here>
-// Space Shuttle class
-var SpaceShuttle = Container.expand(function () {
- var self = Container.call(this);
- var shuttleGraphics = self.attachAsset('shuttle', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 10;
- self.update = function () {
- // Update logic for the space shuttle
- };
- self.shoot = function () {
- var bullet = new HeroBullet();
- bullet.x = self.x;
- bullet.y = self.y - shuttleGraphics.height / 2;
- game.addChild(bullet);
- heroBullets.push(bullet);
- };
+// Hero class
+var Hero = Container.expand(function () {
+ var self = Container.call(this);
+ var heroGraphics = self.attachAsset('spaceShuttle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 10;
+ self.update = function () {
+ // Update logic for hero
+ if (keyState['ArrowLeft']) {
+ self.x -= self.speed;
+ }
+ if (keyState['ArrowRight']) {
+ self.x += self.speed;
+ }
+ };
+ self.shoot = function () {
+ var bullet = new HeroBullet();
+ bullet.x = self.x;
+ bullet.y = self.y - heroGraphics.height / 2;
+ game.addChild(bullet);
+ heroBullets.push(bullet);
+ };
});
+// HeroBullet class
+var HeroBullet = Container.expand(function () {
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('heroBullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -15;
+ self.update = function () {
+ self.y += self.speed;
+ if (self.y < -bulletGraphics.height) {
+ self.destroy();
+ heroBullets.splice(heroBullets.indexOf(self), 1);
+ }
+ };
+});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
-// Initialize arrays and variables
+****/
+var hero;
var heroBullets = [];
-var asteroids = [];
+var enemies = [];
var score = 0;
-var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+var scoreTxt;
+// Initialize hero
+hero = new Hero();
+hero.x = 2048 / 2;
+hero.y = 2732 - 200;
+game.addChild(hero);
+// Initialize score text
+scoreTxt = new Text2('0', {
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
-// Create space shuttle
-var spaceShuttle = new SpaceShuttle();
-spaceShuttle.x = 2048 / 2;
-spaceShuttle.y = 2732 - 200;
-game.addChild(spaceShuttle);
-// Handle touch events for shooting
+// Handle game updates
+game.update = function () {
+ // Update hero bullets
+ for (var i = heroBullets.length - 1; i >= 0; i--) {
+ heroBullets[i].update();
+ }
+ // Update enemies
+ for (var j = enemies.length - 1; j >= 0; j--) {
+ enemies[j].update();
+ if (enemies[j].intersects(hero)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ }
+ // Spawn enemies
+ if (LK.ticks % 60 == 0) {
+ var enemy = new Enemy();
+ enemy.x = Math.random() * 2048;
+ enemy.y = -100;
+ game.addChild(enemy);
+ enemies.push(enemy);
+ }
+ // Check for bullet-enemy collisions
+ for (var k = heroBullets.length - 1; k >= 0; k--) {
+ for (var l = enemies.length - 1; l >= 0; l--) {
+ if (heroBullets[k].intersects(enemies[l])) {
+ heroBullets[k].destroy();
+ enemies[l].destroy();
+ heroBullets.splice(k, 1);
+ enemies.splice(l, 1);
+ score++;
+ scoreTxt.setText(score);
+ break;
+ }
+ }
+ }
+};
+// Handle touch events
+var keyState = {};
+LK.on('keyDown', function (key) {
+ keyState[key] = true;
+});
+LK.on('keyUp', function (key) {
+ keyState[key] = false;
+});
game.down = function (x, y, obj) {
- spaceShuttle.shoot();
+ hero.shoot();
};
-// Update game logic
-game.update = function () {
- // Update space shuttle
- spaceShuttle.update();
- // Update hero bullets
- for (var i = heroBullets.length - 1; i >= 0; i--) {
- heroBullets[i].update();
- }
- // Update asteroids
- for (var j = asteroids.length - 1; j >= 0; j--) {
- asteroids[j].update();
- }
- // Check for collisions
- for (var k = heroBullets.length - 1; k >= 0; k--) {
- for (var l = asteroids.length - 1; l >= 0; l--) {
- if (heroBullets[k].intersects(asteroids[l])) {
- heroBullets[k].destroy();
- asteroids[l].destroy();
- heroBullets.splice(k, 1);
- asteroids.splice(l, 1);
- score++;
- scoreTxt.setText(score);
- break;
- }
- }
- }
- // Spawn new asteroids
- if (LK.ticks % 60 == 0) {
- var asteroid = new Asteroid();
- asteroid.x = Math.random() * 2048;
- asteroid.y = -asteroid.height;
- game.addChild(asteroid);
- asteroids.push(asteroid);
- }
+game.move = function (x, y, obj) {
+ hero.x = x;
+};
+game.up = function (x, y, obj) {
+ // No action needed on touch up
};
\ No newline at end of file