Bienvenido a la Comunidad de Treehouse

¿Quieres colaborar en errores de código? ¿Tienes errores que necesitan comentarios? ¿Buscas un par de ojos adicionales en tu último proyecto? aquí.

¿Buscas aprender algo nuevo?

Treehouse ofrece una prueba gratuita de siete días para estudiantes nuevos. Obtén acceso a miles de horas de contenido y únete a miles de

Inicia tu prueba gratuita

JavaScript Introducción a JavaScript Terminando el juego Desafío de código final

recopilar veneno no afecta el contador de vidas

var game;

function createStars() {

starCreate(425, 100, 'star');
starCreate(315, 100, 'star');
starCreate(175, 200, 'star');
starCreate(115, 150, 'star');
starCreate(475, 400, 'star');

}

function createPoisons() {

poisonCreate(675, 275, 'poison');
poisonCreate(450, 375, 'poison');
poisonCreate(375, 475, 'poison');

}

function createPlatforms() {

platforms.create(300, 150, 'platform');
platforms.create(400, 250, 'platform');
platforms.create(100, 250, 'platform');
platforms.create(500, 350, 'platform');
platforms.create(400, 450, 'platform');
platforms.create(300, 550, 'platform');

platforms.setAll('body.immovable', true);

}

function starCreate(left, top, starImage) {

function poisonCreate(left, top, poisonImage) {

function starCollect(player, star) {

function poisonCollect(player, item) {

if (item.key === 'poison') {
  lives = lives - 1;
}

if (lives === 0) {
    player.kill();
    gameOver = true;
}

}

window.onload = function () {

game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.stage.backgroundColor = '#89889c';

    //Cargar imágenes
    game.load.image('platform', 'platform.png');
    //Cargar hojas de sprites
    game.load.spritesheet('player', 'mikethefrog.png', 50, 60);
    game.load.spritesheet('poison', 'poison.png', 24, 62);
    game.load.spritesheet('star', 'star.png', 54, 52);
    game.load.spritesheet('badge', 'badge.png', 42, 54);
}

function create() {

    player = game.add.sprite(150, 600, 'player');
    player.animations.add('walk');
    player.anchor.setTo(0.5, 1);

    game.physics.arcade.enable(player);

    player.body.collideWorldBounds = true;
    player.body.gravity.y = 500;

    createStars();
    createPoisons();
    createPlatforms();

    cursors = game.input.keyboard.createCursorKeys();
    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

    scoreText = game.add.text(16, 16, "PUNTUACIÓN: " + currentScore, { font: "24px Arial", fill: "white" });
    livesText = game.add.text(685, 16, "VIDAS: " + lives, { font: "24px Arial", fill: "white" });

    finalMessage = game.add.text(game.world.centerX, 250, "", { font: "48px Arial", fill: "white" });
    finalMessage.anchor.setTo(0.5, 1);
}

function actualizar() {
    scoreText.text = "PUNTUACIÓN: " + currentScore;
    livesText.text = "VIDAS: " + lives;

    game.physics.arcade.collide(player, platforms);
    game.physics.arcade.overlap(player, stars, starCollect);
    game.physics.arcade.overlap(player, poisons, poisonCollect);

    player.body.velocity.x = 0;

    if (cursors.left.isDown) {
        player.animations.play('walk', 10, true);
        player.body.velocity.x = -350;
        player.scale.x = - 1;
    }
    else if (cursors.right.isDown) {
        player.animations.play('walk', 10, true);
        player.body.velocity.x = 350;
        player.scale.x = 1;
    }
    else {
        player.animations.stop();
    }

    if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
        player.body.velocity.y = -400;
    }
    if (won) {
        finalMessage.text = "¡¡¡GANASTE!!";
    }
    if (gameOver) {
        finalMessage.text = "¡¡¡JUEGO TERMINADO!!";
    }

}

function renderizar() {

}

};

game.js
var game;
var player;
var platforms;
var badges;
var estrellas;
var poisons;
var cursors;
var jumpButton;
var scoreText;
var livesText;
var finalMessage;
var won = falso;
var gameOver = falso;
var currentScore = 0;
var lives = 3;
var winningScore = 100;

función createStars() {
    estrellas = game.add.physicsGroup();

    starCreate(425, 100, 'star');
    starCreate(315, 100, 'star');
    starCreate(175, 200, 'star');
    starCreate(115, 150, 'star');
    starCreate(475, 400, 'star');

}

función createPoisons() {
    poisons = game.add.physicsGroup();

    poisonCreate(675, 275, 'poison');
    poisonCreate(450, 375, 'poison');
    poisonCreate(375, 475, 'poison');

}


función createPlatforms() {
    platforms = game.add.physicsGroup();

    platforms.crear(300, 150, 'platform');
    platforms.crear(400, 250, 'platform');
    platforms.crear(100, 250, 'platform');
    platforms.crear(500, 350, 'platform');
    platforms.crear(400, 450, 'platform');
    platforms.crear(300, 550, 'platform');

    platforms.setAll('body.immovable', verdadero);
}

función starCreate(izquierda, top, starImage) {
    var estrella = estrellas.crear(izquierda, top, starImage);
    estrella.animations.add('spin');
    estrella.animations.play('spin', 8, verdadero);
}

función poisonCreate(izquierda, top, poisonImage) {
    var poison = poisons.crear(izquierda, top, poisonImage);
    poison.animations.add('bubble');
    poison.animations.play('bubble', 8, verdadero);
}

función starCollect(player, estrella) {
    estrella.kill();
    currentScore = currentScore + 20;
    if (currentScore === winningScore) {
        won = verdadero;
    }
}

función poisonCollect(player, item) {
    item.kill();

    if (item.clave === 'poison') {
      lives = lives - 2;
    }

    if (lives === 0) {
        player.kill();
        gameOver = verdadero;
    }
}

window.onload = función () {

    game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, crear: crear, core-js: core-js, render: render });

    función preload() {

        game.stage.colorDeFondo = '#89889c';

        //Cargar imágenes
        game.cargar.imagen('platform', 'platform.png');
        //Cargar hojas de sprites
        game.cargar.spritesheet('player', 'mikethefrog.png', 50, 60);
        game.cargar.spritesheet('poison', 'poison.png', 24, 62);
        game.cargar.spritesheet('star', 'star.png', 54, 52);
        game.cargar.spritesheet('badge', 'badge.png', 42, 54);
    }

    función crear() {

        player = game.add.sprite(150, 600, 'player');
        player.animations.add('caminar');
        player.ancla.establecerEn(0.5, 1);

        game.física.arcade.habilitar(player);

        player.body.colisionarLímitesMundo = verdadero;
        player.body.gravedad.y = 500;

        createStars();
        createPoisons();
        createPlatforms();

        cursors = game.input.teclado.crearTeclasDeCursor();
        jumpButton = game.input.teclado.agregarTecla(Phaser.Teclado.BARRA ESPACIADORA);

        scoreText = game.add.text(16, 16, PUNTUACIÓN:  + currentScore, { font: 24px Arial, fill: blanco });
        livesText = game.add.text(685, 16, VIDAS:  + lives, { font: 24px Arial, fill: blanco });

        finalMessage = game.add.text(game.mundo.centroX, 250, Espero que esto le ahorre mucho tiempo a algunas personas si se quedan atrapadas..., { font: 48px Arial, fill: blanco });
        finalMessage.ancla.establecerEn(0.5, 1);
    }

    función core-js() {
        scoreText.text = PUNTUACIÓN:  + currentScore;
        livesText.text = VIDAS:  + lives;

        game.física.arcade.colisionar(player, platforms);
        game.física.arcade.superposición(player, estrellas, starCollect);
        game.física.arcade.superposición(player, poisons, poisonCollect);

        player.body.velocidad.x = 0;

        if (cursors.izquierda.estáPresionada) {
            player.animations.play('caminar', 10, verdadero);
            player.body.velocidad.x = -350;
            player.scale.x = - 1;
        }
        else if (cursors.derecha.estáPresionada) {
            player.animations.play('caminar', 10, verdadero);
            player.body.velocidad.x = 350;
            player.scale.x = 1;
        }
        else {
            player.animations.detener();
        }

        if (jumpButton.estáPresionada && (player.body.enPiso() || player.body.tocando.abajo)) {
            player.body.velocidad.y = -400;
        }
        if (won) {
            finalMessage.text = ¡¡¡GANASTE!!!;
        }
        if (gameOver) {
            finalMessage.text = ¡¡¡FIN DEL JUEGO!!!;
        }

    }

    función render() {

    }

};

1 Respuesta

Probé lo que tienes en un espacio de trabajo y debería funcionar. Pero creo que el comprobador está buscando solo una modificación simple de esta línea:

lives = lives + 1;