javascript - 不更新分数属性。 (压倒性的分数)

标签 javascript phaser-framework

为什么我的分数没有正确更新。它会覆盖最后一个,但不会自行更新。我在 Phaser.io 上查看了一些教程,在其中的示例中,分数正在按照我使用的方式自行更新 scoreUpdateLink 。也许问题出在代码的其他地方,而我找不到它。

这是我的代码:

var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div");

var spaceField,
    backgroundSpeed,
    player,
    cursors,
    bullets,
    bulletsTime = 0,
    fireButton,
    bullet,
    bulletSound,
    engineDownSound,
    enemies,
    score = 0,
    scoreText,
    winText;

var mainState = {
    preload: function () {
        //id
        game.load.image("starfield", "images/space.png");
        game.load.image("player", "images/playerSmall.png");
        game.load.image("bullet", "images/fire.png");
        game.load.image("enemy", "images/enemyShips.png");

        // audio
        game.load.audio("engineDownSound", "sounds/engineDown.ogg");
        game.load.audio("bulletSound", "sounds/blaster.mp3");
    },

    create: function () {

        // Full screen when clicking with the mouse on the screen
        game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
        game.input.onDown.add(goFull, this);
        // background
        spaceField = game.add.tileSprite(0, 0, 1000, 800, "starfield");
        backgroundSpeed = 2;
        game.physics.setBoundsToWorld();

        // player spaceship + adding physics + player movement
        player = game.add.sprite(game.world.centerX, game.world.centerY + 300, "player");
        game.physics.enable(player, Phaser.Physics.ARCADE);
        player.body.collideWorldBounds = true; // Player cannot leave the spacefield - must be added after physics
        cursors = game.input.keyboard.createCursorKeys();
        engineDownSound = game.add.audio("engineDownSound");

        // Fire bullets
        bullets = game.add.group();
        bullets.enableBody = true;
        bullets.physicsBodyType = Phaser.Physics.ARCADE; // Enabling physics for bullets
        bullets.createMultiple(30, "bullet");
        bullets.setAll("anchor.x", 0.5);
        bullets.setAll("anchor.y", 1);
        bullets.setAll("outOfBoundsKill", true); // Checks if the bullet is off screen so we can reuse it
        bullets.setAll("checkWorldBounds", true);

        fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        bulletSound = game.add.audio("bulletSound");


        // Enemies

        enemies = game.add.group();
        enemies.enableBody = true;
        enemies.physicsBodyType = Phaser.Physics.ARCADE;
        createEnemies();
    },

    update: function () {
        // Making scrolling background
        spaceField.tilePosition.y += backgroundSpeed;
        player.body.velocity.x = 0; // Everytime when key is not pressed the player does not move
        player.body.velocity.y = 0;

        // Checking which key is pressed

        if (cursors.up.isDown) {
            //player.checkWorldBounds = true;
            //player.events.onOutOfBounds.add(playerOutOfBoundsTop, this);
            player.body.velocity.y = -350;
        }

        if (cursors.down.isDown) {
            player.checkWorldBounds = true;
            // player.events.onOutOfBounds.add(playerOutOfBoundsBottom, this);
            player.body.velocity.y = 350;
            engineDownSound.play();

        }

        if (cursors.left.isDown) {
            player.body.velocity.x = -350;
        }

        if (cursors.right.isDown) {
            player.body.velocity.x = 350;
        }

        if (fireButton.isDown) {
            fireBullet();
        }

        // Collision and enemy death
        game.physics.arcade.overlap(bullets, enemies, collisionHandler, null, this);

        // Score bar
        scoreText = game.add.text(0, 750, "Score: 0", {font: "40px Phaser.RetroFont.TEXT_SET10", fill: "gold"});
        winText = game.add.text(game.world.centerX - 200, game.world.centerY, "You saved the Galaxy!", {font: "60px Phaser.RetroFont.TEXT_SET10", fill: "gold"});
        winText.visible = false;

        // updating score on display
        scoreText.text = "Score: " + score;

        if(score === 4800) {
            winText.visible = true;
            //scoreText.visible = false;
        }
    }
};

function fireBullet() {
    if (game.time.now > bulletsTime) {
        bullet = bullets.getFirstExists(false);

        if (bullet) {
            bullet.reset(player.x + 28, player.y);
            bullet.bulletAngleOffset = 90;
            bullet.bulletAngleVariance = 30;
            bullet.body.velocity.y = -400;
            bulletsTime = game.time.now + 200;
            bulletSound.play();
        }
    }
}

/*function playerOutOfBoundsTop(player) {

 //  Move the Spaceship to the top of the screen again
 player.reset(player.x, 60);

 }*/

function createEnemies() {
    let x,
        y,
        enemy;

    for (y = 0; y < 4; y += 1) {
        for (x = 0; x < 12; x += 1) {
            enemy = enemies.create(x * 48, y * 50, "enemy"); // Creates the enemies
            enemy.anchor.setTo(0.5, 0.5);
        }
    }

    enemies.x = 100;
    enemies.y = 50;

    // Tween is used to move enemies across the map
    var tween = game.add.tween(enemies).to({
        x: 200
    }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);

    tween.onRepeat.add(descend, this);
}

function descend() {
    enemies.y += 20;
}
function goFull() {

    if (game.scale.isFullScreen) {
        game.scale.stopFullScreen();
    } else {
        game.scale.startFullScreen(false);
    }
}

function collisionHandler(bullet, enemy) {
    bullet.kill();
    enemy.kill();

    // Updating score on hit
    score += 100;
}
//id
game.state.add('mainState', mainState);

game.state.start("mainState");

最佳答案

我认为您应该在创建函数中移动您的分数文本,当您更改分数时,只需更新文本即可:

function collisionHandler(bullet, enemy) {
    bullet.kill();
    enemy.kill();

    // Updating score on hit
    score += 100;
    scoreText.text = "Score: " + score;
}

关于javascript - 不更新分数属性。 (压倒性的分数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528409/

相关文章:

javascript - 如何让我的 Phaser 游戏射出不止一支箭?

javascript - Phaser——如何检查 Sprite 是否在移动?

javascript - 移相器。沿着固定路径在二维网格上移动游戏角色

javascript - Phaser 复杂碰撞

javascript - 将鼠标悬停在导航栏中的单个按钮上

javascript - 理解 modal.js 中的复杂 if 条件

javascript - 如何将占位符拖到 PDF 上

javascript - Select2 with Backbone.Marionette : Pre-selected values not hidden

javascript - 如何使用 PhaserJS 添加基本的世界碰撞?

javascript - Gulp同步任务问题