User prompt
when the dog moves to the right, show dog and hide dog2 when the dog moves to the left, show dog2 and hide dog
User prompt
when the dog moves to the right show the dog, when the dog moves to the left show the dog2 and hide the dog.
User prompt
when the dog moves to the right show the dog, when the dog moves to the left show the dog2 and hide the dog.
User prompt
when the dog moves to the right show the dog, when the dog moves to the left show the dog2
User prompt
double the speed of the sheep
User prompt
the sheep must always move away from the dog
User prompt
the sheep must always move away from the dog
User prompt
sheep should not be born closer than 300 pixels to the edges of the screen and should not be closer than 300 pixels to the edges of the screen
User prompt
turn the sheep 30 degrees in the direction of its movement
User prompt
the speed of the sheep must drop back to the previous value at the first collision with another sheep.
User prompt
Make a collision for dog and sheep, allow the sheep to change speed when colliding with the dog in the direction of impact
User prompt
the speed of the sheep must drop back to the previous value at the first collision with another sheep.
User prompt
make a collision for dog and sheep, allow the sheep to change speed when colliding with the dog
User prompt
make a collision for dog and sheep, allow the sheep to change speed.
User prompt
make the sheep bounce off the dog.
User prompt
make the sheep bounce off the dog.
User prompt
make the sheep bounce off the dog.
User prompt
make the sheep bounce off the dog.
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 56
User prompt
introduce collisions for dog
User prompt
enter collisions for the dog
User prompt
sheep should not be born closer than 100 pixels to the edges of the screen and should not be closer than 100 pixels to the edges of the screen
User prompt
sheep should not be born closer than 100 pixels to the edges of the screen and should not be born closer than 100 pixels to the edges of the screen
User prompt
sheep should not be closer than 100 pixels to the edges of the screen
User prompt
introduce collisions for dog
/**** * Classes ****/ // Sheep class var Sheep = Container.expand(function () { var self = Container.call(this); var sheepGraphics = self.attachAsset('sheep', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.direction = Math.random() * Math.PI * 2; self.move = function () { self.x += Math.cos(self.direction) * self.speed; self.y += Math.sin(self.direction) * self.speed; }; self.avoidWalls = function () { if (self.x < 300 || self.x > 1748 || self.y < 300 || self.y > 2432) { self.direction += Math.PI; } }; self.avoidDog = function (dog) { if (self.intersects(dog)) { self.direction += Math.PI / 2; } }; self.avoidSheep = function (sheepArray) { var personalSpace = 150; // Define a personal space radius for each sheep for (var i = 0; i < sheepArray.length; i++) { var otherSheep = sheepArray[i]; if (self !== otherSheep) { var dx = otherSheep.x - self.x; var dy = otherSheep.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < personalSpace) { var avoidanceForce = personalSpace - distance; var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction self.direction += (avoidanceAngle - self.direction) * avoidanceForce / personalSpace; } } } }; }); // Shepherd dog class var Dog = Container.expand(function () { var self = Container.call(this); var dogGraphics = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (pos) { if (pos.x > self.x) { self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); if (self.getChildByName('dog2')) { self.removeChild(self.getChildByName('dog2')); } } else if (pos.x < self.x) { self.attachAsset('dog2', { anchorX: 0.5, anchorY: 0.5, name: 'dog2' }); if (self.getChildByName('dog')) { self.removeChild(self.getChildByName('dog')); } } self.x = pos.x; self.y = pos.y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x90EE90 // Light green background }); /**** * Game Code ****/ // Shepherd dog asset // Sheep asset // Initialize sheep array var sheepArray = []; // Create 20 sheep and add them to the game and sheepArray for (var i = 0; i < 20; i++) { var sheep = new Sheep(); sheep.x = 300 + Math.random() * (2048 - 600); sheep.y = 300 + Math.random() * (2732 - 600); game.addChild(sheep); sheepArray.push(sheep); } // Create shepherd dog and add it to the game var shepherdDog = new Dog(); shepherdDog.x = 1024; // Start in the middle of the game area shepherdDog.y = 1366; game.addChild(shepherdDog); // Event listener for touch move to move the shepherd dog game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); // Main game tick event LK.on('tick', function () { // Move each sheep and handle collisions for (var i = 0; i < sheepArray.length; i++) { var sheep = sheepArray[i]; sheep.move(); sheep.avoidWalls(); sheep.avoidDog(shepherdDog); sheep.avoidSheep(sheepArray); } }); // Ensure the game is touchscreen compatible game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); game.on('up', function (obj) { // This can be used for future features where the dog stops moving on touch up });
===================================================================
--- original.js
+++ change.js
@@ -43,29 +43,33 @@
});
// Shepherd dog class
var Dog = Container.expand(function () {
var self = Container.call(this);
- self.previousX = self.x;
var dogGraphics = self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
- self.previousX = self.x;
self.move = function (pos) {
- self.x = pos.x;
- self.y = pos.y;
- if (self.x > self.previousX) {
+ if (pos.x > self.x) {
self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
- } else if (self.x < self.previousX) {
+ if (self.getChildByName('dog2')) {
+ self.removeChild(self.getChildByName('dog2'));
+ }
+ } else if (pos.x < self.x) {
self.attachAsset('dog2', {
anchorX: 0.5,
- anchorY: 0.5
+ anchorY: 0.5,
+ name: 'dog2'
});
+ if (self.getChildByName('dog')) {
+ self.removeChild(self.getChildByName('dog'));
+ }
}
- self.previousX = self.x;
+ self.x = pos.x;
+ self.y = pos.y;
};
});
/****
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Collie shepherd dog, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a white text bubble that says "baa," comic book style.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
double-sided blue arrow in the form of a semicircle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
computer mouse top view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
angry big bear, lying down and sleeping, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
erase