Code edit (6 edits merged)
Please save this source code
User prompt
in diagonalLine, clamp the value for startingX to within 120 pixels of the screen
Code edit (1 edits merged)
Please save this source code
User prompt
add a pattern to spawn enemies in a diagonal line
Code edit (5 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught ReferenceError: spacingX is not defined' in this line: 'return self.patterns[patternName](count, spacingX, spacingY);' Line Number: 101
Code edit (4 edits merged)
Please save this source code
User prompt
update spawn to factor inthe rectangle pattern function
User prompt
add logic to rectangle to spawn enemies in a rectangular pattern
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught ReferenceError: line is not defined' in this line: 'enemies = line(cols, spacing);' Line Number: 100
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
update rectangleBlock to create a rectangle pattern
Code edit (5 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught ReferenceError: x is not defined' in this line: 'enemy.x = startX + x * spacing;' Line Number: 104
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'width')' in this line: 'var startX = (game.width - (countX * enemy.width + (countX - 1) * spacing)) / 2;' Line Number: 100
User prompt
check why rectangleBlock is spawning enemies at the left edge of the screen
Code edit (1 edits merged)
Please save this source code
User prompt
add logic to rectangleBlock to generate enemies in a rectangle patter, with the rectangle positioned in the middle of the screen
User prompt
Fix Bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'length')' in this line: 'for (var i = 0; i < spawnedEnemies.length; i++) {' Line Number: 138
Code edit (1 edits merged)
Please save this source code
User prompt
update rectangleBlock so it spawns the enemies in the middle of the screen
Code edit (3 edits merged)
Please save this source code
/**** * Classes ****/ var Starfield = Container.expand(function () { var self = Container.call(this); self.stars = []; var numberOfStars = 300; for (var i = 0; i < numberOfStars; i++) { var star = LK.getAsset('star', 'Star'); star.anchor.set(0.5, 0.5); star.x = Math.random() * game.width; star.y = Math.random() * game.height; star.speed = Math.random() * 1 + 0.1; star.alpha = Math.random() * 0.2 + 0.1; star.tint = Math.random() * 0xFFFFFF; self.addChild(star); self.stars.push(star); } self.moveStars = function () { for (var i = 0; i < self.stars.length; i++) { var star = self.stars[i]; star.y += star.speed; if (star.y > game.height) { star.y = -30; } } }; }); // Hero ship class var HeroShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.createAsset('heroShip', 'Cute smiley ship', 0.5, 0.5); self.speed = 5; self.isShooting = false; self.shootingInterval = 30; self._shootingCounter = 0; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - self.height / 2; game.addChild(bullet); return bullet; }; }); // Hero bullet class var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('heroBullet', 'Heart bullet', 0.5, 1); self.speed = -10; self.move = function () { self.y += self.speed; }; }); // Enemy ship class var EnemyShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.createAsset('enemyShip', 'Evil smiley', 0.5, 0.5); self.speed = 2; self.move = function () { self.y += self.speed; }; }); var EnemyPattern = Container.expand(function () { var self = Container.call(this); self.patterns = { line: function line(count, spacing) { var enemies = []; for (var i = 0; i < count; i++) { var enemy = new EnemyShip(); enemy.x = (game.width - enemy.width) / 2; enemy.y = -enemy.height / 2 - i * spacing; enemies.push(enemy); game.addChild(enemy); } return enemies; }, vShape: function vShape(count, spacing) { var enemies = []; var enemyShipInstance = new EnemyShip(); var middle = (game.width - enemyShipInstance.width) / 2; var middleEnemy = new EnemyShip(); game.addChild(middleEnemy); middleEnemy.x = middle; middleEnemy.y = middleEnemy.height / 2 - spacing; enemies.push(middleEnemy); for (var i = 1; i < count; i++) { var leftEnemy = new EnemyShip(); var rightEnemy = new EnemyShip(); leftEnemy.x = middle - i * spacing; rightEnemy.x = middle + i * spacing; leftEnemy.y = rightEnemy.y = -leftEnemy.height / 2 - i * spacing; enemies.push(leftEnemy, rightEnemy); game.addChild(leftEnemy); game.addChild(rightEnemy); } return enemies; }, diagonalLine: function diagonalLine(count, spacing) { var enemies = []; var startingX = Math.random() * (game.width - 240) + 120; for (var i = 0; i < count; i++) { var enemy = new EnemyShip(); enemy.x = startingX + i * spacing; enemy.y = -enemy.height / 2 - i * spacing; enemies.push(enemy); game.addChild(enemy); } return enemies; } }; self.spawn = function (patternName, count, spacing) { if (self.patterns[patternName]) { return self.patterns[patternName](count, spacing); } return []; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xFEF3BD // Init game with black background }); /**** * Game Code ****/ // Initialize starfield var starfield = game.addChild(new Starfield()); starfield.x = 0; starfield.y = 0; // Start off screen // Initialize important asset arrays var heroBullets = []; var enemyShips = []; // Create hero ship var heroShip = game.addChild(new HeroShip()); heroShip.x = game.width / 2; heroShip.y = game.height - 450; // Create enemies function spawnEnemy(patternName, count, spacing) { var pattern = new EnemyPattern(); var spawnedEnemies = pattern.spawn(patternName, count, spacing); if (spawnedEnemies) { for (var i = 0; i < spawnedEnemies.length; i++) { enemyShips.push(spawnedEnemies[i]); } } } // Spawn initial enemies in a line pattern spawnEnemy('diagonalLine', 4, 120); // Game tick event LK.on('tick', function () { // Move the starfield starfield.moveStars(); // Move hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { var bullet = heroBullets[i]; bullet.move(); if (bullet.y < -bullet.height) { bullet.destroy(); heroBullets.splice(i, 1); } else { // Check collision with enemies for (var j = enemyShips.length - 1; j >= 0; j--) { if (bullet.intersects(enemyShips[j])) { bullet.destroy(); enemyShips[j].destroy(); heroBullets.splice(i, 1); enemyShips.splice(j, 1); break; } } } } // Move enemy ships for (var i = enemyShips.length - 1; i >= 0; i--) { var enemy = enemyShips[i]; enemy.move(); if (enemy.y > game.height + enemy.height / 2) { enemy.destroy(); enemyShips.splice(i, 1); spawnEnemy(); } } // Shoot bullets if (heroShip._shootingCounter % heroShip.shootingInterval == 0) { if (heroShip.isShooting) { heroBullets.push(heroShip.shoot()); heroShip._shootingCounter = 0; } } heroShip._shootingCounter += 1; }); // Touch controls to make heroShip follow the pointer game.on('move', function (obj) { var touchPos = obj.event.getLocalPosition(game); heroShip.x = touchPos.x; }); game.on('down', function (obj) { heroShip._shootingCounter = 0; heroShip.isShooting = true; }); game.on('up', function (obj) { heroShip.isShooting = false; });
===================================================================
--- original.js
+++ change.js
@@ -94,14 +94,15 @@
game.addChild(rightEnemy);
}
return enemies;
},
- diagonalLine: function diagonalLine(count, spacingX, spacingY) {
+ diagonalLine: function diagonalLine(count, spacing) {
var enemies = [];
+ var startingX = Math.random() * (game.width - 240) + 120;
for (var i = 0; i < count; i++) {
var enemy = new EnemyShip();
- enemy.x = i * spacingX;
- enemy.y = -enemy.height / 2 - i * spacingY;
+ enemy.x = startingX + i * spacing;
+ enemy.y = -enemy.height / 2 - i * spacing;
enemies.push(enemy);
game.addChild(enemy);
}
return enemies;
@@ -150,9 +151,9 @@
}
}
// Spawn initial enemies in a line pattern
-spawnEnemy('line', 4, 120);
+spawnEnemy('diagonalLine', 4, 120);
// Game tick event
LK.on('tick', function () {
// Move the starfield
a cute cool looking emoji face. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute looking heart. bright red.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
an evil looking emoji. purple and blue colors.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a shiny blue cute star. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A start button. White on Red.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a grey touchpad. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a bright yellow shiny cute star. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.