Code edit (2 edits merged)
Please save this source code
User prompt
arregla el bug que hace que aparezcan arrow no deseados en el nivel de ninja
User prompt
el enemigo Ninja funciona mal, corrige lo siguiente: - Ninja aunque se destruya parece seguir haciendo daño a player - mejora su funcionalidad para que sea más dinamica
User prompt
Please fix the bug: 'Uncaught ReferenceError: star is not defined' in or related to this line: 'var levelNames = ['endless', 'arrow', 'saw', 'ninja', star]; // Added new level with id 3' Line Number: 322
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'x')' in or related to this line: 'levelSelectors[i].levelText.x = levelSelectors[i].x; // Move the text along with the selector' Line Number: 365
User prompt
haz que los textos de los select level tambien se muevan
User prompt
agrega un scrolling lateral, presionando en cualquier lugar de la pantalla, a los select level (endless, arrow, saw, etc)
User prompt
agrega un scrolling lateral a los select level (endless, arrow, saw, etc)
User prompt
agrega un scrolling a los select level
User prompt
Crea una nueva pagina por separado con el fondo credits al tocar el boton del mismo nombre (solo el fondo y que se salga al tocarlo)
User prompt
haz que al tocar credits aparezca el asset con el mismo nombre y termine al tocarlo
User prompt
corrige el bug que hace que los ninja se duliquen en 4 aunque se hayan destruido
User prompt
si el ninja es destruido al colisionar con player que no se duplique
Code edit (5 edits merged)
Please save this source code
User prompt
haz que ninja se divida en 4 con angulo de 90°
Code edit (1 edits merged)
Please save this source code
User prompt
haz que en vez de a x distancia des despues de unos 3 segundos
User prompt
haz que la rotación de ninja no se detenga
User prompt
haz que enemy ninja se detenga y despues de 5 segundos cree los newarrow y se destruya
Code edit (4 edits merged)
Please save this source code
User prompt
mejora la logica de creación y desaparición de los enemy para no consumir tantos recursos
Code edit (1 edits merged)
Please save this source code
User prompt
haz que los textos sean reusables
User prompt
Please fix the bug: 'startButtonAsset is not defined' in or related to this line: 'startButtonAsset.down = function (x, y, obj) {' Line Number: 634
/****
* Plugins
****/
var facekit = LK.import("@upit/facekit.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('Background', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 2048 / 2; // Set the x position to the center of the screen
self.y = 2732 / 2; // Set the y position to the center of the screen
});
var Button = Container.expand(function (text, x, y, callback) {
var self = Container.call(this);
var buttonAsset = self.attachAsset('Buttom', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
buttonAsset.x = x;
buttonAsset.y = y;
var buttonText = new Text2(text, {
size: 100,
fill: 0xFFFFFF,
font: "'Press Start 2P', cursive"
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = x;
buttonText.y = y;
self.addChild(buttonText);
buttonAsset.down = callback;
});
var EnemyArrow = Container.expand(function () {
var self = Container.call(this);
var bEnemyArrowGraphics = self.attachAsset('EnemyArrow', {
anchorX: 0.5,
anchorY: 0.5
});
enemyLogic(self);
var targetX = player.x;
var targetY = player.y;
self.update = function () {
if (!gamestart) {
return;
}
var dx = targetX - self.x;
var dy = targetY - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * 8.5;
self.y += Math.sin(angle) * 8.5;
self.rotation = angle;
if (Math.abs(self.x - targetX) < 5 && Math.abs(self.y - targetY) < 5) {
targetX += Math.cos(angle) * 1000;
targetY += Math.sin(angle) * 1000;
}
if (self.x < 0 || self.x > 3000 || self.y < 0 || self.y > 3000) {
self.destroy();
}
};
});
var EnemyNinja = Container.expand(function () {
var self = Container.call(this);
var bEnemyNinjaGraphics = self.attachAsset('EnemyNinja', {
anchorX: 0.5,
anchorY: 0.5
});
enemyLogic(self);
var targetX = player.x;
var targetY = player.y;
self.update = function () {
if (!gamestart) {
return;
}
var dx = targetX - self.x;
var dy = targetY - self.y;
var angle = Math.atan2(dy, dx);
self.rotation += 0.05;
if (self.hasStopped) {
self.x += Math.cos(angle) * 10; // Ninja moves faster
self.y += Math.sin(angle) * 10;
} else {
// Add dynamic movement when not stopped
self.x += Math.cos(angle) * 5 * Math.sin(LK.ticks / 20);
self.y += Math.sin(angle) * 5 * Math.cos(LK.ticks / 20);
}
if (Math.abs(self.x - targetX) < 5 && Math.abs(self.y - targetY) < 5) {
targetX += Math.cos(angle) * 1000;
targetY += Math.sin(angle) * 1000;
}
if (!self.hasStopped) {
self.hasStopped = true;
LK.setTimeout(function () {
if (!self.destroyed) {
// Check if EnemyNinja is not destroyed
var angleOffset = Math.PI / 2; // 90-degree angles
for (var i = 0; i < 4; i++) {
var newArrow = new NewArrow();
newArrow.x = self.x;
newArrow.y = self.y;
newArrow.rotation = angle + i * angleOffset;
enemyArrows.push(newArrow);
game.addChild(newArrow);
}
self.destroy(); // Destroy EnemyNinja after creating arrows
enemyNinjas.splice(enemyNinjas.indexOf(self), 1); // Remove from enemyNinjas array
}
}, 3000); // Wait for 3 seconds
}
if (self.x < 0 || self.x > 3000 || self.y < 0 || self.y > 3000) {
self.destroy();
}
};
});
var EnemySaw = Container.expand(function () {
var self = Container.call(this);
var bEnemySawGraphics = self.attachAsset('EnemySaw', {
anchorX: 0.5,
anchorY: 0.5
});
enemyLogic(self);
var targetX = player.x;
var targetY = player.y;
self.update = function () {
if (!gamestart) {
return;
}
var dx = targetX - self.x;
var dy = targetY - self.y;
var angle = Math.atan2(dy, dx);
if (!self.stopSaw && !self.hasStopped && Math.sqrt(dx * dx + dy * dy) < 100) {
self.stopSaw = true;
self.hasStopped = true;
LK.setTimeout(function () {
self.stopSaw = false;
targetX = player.x;
targetY = player.y;
}, 2000);
}
if (!self.stopSaw) {
var speedMultiplier = self.hasStopped ? 1.5 : 1;
self.x += Math.cos(angle) * 7.5 * speedMultiplier;
self.y += Math.sin(angle) * 7.5 * speedMultiplier;
}
self.rotation += 0.03;
if (Math.abs(self.x - targetX) < 5 && Math.abs(self.y - targetY) < 5) {
targetX += Math.cos(angle) * 1000;
targetY += Math.sin(angle) * 1000;
}
// Check if the EnemySaw has moved to the opposite side of its spawn point
if (self.x < 0 || self.x > 3000 || self.y < 0 || self.y > 3000) {
self.destroy();
}
};
});
var Energy = Container.expand(function () {
var self = Container.call(this);
var energyGraphics = self.attachAsset('Energy', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 100; // Initial energy value
self.decrease = function (amount) {
self.value = Math.max(0, self.value - amount);
};
self.increase = function (amount) {
self.value = Math.min(100, self.value + amount);
};
});
var Life = Container.expand(function () {
var self = Container.call(this);
var lifeGraphics = self.attachAsset('Life', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 100; // Initial life value
self.decrease = function (amount) {
self.value = Math.max(0, self.value - amount);
if (self.value === 0) {
lifeQuantity = Math.max(0, lifeQuantity - 1);
updateLifeIcons();
if (lifeQuantity === 0) {
self.destroy();
LK.showGameOver();
}
}
};
self.increase = function (amount) {
self.value = Math.min(100, self.value + amount);
};
});
var NewArrow = Container.expand(function () {
var self = Container.call(this);
var newArrowGraphics = self.attachAsset('EnemyArrow', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (!gamestart) {
return;
}
self.x += Math.cos(self.rotation) * 7.5; // Double the speed of regular arrows
self.y += Math.sin(self.rotation) * 7.5;
if (self.x < 0 || self.x > 3000 || self.y < 0 || self.y > 3000) {
self.destroy();
}
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
var faceGraphics = self.attachAsset('faceObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (!gamestart) {
return;
}
var smoothing = 0.05;
var targetX = facekit.noseTip.x;
var targetY = facekit.noseTip.y;
var leftBound = (2048 - 1000) / 2;
var rightBound = leftBound + 1000;
var topBound = (2732 - 1000) / 2;
var bottomBound = topBound + 1000;
targetX = Math.max(leftBound, Math.min(rightBound, targetX));
targetY = Math.max(topBound, Math.min(bottomBound, targetY));
self.x += (targetX - self.x) * smoothing;
self.y += (targetY - self.y) * smoothing;
var allEnemies = enemyArrows.concat(enemySaw, enemyNinjas);
for (var i = 0; i < allEnemies.length; i++) {
if (self.intersects(allEnemies[i])) {
lifeQuantity = Math.max(0, lifeQuantity - 1);
updateLifeIcons();
allEnemies[i].destroy();
if (enemyArrows.includes(allEnemies[i])) {
enemyArrows.splice(enemyArrows.indexOf(allEnemies[i]), 1);
} else if (enemySaw.includes(allEnemies[i])) {
enemySaw.splice(enemySaw.indexOf(allEnemies[i]), 1);
} else if (enemyNinjas.includes(allEnemies[i])) {
enemyNinjas.splice(enemyNinjas.indexOf(allEnemies[i]), 1);
}
if (lifeQuantity === 0) {
self.destroy();
gamestart = false;
LK.showGameOver();
}
break;
}
}
};
});
var RangedLimiter = Container.expand(function () {
var self = Container.call(this);
var limiterGraphics = self.attachAsset('RangedLimiter', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5
});
});
var TextElement = Container.expand(function (text, size, color, font, x, y) {
var self = Container.call(this);
var textElement = new Text2(text, {
size: size,
fill: color,
font: font
});
textElement.anchor.set(0.5, 0.5);
textElement.x = x;
textElement.y = y;
self.addChild(textElement);
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
function showSelectLevelPage() {
// Create a new container for the select level page
var selectLevelContainer = new Container();
// Add a background to the select level page
var selectLevelBackground = LK.getAsset('BackgroundMenu', {
anchorX: 0.5,
anchorY: 0.5
});
selectLevelBackground.x = 2048 / 2;
selectLevelBackground.y = 2732 / 2;
selectLevelContainer.addChild(selectLevelBackground);
// Add a title to the select level page
var selectLevelTitle = new TextElement('Select Level', 150, 0xFFFFFF, "'Press Start 2P', cursive", 2048 / 2, 500);
selectLevelContainer.addChild(selectLevelTitle);
// Add a back button to return to the main menu
var backButton = new Button('Back', 2048 / 2, 2300, function (x, y, obj) {
selectLevelContainer.visible = false; // Hide the select level page
menuContainer.visible = true; // Show the main menu
});
selectLevelContainer.addChild(backButton);
// Add level selectors
var levelNames = ['endless', 'arrow', 'saw', 'ninja', 'star']; // Added new level with id 3
var levelSelectors = []; // Array to store level selectors
for (var i = 0; i < levelNames.length; i++) {
var levelSelector = LK.getAsset('level', {
anchorX: 0.5,
anchorY: 0.5
});
levelSelector.x = 2048 / 2 - 600 + i * 600; // Position each level selector in a row
levelSelector.y = 1500; // Align vertically
levelSelector.levelType = levelNames[i];
levelSelector.down = function (index) {
// Create a closure to capture the current index
return function (x, y, obj) {
gamestart = true; // Start the game
selectLevelContainer.visible = false; // Hide the select level page
levelID = index; // Update levelID based on the order of the level selector
console.log("Starting level:", obj.levelType); // Log the selected level type
// Additional logic to initialize the selected level can be added here
};
}(i); // Pass the current index to the closure
selectLevelContainer.addChild(levelSelector);
// Add level text below each level selector
var levelText = new Text2(levelNames[i], {
size: 80,
fill: 0xFFFFFF,
font: "'Press Start 2P', cursive"
});
levelText.anchor.set(0.5, 0.5);
levelText.x = levelSelector.x;
levelText.y = levelSelector.y + 300; // Position text below the selector
selectLevelContainer.addChild(levelText);
levelSelector.levelText = levelText; // Store levelText in levelSelector
levelSelectors.push(levelSelector); // Add to array
}
// Add scrolling functionality
selectLevelContainer.down = function (x, y, obj) {
var startX = x;
var initialPositions = levelSelectors.map(function (selector) {
return selector.x;
});
selectLevelContainer.move = function (moveX, moveY, moveObj) {
var deltaX = moveX - startX;
for (var i = 0; i < levelSelectors.length; i++) {
levelSelectors[i].x = initialPositions[i] + deltaX;
levelSelectors[i].levelText.x = levelSelectors[i].x; // Move the text along with the selector
}
};
selectLevelContainer.up = function (upX, upY, upObj) {
selectLevelContainer.move = null;
selectLevelContainer.up = null;
};
};
game.addChild(selectLevelContainer);
}
var levelID = 0;
var lifeQuantity = 3;
var energyQuantity = 100;
var gamestart = false;
function enemyLogic(entity) {
var spawnPositions = [{
x: Math.random() * 2000,
y: 0
}, {
x: Math.random() * 2000,
y: 3000
}, {
x: 0,
y: Math.random() * 2000
}, {
x: 3000,
y: Math.random() * 2000
}];
var position = spawnPositions[Math.floor(Math.random() * spawnPositions.length)];
entity.x = position.x;
entity.y = position.y;
entity.spawnTime = entity.spawnTime || Math.random() * 2000 + 2000;
}
var background = game.addChild(new Background());
var timerTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
timerTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(timerTxt);
var elapsedTime = 0;
var timerInterval = LK.setInterval(function () {
if (!gamestart) {
return;
} // Ensure timer only updates when gamestart is true
elapsedTime++;
var minutes = Math.floor(elapsedTime / 60); // Calculate minutes
var seconds = elapsedTime % 60; // Calculate remaining seconds
var timeString = minutes > 0 ? minutes + "m " + seconds + "s" : seconds + "s"; // Format time string
timerTxt.setText(timeString);
}, 1000);
var player = game.addChild(new Player());
player.x = 2048 / 2; // Center the player on the x-axis
player.y = 2732 / 2; // Center the player on the y-axis
game.up = function (x, y, obj) {
player.scaleX = 1;
player.scaleY = 1;
};
game.down = function (x, y, obj) {
player.scaleX = 0.5;
player.scaleY = 0.5;
};
// Create a list to store EnemyArrow instances
var enemyArrows = [];
// Create a timer that creates a new EnemyArrow every random interval between 1 and 2 seconds
function spawnEnemy(type, interval, levelCheck, enemyArray, enemyClass) {
return LK.setInterval(function () {
if (!gamestart || !levelCheck()) {
return;
}
var newEnemy = game.addChild(new enemyClass());
enemyArray.push(newEnemy);
LK.setTimeout(function () {
if (enemyArray.includes(newEnemy)) {
newEnemy.destroy();
enemyArray.splice(enemyArray.indexOf(newEnemy), 1);
}
}, 20000);
}, interval);
}
var enemySaw = [];
var enemyNinjas = [];
var enemyArrowInterval = spawnEnemy('arrow', Math.random() * 380 + 1000, function () {
return levelID === 0 || levelID === 1;
}, enemyArrows, EnemyArrow);
var enemySawInterval = spawnEnemy('saw', 2000, function () {
return levelID === 0 || levelID === 2;
}, enemySaw, EnemySaw);
var enemyNinjaInterval = spawnEnemy('ninja', 1500, function () {
return levelID === 3;
}, enemyNinjas, EnemyNinja);
var lifeIcons = [];
for (var i = 0; i < lifeQuantity; i++) {
var lifeIcon = game.addChild(new Life());
lifeIcon.x = 2048 / 2 - 600 + i * 160; // Position each life icon to the right of the previous one
lifeIcon.y = 100; // Align vertically with the timer
lifeIcons.push(lifeIcon);
}
// Function to update life icons when life decreases
function updateLifeIcons() {
for (var i = 0; i < lifeIcons.length; i++) {
if (i < lifeQuantity) {
lifeIcons[i].visible = true;
} else {
lifeIcons[i].visible = false;
}
}
}
var energy = game.addChild(new Energy());
energy.x = 2048 / 2 + 600; // Position energy to the right of the timer
energy.y = 100; // Align vertically with the timer
var energyText = new Text2(energyQuantity + '%', {
size: 80,
fill: 0xFFFFFF
});
energyText.anchor.set(1, 0.5); // Anchor to the right center
energyText.x = energy.x - 100; // Position to the left of energy
energyText.y = energy.y;
game.addChild(energyText);
var rangerlimited = game.addChild(new RangedLimiter());
rangerlimited.x = 2048 / 2; // Center the RangedLimiter on the x-axis
rangerlimited.y = 2732 / 2; // Center the RangedLimiter on the y-axis
// Create an initial menu
var menuContainer = new Container();
/*
* Menu inicial
*/
var menuBackground = LK.getAsset('BackgroundMenu', {
anchorX: 0.5,
anchorY: 0.5
});
menuBackground.x = 2048 / 2;
menuBackground.y = 2732 / 2;
menuContainer.addChild(menuBackground);
// Add a title to the menu
var title = new TextElement('Game Title', 150, 0xFFFFFF, "'Press Start 2P', cursive", 2048 / 2, 500);
menuContainer.addChild(title);
// Create buttons
var startButton = new Button('Game Start', 2048 / 2, 1000, function (x, y, obj) {
menuContainer.visible = false; // Hide the menu
showSelectLevelPage(); // Redirect to 'select level' page
});
menuContainer.addChild(startButton);
var recordButton = new Button('Record', 2048 / 2, 1300, function (x, y, obj) {
var tempMessage = new Text2('Option not yet implemented due to engine errors', {
size: 40,
fill: 0xFF0000,
font: "'Press Start 2P', cursive"
});
tempMessage.anchor.set(0.5, 0.5);
tempMessage.x = 2048 / 2;
tempMessage.y = 2732 - 300; // Position at the bottom of the screen
game.addChild(tempMessage);
LK.setTimeout(function () {
game.removeChild(tempMessage);
}, 3000);
});
menuContainer.addChild(recordButton);
var howToPlayButton = new Button('How to Play', 2048 / 2, 1600, function (x, y, obj) {
// Add logic for how to play button
});
menuContainer.addChild(howToPlayButton);
var creditsButton = new Button('Credits', 2048 / 2, 1900, function (x, y, obj) {
showCreditsPage();
});
menuContainer.addChild(creditsButton);
function showCreditsPage() {
// Create a new container for the credits page
var creditsContainer = new Container();
// Add the credits background
var creditsBackground = LK.getAsset('Credits', {
anchorX: 0.5,
anchorY: 0.5
});
creditsBackground.x = 2048 / 2;
creditsBackground.y = 2732 / 2;
creditsContainer.addChild(creditsBackground);
// Add event listener to hide credits page on touch
creditsBackground.down = function (x, y, obj) {
creditsContainer.visible = false; // Hide the credits page
menuContainer.visible = true; // Show the main menu
};
// Add the credits page to the game
game.addChild(creditsContainer);
}
game.addChild(menuContainer);
;
// Function to display the records page
function showRecordsPage() {
// Create a new container for the records page
var recordsContainer = new Container();
// Add a black background to the records page
var recordsBackground = LK.getAsset('BackgroundMenu', {
anchorX: 0.5,
anchorY: 0.5
});
recordsBackground.x = 2048 / 2;
recordsBackground.y = 2732 / 2;
recordsContainer.addChild(recordsBackground);
// Add a title to the records page
var recordsTitle = new TextElement('Records', 150, 0xFFFFFF, "'Press Start 2P', cursive", 2048 / 2, 500);
recordsContainer.addChild(recordsTitle);
var backButtonAsset = LK.getAsset('Buttom', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
backButtonAsset.x = 2048 / 2;
backButtonAsset.y = 2300;
recordsContainer.addChild(backButtonAsset);
var backButton = new Text2('Back', {
size: 100,
fill: 0xFFFFFF,
font: "'Press Start 2P', cursive"
});
backButton.anchor.set(0.5, 0.5);
backButton.x = 2048 / 2;
backButton.y = 2400;
recordsContainer.addChild(backButton);
// Add event listener to 'Back' button
backButtonAsset.down = function (x, y, obj) {
recordsContainer.visible = false; // Hide the records page
menuContainer.visible = true; // Show the main menu
};
// Add the records page to the game
game.addChild(recordsContainer);
} ===================================================================
--- original.js
+++ change.js
@@ -83,8 +83,12 @@
self.rotation += 0.05;
if (self.hasStopped) {
self.x += Math.cos(angle) * 10; // Ninja moves faster
self.y += Math.sin(angle) * 10;
+ } else {
+ // Add dynamic movement when not stopped
+ self.x += Math.cos(angle) * 5 * Math.sin(LK.ticks / 20);
+ self.y += Math.sin(angle) * 5 * Math.cos(LK.ticks / 20);
}
if (Math.abs(self.x - targetX) < 5 && Math.abs(self.y - targetY) < 5) {
targetX += Math.cos(angle) * 1000;
targetY += Math.sin(angle) * 1000;
@@ -103,8 +107,9 @@
enemyArrows.push(newArrow);
game.addChild(newArrow);
}
self.destroy(); // Destroy EnemyNinja after creating arrows
+ enemyNinjas.splice(enemyNinjas.indexOf(self), 1); // Remove from enemyNinjas array
}
}, 3000); // Wait for 3 seconds
}
if (self.x < 0 || self.x > 3000 || self.y < 0 || self.y > 3000) {