/****
* Classes
****/
// Define the 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 = -15;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
});
// Define the Monster class
var Monster = Container.expand(function () {
var self = Container.call(this);
var monsterGraphics = self.attachAsset('monster', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Monster update logic
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 2400; // Near the bottom
var monster = game.addChild(new Monster());
monster.x = 1024; // Center horizontally
monster.y = 200; // Near the top
var bullets = [];
var score = 0;
// Function to handle shooting
function shootBullet() {
var newBullet = new Bullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
// Function to handle hero movement
function moveHero(x, y) {
hero.x = x;
hero.y = y;
}
// Game update loop
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Check for collisions
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].intersects(monster)) {
score += 10;
bullets[j].destroy();
bullets.splice(j, 1);
// Flash monster to indicate hit
LK.effects.flashObject(monster, 0xff0000, 500);
}
}
// Update monster
monster.update();
if (hero.intersects(monster)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
// Handle touch events for hero movement
game.down = function (x, y, obj) {
moveHero(x, y);
};
game.move = function (x, y, obj) {
moveHero(x, y);
};
game.up = function (x, y, obj) {
// Optionally handle touch release
};
// Set interval for shooting bullets
var shootInterval = LK.setInterval(shootBullet, 500); ===================================================================
--- original.js
+++ change.js
@@ -1,56 +1,60 @@
-/****
+/****
* Classes
-****/
+****/
// Define the 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 = -15;
- 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 = -15;
+ self.update = function () {
+ self.y += self.speed;
+ };
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Hero class
var Hero = Container.expand(function () {
- var self = Container.call(this);
- var heroGraphics = self.attachAsset('hero', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 10;
- self.update = function () {
- // Hero update logic
- };
+ var self = Container.call(this);
+ var heroGraphics = self.attachAsset('hero', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 10;
+ self.update = function () {
+ // Hero update logic
+ };
});
// Define the Monster class
var Monster = Container.expand(function () {
- var self = Container.call(this);
- var monsterGraphics = self.attachAsset('monster', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 5;
- self.update = function () {
- // Monster update logic
- };
+ var self = Container.call(this);
+ var monsterGraphics = self.attachAsset('monster', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.update = function () {
+ // Monster update logic
+ self.y += self.speed;
+ if (self.y > 2732) {
+ self.y = 0;
+ }
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize game variables
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 2400; // Near the bottom
@@ -60,54 +64,54 @@
var bullets = [];
var score = 0;
// Function to handle shooting
function shootBullet() {
- var newBullet = new Bullet();
- newBullet.x = hero.x;
- newBullet.y = hero.y;
- bullets.push(newBullet);
- game.addChild(newBullet);
+ var newBullet = new Bullet();
+ newBullet.x = hero.x;
+ newBullet.y = hero.y;
+ bullets.push(newBullet);
+ game.addChild(newBullet);
}
// Function to handle hero movement
function moveHero(x, y) {
- hero.x = x;
- hero.y = y;
+ hero.x = x;
+ hero.y = y;
}
// Game update loop
game.update = function () {
- // Update bullets
- for (var i = bullets.length - 1; i >= 0; i--) {
- bullets[i].update();
- if (bullets[i].y < -50) {
- bullets[i].destroy();
- bullets.splice(i, 1);
- }
- }
- // Check for collisions
- for (var j = bullets.length - 1; j >= 0; j--) {
- if (bullets[j].intersects(monster)) {
- score += 10;
- bullets[j].destroy();
- bullets.splice(j, 1);
- // Flash monster to indicate hit
- LK.effects.flashObject(monster, 0xff0000, 500);
- }
- }
- // Update monster
- monster.update();
- if (hero.intersects(monster)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
+ // Update bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ bullets[i].update();
+ if (bullets[i].y < -50) {
+ bullets[i].destroy();
+ bullets.splice(i, 1);
+ }
+ }
+ // Check for collisions
+ for (var j = bullets.length - 1; j >= 0; j--) {
+ if (bullets[j].intersects(monster)) {
+ score += 10;
+ bullets[j].destroy();
+ bullets.splice(j, 1);
+ // Flash monster to indicate hit
+ LK.effects.flashObject(monster, 0xff0000, 500);
+ }
+ }
+ // Update monster
+ monster.update();
+ if (hero.intersects(monster)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
};
// Handle touch events for hero movement
game.down = function (x, y, obj) {
- moveHero(x, y);
+ moveHero(x, y);
};
game.move = function (x, y, obj) {
- moveHero(x, y);
+ moveHero(x, y);
};
game.up = function (x, y, obj) {
- // Optionally handle touch release
+ // Optionally handle touch release
};
// Set interval for shooting bullets
var shootInterval = LK.setInterval(shootBullet, 500);
\ No newline at end of file