Code edit (1 edits merged)
Please save this source code
User prompt
move enemy and player 150 pixels higher
User prompt
player starting position in the y axis should be the same as the one its going to keep on moving in the game after jumping. same for enemy
User prompt
when we update starting position of player we should also update the position where they normally move
User prompt
move player and enemy 200 pixels higher
User prompt
player and enemy should be 100 pixiels higher, where the floor is
Code edit (10 edits merged)
Please save this source code
User prompt
nice, can you now triple the size of the player and the enemy
User prompt
now only 40 pixels higher
User prompt
actually move them both 100 pixels higher
User prompt
player and enemy starting position and movement should be 200 pixels from the bottom of the screen
User prompt
Please fix the bug: 'enemy is not defined' in or related to this line: 'enemy.y = 2732 / 2;' Line Number: 169
User prompt
player and enemy should be 500 pixels down
User prompt
add 3 different backgrounds to use in paraxl scrolling and they should be changed randomly
Code edit (1 edits merged)
Please save this source code
User prompt
transitioning from one frame to the other should be smooth and have not white or transparent space between them
Code edit (3 edits merged)
Please save this source code
User prompt
enemy shoudl also have several images like player to replicate movement. enemy will have 2
Code edit (4 edits merged)
Please save this source code
User prompt
player image shoudl last a little longe before the next one appears so that there are no gaps between them
User prompt
how can we make mario not have glitches? maybe wait a little longer before swaping images in run animation?
User prompt
now make sure background is continuous
User prompt
add lateral paralx scrolling to seem like player is moving
User prompt
reduce jump height
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ // Define a class for the background to implement parallax scrolling var Background = Container.expand(function () { var self = Container.call(this); var backgrounds = ['background1', 'background2', 'background3']; var randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)]; var backgroundGraphics = self.attachAsset(randomBackground, { anchorX: 0, anchorY: 0 }); self.speed = 2; self.update = function () { self.x -= self.speed; if (self.x <= -2048) { self.x = 2048; } }; }); var Background2 = Container.expand(function () { var self = Container.call(this); var backgrounds = ['background1', 'background2', 'background3']; var randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)]; var backgroundGraphics = self.attachAsset(randomBackground, { anchorX: 0, anchorY: 0 }); self.speed = 2; self.update = function () { self.x -= self.speed; if (self.x <= -2048) { self.x = 2048; } }; }); // Define a class for enemies var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy1', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.passed = false; self.runFrames = ['enemy1', 'enemy2']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 30; self.swapSprite = function (newAsset) { var newGraphics = self.attachAsset(newAsset, { anchorX: 0.5, anchorY: 0.5 }); if (enemyGraphics) { enemyGraphics.destroy(); } enemyGraphics = newGraphics; }; self.update = function () { self.x -= self.speed; self.runFrameCounter++; if (self.runFrameCounter >= self.runFrameDelay) { self.runFrameIndex = (self.runFrameIndex + 1) % self.runFrames.length; self.swapSprite(self.runFrames[self.runFrameIndex]); self.runFrameCounter = 0; } if (self.x < -50) { self.destroy(); } }; }); // Define a class for the player character with modified sprite swapping var Player = Container.expand(function () { var self = Container.call(this); // Use two sprites for seamless transitions var currentSprite = self.attachAsset('player_run1', { anchorX: 0.5, anchorY: 0.5, visible: true }); var nextSprite = self.attachAsset('player_run1', { anchorX: 0.5, anchorY: 0.5, visible: false }); self.currentAsset = 'player_run1'; // Track the current asset self.speed = 5; self.jumpHeight = 30; self.isJumping = false; self.velocityY = 0; self.runFrames = ['player_run1', 'player_run2', 'player_run3']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 15; // Adjusted for smoother animation // Helper function to swap sprites using attach and destroy self.swapSprite = function (newAsset) { if (self.currentAsset !== newAsset) { // Attach new sprite and make it visible nextSprite = self.attachAsset(newAsset, { anchorX: 0.5, anchorY: 0.5, visible: true }); // Hide and destroy the old sprite currentSprite.visible = false; currentSprite.destroy(); // Update references currentSprite = nextSprite; self.currentAsset = newAsset; } }; self.update = function () { if (self.isJumping) { // Handle jumping physics self.y += self.velocityY; self.velocityY += 0.8; // Gravity if (self.y >= 2732 / 2) { // Landed on ground self.y = 2732 / 2; self.isJumping = false; self.velocityY = 0; self.swapSprite(self.runFrames[self.runFrameIndex]); // Back to run animation } } else { // Handle running animation self.runFrameCounter++; if (self.runFrameCounter >= self.runFrameDelay) { self.runFrameIndex = (self.runFrameIndex + 1) % self.runFrames.length; self.swapSprite(self.runFrames[self.runFrameIndex]); self.runFrameCounter = 0; } } }; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.velocityY = -self.jumpHeight; self.swapSprite('player_jump'); // Switch to jump sprite self.runFrameCounter = 0; // Reset for smooth transition on landing } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ var background = game.addChild(new Background()); background.x = 0; background.y = 0; var background2 = game.addChild(new Background2()); background2.x = 2048; background2.y = 0; var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; var enemies = []; var enemySpawnInterval = 100; var enemySpawnCounter = 0; var scoreText = new Text2('0', { size: 100, fill: 0xFFFFFF }); LK.gui.top.addChild(scoreText); scoreText.x = 2048 / 2; scoreText.y = 0; game.update = function () { background.update(); background2.update(); player.update(); enemySpawnCounter++; if (enemySpawnCounter >= enemySpawnInterval) { var enemy = new Enemy(); enemy.x = 2048; enemy.y = 2732 / 2; enemies.push(enemy); game.addChild(enemy); enemySpawnInterval = Math.floor(Math.random() * 150) + 50; enemySpawnCounter = 0; } for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (player.intersects(enemies[j])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else if (player.x > enemies[j].x && !enemies[j].passed) { enemies[j].passed = true; LK.setScore(LK.getScore() + 1); scoreText.setText(LK.getScore()); } if (enemies[j].x < -50) { enemies.splice(j, 1); } } }; game.down = function (x, y, obj) { player.jump(); };
===================================================================
--- original.js
+++ change.js
@@ -3,9 +3,11 @@
****/
// Define a class for the background to implement parallax scrolling
var Background = Container.expand(function () {
var self = Container.call(this);
- var backgroundGraphics = self.attachAsset('background', {
+ var backgrounds = ['background1', 'background2', 'background3'];
+ var randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)];
+ var backgroundGraphics = self.attachAsset(randomBackground, {
anchorX: 0,
anchorY: 0
});
self.speed = 2;
@@ -17,9 +19,11 @@
};
});
var Background2 = Container.expand(function () {
var self = Container.call(this);
- var backgroundGraphics = self.attachAsset('background', {
+ var backgrounds = ['background1', 'background2', 'background3'];
+ var randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)];
+ var backgroundGraphics = self.attachAsset(randomBackground, {
anchorX: 0,
anchorY: 0
});
self.speed = 2;
@@ -42,16 +46,13 @@
self.runFrames = ['enemy1', 'enemy2'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
self.runFrameDelay = 30;
- // Helper function to swap sprites safely
self.swapSprite = function (newAsset) {
- // Attach new sprite first
var newGraphics = self.attachAsset(newAsset, {
anchorX: 0.5,
anchorY: 0.5
});
- // Destroy old sprite after new one is in place
if (enemyGraphics) {
enemyGraphics.destroy();
}
enemyGraphics = newGraphics;
@@ -68,47 +69,59 @@
self.destroy();
}
};
});
-// Define a class for the player character
+// Define a class for the player character with modified sprite swapping
var Player = Container.expand(function () {
var self = Container.call(this);
- var playerGraphics = self.attachAsset('player_run1', {
+ // Use two sprites for seamless transitions
+ var currentSprite = self.attachAsset('player_run1', {
anchorX: 0.5,
- anchorY: 0.5
+ anchorY: 0.5,
+ visible: true
});
+ var nextSprite = self.attachAsset('player_run1', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ visible: false
+ });
+ self.currentAsset = 'player_run1'; // Track the current asset
self.speed = 5;
self.jumpHeight = 30;
self.isJumping = false;
self.velocityY = 0;
self.runFrames = ['player_run1', 'player_run2', 'player_run3'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
- self.runFrameDelay = 30;
- // Helper function to swap sprites safely
+ self.runFrameDelay = 15; // Adjusted for smoother animation
+ // Helper function to swap sprites using attach and destroy
self.swapSprite = function (newAsset) {
- // Attach new sprite first
- var newGraphics = self.attachAsset(newAsset, {
- anchorX: 0.5,
- anchorY: 0.5
- });
- // Destroy old sprite after new one is in place
- if (playerGraphics) {
- playerGraphics.destroy();
+ if (self.currentAsset !== newAsset) {
+ // Attach new sprite and make it visible
+ nextSprite = self.attachAsset(newAsset, {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ visible: true
+ });
+ // Hide and destroy the old sprite
+ currentSprite.visible = false;
+ currentSprite.destroy();
+ // Update references
+ currentSprite = nextSprite;
+ self.currentAsset = newAsset;
}
- playerGraphics = newGraphics;
};
self.update = function () {
if (self.isJumping) {
// Handle jumping physics
self.y += self.velocityY;
- self.velocityY += 0.8;
+ self.velocityY += 0.8; // Gravity
if (self.y >= 2732 / 2) {
// Landed on ground
self.y = 2732 / 2;
self.isJumping = false;
self.velocityY = 0;
- self.swapSprite(self.runFrames[self.runFrameIndex]);
+ self.swapSprite(self.runFrames[self.runFrameIndex]); // Back to run animation
}
} else {
// Handle running animation
self.runFrameCounter++;
@@ -122,9 +135,10 @@
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = -self.jumpHeight;
- self.swapSprite('player_jump');
+ self.swapSprite('player_jump'); // Switch to jump sprite
+ self.runFrameCounter = 0; // Reset for smooth transition on landing
}
};
});
@@ -137,38 +151,31 @@
/****
* Game Code
****/
-// Initialize the backgrounds with the new classes
var background = game.addChild(new Background());
background.x = 0;
background.y = 0;
var background2 = game.addChild(new Background2());
background2.x = 2048;
background2.y = 0;
-// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
-// Initialize enemies
var enemies = [];
var enemySpawnInterval = 100;
var enemySpawnCounter = 0;
-// Create a new Text2 object to display the score
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
-// Add the score text to the game GUI at the top center of the screen
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 0;
-// Handle game updates
game.update = function () {
background.update();
background2.update();
player.update();
- // Spawn enemies
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
var enemy = new Enemy();
enemy.x = 2048;
@@ -177,9 +184,8 @@
game.addChild(enemy);
enemySpawnInterval = Math.floor(Math.random() * 150) + 50;
enemySpawnCounter = 0;
}
- // Update enemies and check collisions
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
@@ -193,8 +199,7 @@
enemies.splice(j, 1);
}
}
};
-// Handle player jump on input
game.down = function (x, y, obj) {
player.jump();
};
\ No newline at end of file