User prompt
Make the score on top.
User prompt
Make every astro that the spaceship dodges, make that count as a point.
User prompt
No, just make them come down in your direction.
User prompt
Yes, but make the asteroids come for you.
User prompt
Make it go left to right. It can't move up or down.
Initial prompt
asteroid dodger
/**** * Classes ****/ // Asteroid class var Asteroid = Container.expand(function () { var self = Container.call(this); var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { var dx = spaceship.x - self.x; var dy = spaceship.y - self.y; var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * self.speed; self.y += Math.sin(angle) * self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Spaceship class var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Spaceship update logic if needed }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize spaceship var spaceship = game.addChild(new Spaceship()); spaceship.x = 2048 / 2; spaceship.y = 2732 - 200; // Array to hold asteroids var asteroids = []; // Function to handle spaceship movement function handleMove(x, y, obj) { spaceship.x = x; // spaceship.y = y; // Commented out to restrict vertical movement } // Game move event game.move = handleMove; // Game update function game.update = function () { // Create new asteroids if (LK.ticks % 60 == 0) { var newAsteroid = new Asteroid(); newAsteroid.x = Math.random() * 2048; newAsteroid.y = -50; asteroids.push(newAsteroid); game.addChild(newAsteroid); } // Update asteroids for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; asteroid.update(); // Check for collision with spaceship if (spaceship.intersects(asteroid)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } // Remove off-screen asteroids if (asteroid.y > 2732 + 50) { asteroid.destroy(); asteroids.splice(i, 1); } } };
===================================================================
--- original.js
+++ change.js
@@ -9,9 +9,13 @@
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
- self.y += self.speed;
+ var dx = spaceship.x - self.x;
+ var dy = spaceship.y - self.y;
+ var angle = Math.atan2(dy, dx);
+ self.x += Math.cos(angle) * self.speed;
+ self.y += Math.sin(angle) * self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>