User prompt
when all of the monsters are killed I want the trees and monsters to start over again as a fresh level
User prompt
Have the monsters slowly chase the hero
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'var distanceToTarget = Math.sqrt(Math.pow(hero.targetPosition.x - hero.x, 2) + Math.pow(hero.targetPosition.y - hero.y, 2));' Line Number: 151
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'var distanceToTarget = Math.sqrt(Math.pow(hero.targetPosition.x - hero.x, 2) + Math.pow(hero.targetPosition.y - hero.y, 2));' Line Number: 151
User prompt
the hero should stop moving when it gets to the target position
User prompt
the hero should walk towards where ever the player clicks the left click on the screen
User prompt
lets do less trees
User prompt
set the score in a position where it can be seen on the screen
User prompt
Fix Bug: 'ReferenceError: heroInfo is not defined' in or related to this line: 'heroInfo.setText('Speed: ' + hero.speed + ', Direction: (' + hero.direction.x.toFixed(2) + ', ' + hero.direction.y.toFixed(2) + ')');' Line Number: 141
User prompt
Fix Bug: 'Uncaught ReferenceError: heroInfo is not defined' in or related to this line: 'LK.gui.top.addChild(heroInfo);' Line Number: 107
User prompt
show a score at the top that goes up every time i kill a monster
User prompt
lets add some monsters to the game who die when they are hit with a bullet.
User prompt
In tick, if hero is touching a tree then move away from the tree in the direction he approached from
User prompt
In tick, if the hero is intersecting a tree, make the hero slowly move away from the tree
User prompt
Make sure trees are added to the trees array
User prompt
Just use intersects to test if hero is walking into a tree
User prompt
the bullet direction should go in the direction of the mouse pointer when it is fired
Code edit (1 edits merged)
Please save this source code
User prompt
the hero speed should not change regardless of the mouse pointer movement speed
Code edit (2 edits merged)
Please save this source code
User prompt
the hero should move slowly
User prompt
when a bullet hits a tree it should be destroyed
Code edit (1 edits merged)
Please save this source code
User prompt
the bullets should move slowly in the direction the hero was moving in
User prompt
the hero bullets needs to move constantly after being fired
/**** * Classes ****/ // 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.speed = 5; self.direction = { x: 0, y: 0 }; self.checkCollision = function (trees) { for (var i = 0; i < trees.length; i++) { if (self.intersects(trees[i])) { return true; } } 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.destroyedByBullet = function () { self.isAlive = false; 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) { 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 ****/ 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 var heroInfo = new Text2('text', { size: 30, fill: '#ffffff' }); LK.gui.top.addChild(heroInfo); heroInfo.anchor.set(0.5, 0); heroInfo.x = 2048 / 2; 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) { dragNode = hero; handleMove(obj); }); // Update the text box every game tick LK.on('tick', function () { heroInfo.setText('Speed: ' + hero.speed + ', Direction: (' + hero.direction.x.toFixed(2) + ', ' + hero.direction.y.toFixed(2) + ')'); for (var i = 0; i < trees.length; i++) { if (hero.intersects(trees[i])) { // Move hero away from the tree hero.x -= hero.direction.x * hero.speed; hero.y -= hero.direction.y * hero.speed; // Reset hero's direction to prevent further collision hero.direction.x = 0; hero.direction.y = 0; break; // Only handle one collision per tick } } }); game.on('up', function (obj) { dragNode = null; }); var bullets = []; game.on('rightdown', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); 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); }); LK.on('tick', function () { for (var i = bullets.length - 1; i >= 0; i--) { if (bullets[i].move(trees)) { bullets.splice(i, 1); } } }); // Add trees for (var i = 0; i < 10; 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()); monster.x = Math.random() * 2048; monster.y = Math.random() * 2732; monsters.push(monster); }
===================================================================
--- original.js
+++ change.js
@@ -28,8 +28,20 @@
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.destroyedByBullet = function () {
+ self.isAlive = false;
+ self.destroy();
+ };
+});
var Bullet = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('bullet', {
anchorX: 0.5,
@@ -43,15 +55,20 @@
self.move = function (trees) {
self.x += self.direction.x * self.speed;
self.y += self.direction.y * self.speed;
for (var i = 0; i < trees.length; i++) {
- var treeBounds = trees[i].getBounds();
- var bulletBounds = self.getBounds();
- if (bulletBounds.x < treeBounds.x + treeBounds.width && bulletBounds.x + bulletBounds.width > treeBounds.x && bulletBounds.y < treeBounds.y + treeBounds.height && bulletBounds.y + bulletBounds.height > treeBounds.y) {
+ 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;
};
});
@@ -154,5 +171,12 @@
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());
+ monster.x = Math.random() * 2048;
+ monster.y = Math.random() * 2732;
+ monsters.push(monster);
}
\ No newline at end of file
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.