Code edit (8 edits merged)
Please save this source code
User prompt
create an asset to set the background called "happyBkg1"
Code edit (1 edits merged)
Please save this source code
User prompt
update code so that heroship follows the pointer
User prompt
Fix Bug: 'TypeError: LK.tween is not a function' in this line: 'LK.tween(self, {' Line Number: 25
User prompt
make it so the heroship moves towards the pointer. tween the position to make the movement smooth.
Initial prompt
Smiley shots
/**** * Classes ****/ // 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.moveLeft = function () { self.x -= self.speed; }; self.moveRight = function () { self.x += self.speed; }; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - self.height / 2; game.addChild(bullet); return bullet; }; self.moveTo = function (targetX, targetY) { var distance = Math.sqrt(Math.pow(targetX - self.x, 2) + Math.pow(targetY - self.y, 2)); var duration = distance / self.speed * 60; // Duration based on speed and 60FPS self.customTween(self, targetX, targetY, duration); }; }); // 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; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Touch controls for hero ship to move towards touch position // Initialize important asset arrays game.on('down', function (obj) { var touchPos = obj.event.getLocalPosition(game); heroShip.moveTo(touchPos.x, heroShip.y); }); var heroBullets = []; var enemyShips = []; // Create hero ship var heroShip = game.addChild(new HeroShip()); heroShip.x = game.width / 2; heroShip.y = game.height - 150; // Create enemies function spawnEnemy() { var enemy = new EnemyShip(); enemy.x = Math.random() * (game.width - enemy.width) + enemy.width / 2; enemy.y = -enemy.height / 2; enemyShips.push(enemy); game.addChild(enemy); } // Spawn initial enemies for (var i = 0; i < 5; i++) { spawnEnemy(); } // Game tick event LK.on('tick', function () { // 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 (LK.ticks % 30 == 0) { heroBullets.push(heroShip.shoot()); } });
===================================================================
--- original.js
+++ change.js
@@ -21,12 +21,9 @@
};
self.moveTo = function (targetX, targetY) {
var distance = Math.sqrt(Math.pow(targetX - self.x, 2) + Math.pow(targetY - self.y, 2));
var duration = distance / self.speed * 60; // Duration based on speed and 60FPS
- LK.tween(self, {
- x: targetX,
- y: targetY
- }, duration);
+ self.customTween(self, targetX, targetY, duration);
};
});
// Hero bullet class
var HeroBullet = Container.expand(function () {
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.