User prompt
reduce score size
User prompt
Score is not being updated int he to right o the screen
User prompt
score should be on the top right of the screen
User prompt
show score in the ui
User prompt
Add score
User prompt
make sure flyinkoopas do not spawn in very low position and neither do coins.
User prompt
remove coin collection count
User prompt
remove score from game
User prompt
score is stil not working. Please fix it
User prompt
score is not working. Only get 1 point when I collect the first coin and then nothing. It hosul be very simple. Add 1 coin when a coin is ccolelcted
User prompt
score should be updated with coins
User prompt
flying koopa sould be the same size as koopa
User prompt
Update animations to use somthing like this: The key part is the Player class in the `Player` container. The code that defines sprite animations primarily involves: 1. **Frame Arrays**: The game uses arrays of sprite names to represent animation sequences: ```javascript self.runAnimation = ['playerrun1', 'playerrun2', 'playerrun3', 'playerrun4', 'playerrun5', 'playerrun6']; self.jumpAnimation = ['playerjump1', 'playerjump2', 'playerjump3']; self.attackAnimation = ['playerattack1', 'playerattack2', 'playerattack3', 'playerattack4', 'playerattack5']; ``` 2. **Pre-attaching Sprites**: All animation frames are loaded and attached to the player object with initial visibility settings: ```javascript for (var i = 0; i < self.runAnimation.length; i++) { var sprite = self.attachAsset(self.runAnimation[i], { anchorX: 0.5, anchorY: 0.5 }); sprite.alpha = i === 0 ? 1 : 0; self.sprites.push(sprite); } ``` 3. **Animation Timing**: The code uses counters and speed variables to control animation timing: ```javascript self.animationCounter += self.animationSpeed; if (self.animationCounter >= 1) { self.animationCounter = 0; self.runFrame = (self.runFrame + 1) % self.runAnimation.length; } ``` 4. **Frame Switching**: In the update method, the code toggles sprite visibility based on the current state and animation frame: ```javascript for (var i = 0; i < self.sprites.length; i++) { self.sprites[i].alpha = 0; } // Later in the code self.sprites[self.runFrame].alpha = 1; ``` This approach creates animation by rapidly switching between pre-loaded sprites rather than using a sprite sheet or modifying a single sprite. The same pattern is used for different animations (running, jumping, attacking) with appropriate frame selections based on the character's current state.
User prompt
Fix scoore not counting coins
User prompt
To mitigate flickering, ensure that sprite swapping is handled smoothly, possibly by preloading the next sprite before making the current one invisible, and by adjusting the frame delay to allow each frame to be visible for a sufficient amount of time
User prompt
Aply the same for enmeies
User prompt
Make player animation faster but re use sprites to not show flickering
User prompt
Use each frame twice for player movemnt
User prompt
When mario jumps on an enemy an kills it, make it bounce upwards a little
User prompt
Remove gauge bar, just amke mario jump on tap
User prompt
`self.runAnimation`
User prompt
Please fix the bug: 'Error: Invalid end type for property currentAsset: string' in or related to this line: 'tween(self, {' Line Number: 333
User prompt
Replace sprite swaping bby using animation function ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Fix animation of olayer ru no ing
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'var nextSprite = LK.getAsset(newAsset, {' Line Number: 298
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ 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 = 3; self.update = function () { if (!game.playerDead) { 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 = 3; self.update = function () { if (!game.playerDead) { self.x -= self.speed; } if (self.x <= -2048) { self.x = 2048; } }; }); var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); self.speed = 3; self.collected = false; self.y = 2732 - 600; // Position coins higher than the ground self.update = function () { if (!game.playerDead) { self.x -= self.speed; } if (self.x < -100) { self.destroy(); } }; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('turtle1', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); self.speed = 6 + Math.random() * 2; self.passed = false; self.isJumping = false; self.velocityY = 0; self.groundY = 2732 - 400; // 400 pixels from bottom self.y = self.groundY; // Set initial position to ground level self.runFrames = ['turtle1', 'turtle2']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 20; self.swapSprite = function (newAsset) { var newGraphics = self.attachAsset(newAsset, { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); if (enemyGraphics) { enemyGraphics.destroy(); } enemyGraphics = newGraphics; }; self.update = function () { self.x -= self.speed + Math.floor(LK.ticks / 600) * 0.1; // Increase speed over time if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.5; // Gravity effect if (self.y > 2732) { self.destroy(); } } else { 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 < -100) { self.destroy(); } }; }); var FlyKoopa = Container.expand(function () { var self = Container.call(this); var flyKoopaGraphics = self.attachAsset('flykoopa1', { anchorX: 0.5, anchorY: 0.5, scaleX: 2.0, scaleY: 2.0 }); self.speed = 4 + Math.random() * 2; self.runFrames = ['flykoopa1', 'flykoopa2']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 20; self.swapSprite = function (newAsset) { var newGraphics = self.attachAsset(newAsset, { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); if (flyKoopaGraphics) { flyKoopaGraphics.destroy(); } flyKoopaGraphics = 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.isJumping) { self.y += self.velocityY; self.velocityY += 0.5; // Gravity effect if (self.y > 2732) { self.destroy(); } } else { 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 < -100) { self.destroy(); } }; }); var GaugeBar = Container.expand(function () { var self = Container.call(this); // Background of the gauge var gaugeBackground = self.attachAsset('gauge_background', { anchorX: 0.5, // Center the background horizontally anchorY: 0.5 // Center the background vertically }); // Fill of the gauge var gaugeFill = self.attachAsset('gauge_fill', { anchorX: 0, // Anchor the fill to the left edge anchorY: 0.5 // Center the fill vertically }); self.maxWidth = gaugeFill.width; // Store the maximum width of the fill self.currentValue = 0; // Initial value gaugeFill.scaleX = 0; // Start with the gauge empty // Adjust the fill's position to align with the left edge of the background gaugeFill.x = -self.maxWidth / 2; // Offset to the left edge of the background self.updateGauge = function (value) { self.currentValue = Math.min(100, Math.max(0, value)); // Clamp value between 0 and 100 gaugeFill.scaleX = self.currentValue / 100; // Scale the fill from the left }; }); var Goomba = Container.expand(function () { var self = Container.call(this); var goombaGraphics = self.attachAsset('goomba1', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); self.speed = 4 + Math.random() * 2; self.passed = false; self.isJumping = false; self.velocityY = 0; self.groundY = 2732 - 400; // 400 pixels from bottom self.y = self.groundY; // Set initial position to ground level self.runFrames = ['goomba1', 'goomba2']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 25; self.swapSprite = function (newAsset) { var newGraphics = self.attachAsset(newAsset, { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); if (goombaGraphics) { goombaGraphics.destroy(); } goombaGraphics = newGraphics; }; self.update = function () { self.x -= self.speed; if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.5; // Gravity effect if (self.y > 2732) { self.destroy(); } } else { 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 < -100) { self.destroy(); } }; }); var Player = Container.expand(function () { var self = Container.call(this); var currentSprite = self.attachAsset('player_run1', { anchorX: 0.5, anchorY: 0.5, visible: true, scaleX: 1.5, scaleY: 1.5 }); var nextSprite = self.attachAsset('player_run1', { anchorX: 0.5, anchorY: 0.5, visible: false, scaleX: 1.5, scaleY: 1.5 }); self.currentAsset = 'player_run1'; self.speed = 5 + Math.floor(LK.ticks / 600) * 0.1; // Increase speed over time self.jumpHeight = 25; self.isJumping = false; self.velocityY = 0; self.groundY = 2732 - 400; // 400 pixels from bottom self.y = self.groundY; // Set initial position to ground level self.runFrames = ['player_run1', 'player_run2', 'player_run3']; self.runFrameIndex = 0; self.runFrameCounter = 0; self.runFrameDelay = 15; self.swapSprite = function (newAsset) { if (self.currentAsset !== newAsset) { var nextSprite = LK.getAsset(newAsset, { anchorX: 0.5, anchorY: 0.5 }); nextSprite.visible = true; nextSprite.scaleX = 1.5; nextSprite.scaleY = 1.5; self.addChild(nextSprite); currentSprite.visible = false; currentSprite.destroy(); currentSprite = nextSprite; self.currentAsset = newAsset; } }; self.update = function () { if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.3; if (self.y >= self.groundY - 30) { if (self.y < 0) { // Prevent jump from exceeding the top of the screen self.y = 0; self.velocityY = 0; } if (!self.isDead) { self.y = self.groundY - 30; self.isJumping = false; self.velocityY = 0; self.swapSprite(self.runFrames[self.runFrameIndex]); } } } else { tween(self, { runFrameIndex: (self.runFrameIndex + 1) % self.runFrames.length }, { duration: self.runFrameDelay * 16.67, // Convert frame delay to milliseconds easing: tween.linear, onFinish: function onFinish() { self.swapSprite(self.runFrames[self.runFrameIndex]); } }); } }; self.jump = function () { if (!self.isJumping) { self.isJumping = true; LK.getSound('jump').play(); self.velocityY = -self.jumpHeight; self.swapSprite('player_jump'); self.runFrameCounter = 0; } }; }); /**** * Initialize Game ****/ /**** *-Seperator- ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ LK.playMusic('theme'); 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 / 4; player.y = player.groundY - 30; // Use groundY for initial position and move 30 pixels higher var enemies = []; var enemySpawnInterval = 80; var enemySpawnCounter = 0; var gaugeBar = game.addChild(new GaugeBar()); var coins = []; var coinSpawnInterval = Math.max(50, 150 - Math.floor(LK.ticks / 600)); // Decrease interval over time, minimum 50 var coinSpawnCounter = 0; gaugeBar.x = 2048 / 2; gaugeBar.y = 100; var scoreText = new Text2('0', { size: 100, fill: 0xFFFFFF }); LK.gui.top.addChild(scoreText); scoreText.x = 2048 / 2; scoreText.y = 50; var coinIcon = LK.getAsset('coin', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.75 }); LK.gui.topRight.addChild(coinIcon); coinIcon.x = -150; // Offset from the right edge coinIcon.y = 100; var coinCounterText = new Text2('0', { size: 100, fill: 0xFFFFFF }); LK.gui.topRight.addChild(coinCounterText); coinCounterText.x = -50; // Offset from the right edge coinCounterText.y = 50; game.update = function () { background.update(); background2.update(); player.update(); enemySpawnCounter++; coinSpawnCounter++; if (coinSpawnCounter >= coinSpawnInterval) { var coin = new Coin(); coin.x = 2048 + Math.random() * 200; coin.y = Math.random() * (2732 - 100); // Random height up to the top of the screen coins.push(coin); game.addChild(coin); coinSpawnCounter = 0; } if (enemySpawnCounter >= enemySpawnInterval) { var enemyType = Math.random() < 0.33 ? 'Enemy' : Math.random() < 0.5 ? 'Goomba' : 'FlyKoopa'; var enemy; if (enemyType === 'Enemy') { enemy = new Enemy(); } else if (enemyType === 'Goomba') { enemy = new Goomba(); } else { enemy = new FlyKoopa(); enemy.y = Math.random() * (2732 - 200); // Random height up to the top of the screen } enemy.x = 2048 + Math.random() * 200; if (enemyType === 'FlyKoopa') { enemy.y = Math.random() * (2732 - 200); // Random height up to the top of the screen } else { enemy.y = enemy.groundY; // Use groundY for initial position } enemies.push(enemy); game.addChild(enemy); enemySpawnInterval = Math.max(20, Math.floor(Math.random() * 100) + 60 - Math.floor(LK.ticks / 600)); // Decrease interval over time, minimum 20 enemySpawnCounter = 0; } for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (player.intersects(enemies[j])) { if (!player.isDead && player.isJumping && player.velocityY > 0 && player.y < enemies[j].y) { // Player is falling and lands on enemy enemies[j].velocityY = -10; // Pop up effect LK.getSound('stomp').play(); // Play stomp sound enemies[j].isJumping = true; enemies[j].canHarmPlayer = false; // Enemy can no longer harm player } else if (enemies[j].canHarmPlayer !== false) { if (!player.isDead && enemies[j].canHarmPlayer !== false) { LK.getSound('dead').play(); // Play dead sound player.isDead = true; game.playerDead = true; player.velocityY = -10; // Pop up effect similar to enemy player.isJumping = true; LK.setTimeout(function () { LK.showGameOver(); }, 3000); // Delay game over by 3 seconds } } } else if (player.x > enemies[j].x && !enemies[j].passed) { enemies[j].passed = true; } if (enemies[j].x < -100) { enemies.splice(j, 1); } } for (var k = coins.length - 1; k >= 0; k--) { coins[k].update(); if (player.intersects(coins[k])) { if (!coins[k].collected) { coins[k].collected = true; LK.setScore(LK.getScore() + 5); // Increase score by 5 for collecting a coin scoreText.setText(LK.getScore()); coinCounterText.setText(parseInt(coinCounterText.text) + 1); LK.getSound('Coin').play(); // Play coin sound coins[k].destroy(); coins.splice(k, 1); } } } }; game.down = function (x, y, obj) { var startTime = Date.now(); var interval = LK.setInterval(function () { var elapsed = Date.now() - startTime; var newValue = Math.min(100, elapsed / 10); // Increase gauge value over time gaugeBar.updateGauge(newValue); }, 100); game.up = function () { LK.clearInterval(interval); var jumpHeight = gaugeBar.currentValue; // Adjust gauge value to be more proportional to jump height player.jumpHeight = jumpHeight / 1.5; player.jump(); gaugeBar.updateGauge(0); // Reset gauge after jump }; var jumpHeight = gaugeBar.currentValue; // Scale the gauge value to jump height player.jumpHeight = jumpHeight / 1.5; gaugeBar.updateGauge(0); // Reset gauge after jump };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
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 = 3;
self.update = function () {
if (!game.playerDead) {
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 = 3;
self.update = function () {
if (!game.playerDead) {
self.x -= self.speed;
}
if (self.x <= -2048) {
self.x = 2048;
}
};
});
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.speed = 3;
self.collected = false;
self.y = 2732 - 600; // Position coins higher than the ground
self.update = function () {
if (!game.playerDead) {
self.x -= self.speed;
}
if (self.x < -100) {
self.destroy();
}
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('turtle1', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.speed = 6 + Math.random() * 2;
self.passed = false;
self.isJumping = false;
self.velocityY = 0;
self.groundY = 2732 - 400; // 400 pixels from bottom
self.y = self.groundY; // Set initial position to ground level
self.runFrames = ['turtle1', 'turtle2'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
self.runFrameDelay = 20;
self.swapSprite = function (newAsset) {
var newGraphics = self.attachAsset(newAsset, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
if (enemyGraphics) {
enemyGraphics.destroy();
}
enemyGraphics = newGraphics;
};
self.update = function () {
self.x -= self.speed + Math.floor(LK.ticks / 600) * 0.1; // Increase speed over time
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.5; // Gravity effect
if (self.y > 2732) {
self.destroy();
}
} else {
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 < -100) {
self.destroy();
}
};
});
var FlyKoopa = Container.expand(function () {
var self = Container.call(this);
var flyKoopaGraphics = self.attachAsset('flykoopa1', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.0,
scaleY: 2.0
});
self.speed = 4 + Math.random() * 2;
self.runFrames = ['flykoopa1', 'flykoopa2'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
self.runFrameDelay = 20;
self.swapSprite = function (newAsset) {
var newGraphics = self.attachAsset(newAsset, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
if (flyKoopaGraphics) {
flyKoopaGraphics.destroy();
}
flyKoopaGraphics = 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.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.5; // Gravity effect
if (self.y > 2732) {
self.destroy();
}
} else {
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 < -100) {
self.destroy();
}
};
});
var GaugeBar = Container.expand(function () {
var self = Container.call(this);
// Background of the gauge
var gaugeBackground = self.attachAsset('gauge_background', {
anchorX: 0.5,
// Center the background horizontally
anchorY: 0.5 // Center the background vertically
});
// Fill of the gauge
var gaugeFill = self.attachAsset('gauge_fill', {
anchorX: 0,
// Anchor the fill to the left edge
anchorY: 0.5 // Center the fill vertically
});
self.maxWidth = gaugeFill.width; // Store the maximum width of the fill
self.currentValue = 0; // Initial value
gaugeFill.scaleX = 0; // Start with the gauge empty
// Adjust the fill's position to align with the left edge of the background
gaugeFill.x = -self.maxWidth / 2; // Offset to the left edge of the background
self.updateGauge = function (value) {
self.currentValue = Math.min(100, Math.max(0, value)); // Clamp value between 0 and 100
gaugeFill.scaleX = self.currentValue / 100; // Scale the fill from the left
};
});
var Goomba = Container.expand(function () {
var self = Container.call(this);
var goombaGraphics = self.attachAsset('goomba1', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.speed = 4 + Math.random() * 2;
self.passed = false;
self.isJumping = false;
self.velocityY = 0;
self.groundY = 2732 - 400; // 400 pixels from bottom
self.y = self.groundY; // Set initial position to ground level
self.runFrames = ['goomba1', 'goomba2'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
self.runFrameDelay = 25;
self.swapSprite = function (newAsset) {
var newGraphics = self.attachAsset(newAsset, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
if (goombaGraphics) {
goombaGraphics.destroy();
}
goombaGraphics = newGraphics;
};
self.update = function () {
self.x -= self.speed;
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.5; // Gravity effect
if (self.y > 2732) {
self.destroy();
}
} else {
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 < -100) {
self.destroy();
}
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
var currentSprite = self.attachAsset('player_run1', {
anchorX: 0.5,
anchorY: 0.5,
visible: true,
scaleX: 1.5,
scaleY: 1.5
});
var nextSprite = self.attachAsset('player_run1', {
anchorX: 0.5,
anchorY: 0.5,
visible: false,
scaleX: 1.5,
scaleY: 1.5
});
self.currentAsset = 'player_run1';
self.speed = 5 + Math.floor(LK.ticks / 600) * 0.1; // Increase speed over time
self.jumpHeight = 25;
self.isJumping = false;
self.velocityY = 0;
self.groundY = 2732 - 400; // 400 pixels from bottom
self.y = self.groundY; // Set initial position to ground level
self.runFrames = ['player_run1', 'player_run2', 'player_run3'];
self.runFrameIndex = 0;
self.runFrameCounter = 0;
self.runFrameDelay = 15;
self.swapSprite = function (newAsset) {
if (self.currentAsset !== newAsset) {
var nextSprite = LK.getAsset(newAsset, {
anchorX: 0.5,
anchorY: 0.5
});
nextSprite.visible = true;
nextSprite.scaleX = 1.5;
nextSprite.scaleY = 1.5;
self.addChild(nextSprite);
currentSprite.visible = false;
currentSprite.destroy();
currentSprite = nextSprite;
self.currentAsset = newAsset;
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.3;
if (self.y >= self.groundY - 30) {
if (self.y < 0) {
// Prevent jump from exceeding the top of the screen
self.y = 0;
self.velocityY = 0;
}
if (!self.isDead) {
self.y = self.groundY - 30;
self.isJumping = false;
self.velocityY = 0;
self.swapSprite(self.runFrames[self.runFrameIndex]);
}
}
} else {
tween(self, {
runFrameIndex: (self.runFrameIndex + 1) % self.runFrames.length
}, {
duration: self.runFrameDelay * 16.67,
// Convert frame delay to milliseconds
easing: tween.linear,
onFinish: function onFinish() {
self.swapSprite(self.runFrames[self.runFrameIndex]);
}
});
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
LK.getSound('jump').play();
self.velocityY = -self.jumpHeight;
self.swapSprite('player_jump');
self.runFrameCounter = 0;
}
};
});
/****
* Initialize Game
****/
/****
*-Seperator-
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
LK.playMusic('theme');
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 / 4;
player.y = player.groundY - 30; // Use groundY for initial position and move 30 pixels higher
var enemies = [];
var enemySpawnInterval = 80;
var enemySpawnCounter = 0;
var gaugeBar = game.addChild(new GaugeBar());
var coins = [];
var coinSpawnInterval = Math.max(50, 150 - Math.floor(LK.ticks / 600)); // Decrease interval over time, minimum 50
var coinSpawnCounter = 0;
gaugeBar.x = 2048 / 2;
gaugeBar.y = 100;
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 50;
var coinIcon = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.75,
scaleY: 0.75
});
LK.gui.topRight.addChild(coinIcon);
coinIcon.x = -150; // Offset from the right edge
coinIcon.y = 100;
var coinCounterText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
LK.gui.topRight.addChild(coinCounterText);
coinCounterText.x = -50; // Offset from the right edge
coinCounterText.y = 50;
game.update = function () {
background.update();
background2.update();
player.update();
enemySpawnCounter++;
coinSpawnCounter++;
if (coinSpawnCounter >= coinSpawnInterval) {
var coin = new Coin();
coin.x = 2048 + Math.random() * 200;
coin.y = Math.random() * (2732 - 100); // Random height up to the top of the screen
coins.push(coin);
game.addChild(coin);
coinSpawnCounter = 0;
}
if (enemySpawnCounter >= enemySpawnInterval) {
var enemyType = Math.random() < 0.33 ? 'Enemy' : Math.random() < 0.5 ? 'Goomba' : 'FlyKoopa';
var enemy;
if (enemyType === 'Enemy') {
enemy = new Enemy();
} else if (enemyType === 'Goomba') {
enemy = new Goomba();
} else {
enemy = new FlyKoopa();
enemy.y = Math.random() * (2732 - 200); // Random height up to the top of the screen
}
enemy.x = 2048 + Math.random() * 200;
if (enemyType === 'FlyKoopa') {
enemy.y = Math.random() * (2732 - 200); // Random height up to the top of the screen
} else {
enemy.y = enemy.groundY; // Use groundY for initial position
}
enemies.push(enemy);
game.addChild(enemy);
enemySpawnInterval = Math.max(20, Math.floor(Math.random() * 100) + 60 - Math.floor(LK.ticks / 600)); // Decrease interval over time, minimum 20
enemySpawnCounter = 0;
}
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
if (!player.isDead && player.isJumping && player.velocityY > 0 && player.y < enemies[j].y) {
// Player is falling and lands on enemy
enemies[j].velocityY = -10; // Pop up effect
LK.getSound('stomp').play(); // Play stomp sound
enemies[j].isJumping = true;
enemies[j].canHarmPlayer = false; // Enemy can no longer harm player
} else if (enemies[j].canHarmPlayer !== false) {
if (!player.isDead && enemies[j].canHarmPlayer !== false) {
LK.getSound('dead').play(); // Play dead sound
player.isDead = true;
game.playerDead = true;
player.velocityY = -10; // Pop up effect similar to enemy
player.isJumping = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 3000); // Delay game over by 3 seconds
}
}
} else if (player.x > enemies[j].x && !enemies[j].passed) {
enemies[j].passed = true;
}
if (enemies[j].x < -100) {
enemies.splice(j, 1);
}
}
for (var k = coins.length - 1; k >= 0; k--) {
coins[k].update();
if (player.intersects(coins[k])) {
if (!coins[k].collected) {
coins[k].collected = true;
LK.setScore(LK.getScore() + 5); // Increase score by 5 for collecting a coin
scoreText.setText(LK.getScore());
coinCounterText.setText(parseInt(coinCounterText.text) + 1);
LK.getSound('Coin').play(); // Play coin sound
coins[k].destroy();
coins.splice(k, 1);
}
}
}
};
game.down = function (x, y, obj) {
var startTime = Date.now();
var interval = LK.setInterval(function () {
var elapsed = Date.now() - startTime;
var newValue = Math.min(100, elapsed / 10); // Increase gauge value over time
gaugeBar.updateGauge(newValue);
}, 100);
game.up = function () {
LK.clearInterval(interval);
var jumpHeight = gaugeBar.currentValue; // Adjust gauge value to be more proportional to jump height
player.jumpHeight = jumpHeight / 1.5;
player.jump();
gaugeBar.updateGauge(0); // Reset gauge after jump
};
var jumpHeight = gaugeBar.currentValue; // Scale the gauge value to jump height
player.jumpHeight = jumpHeight / 1.5;
gaugeBar.updateGauge(0); // Reset gauge after jump
};