So, I want to make a game where you need to take coins and be bigger, but when you take 4 coins I need to reset the player scale (Reset Player Scale is not a problem) and I need to switch the player image, the sprite for another, like a pass Level system. (and that is the problem) I just do it with JavaScript and I'm using kaboom.js and that my script:
import kaboom from "kaboom"
// initialize context
kaboom()
const speed = 150;
let PlayerScale = 1;
let limitcoins = 0;
// load assets
loadSprite("player", "sprites/bean.png")
loadSprite("coin", "sprites/coin.png");
loadSprite("player2", "sprites/bean2.png");
// Adicionar um sprite chamado "player"
const player = add([
sprite("player"),
pos(width() / 2, height() / 2),
scale(PlayerScale),
area(),
"player" //tag do player pra deteção de colisão
]);
// Adicionar um sprite chamado "player2" com a tag "player2" e escala inicial de 0
const player2 = add([
sprite("player2"),
pos(width() / 2, height() / 2),
scale(0),
area(),
"player2"
]);
// Function to spawn a new coin at a random position
function spawnCoin() {
const spawnDistance = 400;
const spawnX = player.pos.x + rand(-spawnDistance, spawnDistance);
const spawnY = player.pos.y + rand(-spawnDistance, spawnDistance)
const coin = add([
sprite("coin"),
pos(spawnX, spawnY),
scale(0.5), // scale the sprite down by half
area(),
]);
// Detect collision with player
coin.collides("player", (player) => {
if (!coin.collided) {
coin.collided = true; // desativa a detecção de colisão
destroy(coin);
PlayerScale += 1; // increase player scale
player.scale = PlayerScale; // update player scale
// Definir a posição da câmera com base na posição do jogador
player.action(() => {
camPos(player.pos);
});
// Spawn the initial coin
setInterval(() => {
spawnCoin();
limitcoins += 1;
}, 1000);
// Mover o sprite com as teclas W, A, S e D
keyDown("w", () => {
player.move(0, -speed);
});
keyDown("a", () => {
player.move(-speed, 0);
});
keyDown("s", () => {
player.move(0, speed);
});
keyDown("d", () => {
player.move(speed, 0);
});
Sorry I know my english sucks
I don't know how to make it but I tried that:
// Trocar o sprite do jogador se playerScale for igual a 4
if (playerScale === 4) {
player.swap("player2");
}
}
});
}
I don't know if that player.swap script is good, so if it sucks will be better a script made from scratch.