Code edit (1 edits merged)
Please save this source code
User prompt
I want to add a text note above the Number of bullets that says "Bullets that hit a monster are returned, All others are lost"
User prompt
bullet count should not increase when bullet is destroyed for not being visible anymore
Code edit (1 edits merged)
Please save this source code
User prompt
only allow the bullet count to increase back up to one when it is destroyed by hitting a monster
User prompt
Fix Bug: 'Uncaught TypeError: LK.Button is not a constructor' in or related to this line: 'var fireButton = new LK.Button({' Line Number: 365
User prompt
Fix Bug: 'Uncaught TypeError: LK.getButton is not a function' in or related to this line: 'var fireButton = LK.getButton({' Line Number: 365
User prompt
Fix Bug: 'Uncaught TypeError: LK.Button is not a constructor' in or related to this line: 'var fireButton = new LK.Button({' Line Number: 365
User prompt
Fix Bug: 'Uncaught TypeError: LK.createButton is not a function' in or related to this line: 'var fireButton = LK.createButton({' Line Number: 365
User prompt
Fix Bug: 'Uncaught TypeError: LK.Button is not a constructor' in or related to this line: 'var fireButton = new LK.Button({' Line Number: 365
User prompt
lets allocate the bottom corner of the visible screen to include some player buttons for firing bullets and resetting bullet count to 10
User prompt
i want to remove the code that adds bulletcount when one is destroyed
Code edit (1 edits merged)
Please save this source code
User prompt
lets add the ability to reset the bulletcount back to 10 at any time during the game when the player hits the "r" key on the keyboard
User prompt
change the bullet count reset value from "1" to "R"
User prompt
Fix Bug: 'TypeError: resetButton.intersectsPoint is not a function' in or related to this line: 'if (resetButton.intersectsPoint(pos.x, pos.y)) {' Line Number: 383
User prompt
allow for the user to reset the number of bullets at any time during the game
User prompt
Fix Bug: 'Uncaught TypeError: window.addEventListener is not a function' in or related to this line: 'window.addEventListener('keydown', function (event) {' Line Number: 139
User prompt
reset the number of bullets to 10 when the player hits the 1 key on the keyboard
User prompt
i want to show the number of bullets available as white text on the bottom right of the visible area
User prompt
i would like to limit the amount of bullets available in a game, the starting amount should be 10
/**** * Classes ****/ var Instructions = Container.expand(function () { var self = Container.call(this); var instructionsText = new Text2('Instructions: Use your Left Click on mouse to move the hero.\n Right Click to shoot bullets.\n Kill all the monsters to advance to the next level. \n Good Luck!', { size: 30, fill: '#ffffff', align: 'center' }); self.addChild(instructionsText); instructionsText.x = 0 - instructionsText.width / 2; instructionsText.y = 2 - instructionsText.height / 2; }); // No assets are defined as this is a blank game var Hero = Container.expand(function () { var self = Container.call(this); self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.targetPosition = { x: self.x, y: self.y }; self.speed = 5; self.direction = { x: 0, y: 0 }; self.update = function () { // Calculate the angle in radians and rotate the hero var angle = Math.atan2(self.direction.y, self.direction.x); self.rotation = angle; }; self.checkCollision = function (trees) { return false; }; }); var Tree = Container.expand(function () { var self = Container.call(this); self.attachAsset('tree', { anchorX: 0.5, anchorY: 0.5 }); }); var Monster = Container.expand(function () { var self = Container.call(this); self.attachAsset('monster', { anchorX: 0.5, anchorY: 0.5 }); self.isAlive = true; self.speed = 1; self.chaseHero = function (hero) { if (!self.isAlive || !hero) { return; } var dx = hero.x - self.x; var dy = hero.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 0) { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; } if (self.intersects(hero)) { if (LK.getScore() < 10) { LK.showGameOver({ text: 'You Suck', color: '#FFFFFF' }); } else { LK.showGameOver(); } } }; self.destroyedByBullet = function () { self.isAlive = false; LK.setScore(LK.getScore() + 1); scoreTxt.setText('# of Monsters Killed: ' + LK.getScore().toString()); self.destroy(); }; }); var Bullet = Container.expand(function () { var self = Container.call(this); self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.direction = { x: 0, y: 0 }; self.move = function (trees) { if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { self.destroy(); bulletCount++; bulletCountTxt.setText('Bullets: ' + bulletCount); return true; } self.x += self.direction.x * self.speed; self.y += self.direction.y * self.speed; for (var i = 0; i < trees.length; i++) { if (self.intersects(trees[i])) { self.destroy(); return true; } } for (var j = 0; j < monsters.length; j++) { if (self.intersects(monsters[j]) && monsters[j].isAlive) { monsters[j].destroyedByBullet(); self.destroy(); return true; } } return false; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Initialize game with black background }); /**** * Game Code ****/ LK.on('keydown', function (event) { if (event.key === '1') { bulletCount = 10; bulletCountTxt.setText('Bullets: ' + bulletCount); } }); var instructions = game.addChild(new Instructions()); instructions.x = 2048 / 2; instructions.y = 2732 / 3; var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 / 2; // Create a text box to display hero's speed and direction // The heroInfo variable was not defined, so we remove the lines that reference it. var dragNode = null; var trees = []; function handleMove(obj) { var event = obj.event; var pos = event.getLocalPosition(game); if (dragNode) { var oldX = dragNode.x; var oldY = dragNode.y; if (dragNode === hero) { var targetX = pos.x; var targetY = pos.y; var distance = Math.sqrt(Math.pow(targetX - hero.x, 2) + Math.pow(targetY - hero.y, 2)); hero.direction.x = (targetX - hero.x) / distance; hero.direction.y = (targetY - hero.y) / distance; var nextX = hero.x + hero.direction.x * hero.speed; var nextY = hero.y + hero.direction.y * hero.speed; if (!hero.checkCollision(trees)) { hero.x = nextX; hero.y = nextY; } else { hero.direction.x = 0; hero.direction.y = 0; } } } } game.on('move', handleMove); game.on('down', function (obj) { if (instructions) { instructions.destroy(); instructions = null; } var event = obj.event; var pos = event.getLocalPosition(game); hero.targetPosition = { x: pos.x, y: pos.y }; hero.direction.x = hero.targetPosition.x - hero.x; hero.direction.y = hero.targetPosition.y - hero.y; var distance = Math.sqrt(hero.direction.x * hero.direction.x + hero.direction.y * hero.direction.y); hero.direction.x /= distance; hero.direction.y /= distance; }); // Add touch controls for mobile devices LK.on('touchstart', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); hero.targetPosition = { x: pos.x, y: pos.y }; hero.direction.x = hero.targetPosition.x - hero.x; hero.direction.y = hero.targetPosition.y - hero.y; var distance = Math.sqrt(hero.direction.x * hero.direction.x + hero.direction.y * hero.direction.y); hero.direction.x /= distance; hero.direction.y /= distance; }); // Update the text box every game tick LK.on('tick', function () { if (hero.targetPosition) { var distanceToTarget = hero.targetPosition ? Math.sqrt(Math.pow(hero.targetPosition.x - hero.x, 2) + Math.pow(hero.targetPosition.y - hero.y, 2)) : 0; } if (distanceToTarget > hero.speed) { var nextX = hero.x + hero.direction.x * hero.speed; var nextY = hero.y + hero.direction.y * hero.speed; if (!hero.checkCollision(trees)) { hero.x = nextX; hero.y = nextY; } } else { hero.x = hero.targetPosition.x; hero.y = hero.targetPosition.y; hero.direction.x = 0; hero.direction.y = 0; } levelResetTxt.setText('Level Number: ' + levelResetCount.toString()); }); game.on('up', function (obj) { dragNode = null; }); var bullets = []; game.on('rightdown', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); if (bulletCount > 0) { var bullet = game.addChild(new Bullet()); bullet.x = hero.x; bullet.y = hero.y; var targetX = pos.x; var targetY = pos.y; var distance = Math.sqrt(Math.pow(targetX - hero.x, 2) + Math.pow(targetY - hero.y, 2)); bullet.direction = { x: (targetX - hero.x) / distance, y: (targetY - hero.y) / distance }; bullet.speed = 10; bullets.push(bullet); bulletCount--; bulletCountTxt.setText('Bullets: ' + bulletCount); } }); LK.on('tick', function () { for (var i = bullets.length - 1; i >= 0; i--) { if (bullets[i].move(trees)) { bullets.splice(i, 1); } } var allMonstersKilled = monsters.every(function (monster) { return !monster.isAlive; }); if (allMonstersKilled) { resetLevel(); } else { for (var i = 0; i < monsters.length; i++) { monsters[i].chaseHero(hero); } } }); // Add fewer trees for (var i = 0; i < 5; i++) { var tree = game.addChild(new Tree()); tree.x = Math.random() * 2048; tree.y = Math.random() * 2732; trees.push(tree); } var monsters = []; for (var i = 0; i < 5; i++) { var monster = game.addChild(new Monster()); var edge = Math.floor(Math.random() * 4); switch (edge) { case 0: monster.x = Math.random() * 2048; monster.y = 0; break; case 1: monster.x = 2048; monster.y = Math.random() * 2732; break; case 2: monster.x = Math.random() * 2048; monster.y = 2732; break; case 3: monster.x = 0; monster.y = Math.random() * 2732; break; } monsters.push(monster); } function resetLevel() { levelResetCount++; // Remove existing trees and monsters trees.forEach(function (tree) { tree.destroy(); }); monsters.forEach(function (monster) { monster.destroy(); }); trees = []; monsters = []; // Add trees for (var i = 0; i < 5 + levelResetCount; i++) { var tree; var positionValid; do { positionValid = true; tree = new Tree(); tree.x = Math.random() * 2048; tree.y = Math.random() * 2732; // Check if the tree spawns on top of the hero if (tree.intersects(hero)) { positionValid = false; } } while (!positionValid); game.addChild(tree); trees.push(tree); } // Add monsters for (var i = 0; i < 6 + levelResetCount; i++) { var monster = game.addChild(new Monster()); var edge = Math.floor(Math.random() * 4); switch (edge) { case 0: // Top edge monster.x = Math.random() * 2048; monster.y = 0; break; case 1: // Right edge monster.x = 2048; monster.y = Math.random() * 2732; break; case 2: // Bottom edge monster.x = Math.random() * 2048; monster.y = 2732; break; case 3: // Left edge monster.x = 0; monster.y = Math.random() * 2732; break; } monsters.push(monster); } } var levelResetCount = 1; var bulletCount = 10; var bulletCountTxt = new Text2('Bullets: ' + bulletCount, { size: 30, fill: '#ffffff' }); LK.gui.bottomRight.addChild(bulletCountTxt); bulletCountTxt.anchor.set(1, 0); bulletCountTxt.x = -50; bulletCountTxt.y = -50; // Create a reset button var resetButton = new Text2('Reset Bullets', { size: 30, fill: '#ffffff' }); LK.gui.bottomLeft.addChild(resetButton); resetButton.anchor.set(0, 0); resetButton.x = 50; resetButton.y = -50; // Add an event listener to the reset button game.on('down', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); if (resetButton.intersects({ x: pos.x, y: pos.y, width: 1, height: 1 })) { bulletCount = 10; bulletCountTxt.setText('Bullets: ' + bulletCount); } }); var score = 0; var scoreTxt = new Text2('# of Monsters Killed: 0', { size: 30, fill: '#ffffff' }); LK.gui.topRight.addChild(scoreTxt); scoreTxt.anchor.set(1, 0); scoreTxt.x = -50; scoreTxt.y = 50; var levelResetTxt = new Text2(levelResetCount.toString(), { size: 30, fill: '#ffffff' }); LK.gui.top.addChild(levelResetTxt); levelResetTxt.anchor.set(1, 0); levelResetTxt.y = 50; // Add a button for mobile devices to fire the bullet
===================================================================
--- original.js
+++ change.js
@@ -371,9 +371,14 @@
// Add an event listener to the reset button
game.on('down', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
- if (resetButton.intersectsPoint(pos.x, pos.y)) {
+ if (resetButton.intersects({
+ x: pos.x,
+ y: pos.y,
+ width: 1,
+ height: 1
+ })) {
bulletCount = 10;
bulletCountTxt.setText('Bullets: ' + bulletCount);
}
});
Make a Tree. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Create a monster that is half bat half ogre. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
create a small bullet projectile. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Create a single bearded man in a suit reaching out holding an imaginary weapon, I want him generated in 32 bit graphic style and i want to see his entire body. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.