Javascript 不更新分数属性。(压倒比分)

Javascript 不更新分数属性。(压倒比分),javascript,phaser-framework,Javascript,Phaser Framework,为什么我的分数没有正确更新。它覆盖最后一个,但不更新自身。我在phaser.io上查看了一些教程,在那里的示例中,分数正在以我使用的方式自我更新。也许问题出在代码的其他地方,我找不到它 这是我的密码: var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div"); var spaceField, backgroundSpeed, player, cursors, bullets, bul

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

这是我的密码:

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");
var game=new Phaser.game(1000800,Phaser.CANVAS,“game_div”);
var spaceField,
背景速度,,
玩家,
光标,
子弹,
Bullettime=0,
消防按钮,
子弹,
子弹声,
引擎关闭声音,
敌人,
分数=0,
记分文本,
winText;
var主状态={
预加载:函数(){
//身份证
game.load.image(“starfield”、“images/space.png”);
game.load.image(“player”、“images/playerSmall.png”);
game.load.image(“bullet”、“images/fire.png”);
game.load.image(“敌人”,“图像/敌人.png”);
//音频
game.load.audio(“engineDownSound”、“sounds/engineDown.ogg”);
game.load.audio(“bulletSound”、“sounds/blaster.mp3”);
},
创建:函数(){
//在屏幕上用鼠标单击时全屏显示
game.scale.fullScreenScaleMode=Phaser.ScaleManager.EXACT_FIT;
game.input.onDown.add(goFull,this);
//背景
spaceField=game.add.tileSprite(0,0,1000,800,“starfield”);
背景速度=2;
游戏。物理。挫折世界();
//玩家太空船+添加物理+玩家移动
player=game.add.sprite(game.world.centerX,game.world.centerY+300,“玩家”);
游戏。物理。启用(玩家,移相器。物理。街机);
player.body.CollizeWorldBounds=true;//玩家不能离开空间场-必须在物理之后添加
cursors=game.input.keyboard.CreateCursorWorkeys();
engineDownSound=game.add.audio(“engineDownSound”);
//发射子弹
子弹=game.add.group();
bullets.enableBody=true;
bullets.physicsBodyType=Phaser.Physics.ARCADE;//为项目启用物理
项目符号。创建多个(30,“项目符号”);
子弹.setAll(“锚点.x”,0.5);
子弹.setAll(“anchor.y”,1);
setAll(“outOfBoundsKill”,true);//检查子弹是否在屏幕外,以便我们可以重用它
bullets.setAll(“checkWorldBounds”,true);
fireButton=game.input.keyboard.addKey(Phaser.keyboard.SPACEBAR);
bulletSound=游戏添加音频(“bulletSound”);
//敌人
敌人=game.add.group();
friends.enableBody=true;
敌人.physicsBodyType=Phaser.Physics.ARCADE;
创建敌人();
},
更新:函数(){
//制作滚动背景
spaceField.tilePosition.y+=背景速度;
player.body.velocity.x=0;//每次未按下键时,播放器都不会移动
player.body.velocity.y=0;
//检查按下了哪个键
if(游标向上、向下){
//player.checkWorldBounds=true;
//player.events.onOutOfBounds.add(playerOutOfBoundsTop,this);
player.body.velocity.y=-350;
}
if(游标。向下。isDown){
player.checkWorldBounds=true;
//player.events.onOutOfBounds.add(playerOutOfBoundsBottom,this);
player.body.velocity.y=350;
引擎关闭声音。播放();
}
if(游标。左。isDown){
player.body.velocity.x=-350;
}
if(游标。右。isDown){
player.body.velocity.x=350;
}
如果(fireButton.isDown){
火弹();
}
//碰撞与敌人死亡
游戏。物理。街机。重叠(子弹,敌人,碰撞处理器,空,这个);
//记分栏
scoreText=game.add.text(0750,“分数:0”{font:“40px Phaser.RetroFont.text_SET10”,填充:“gold”});
winText=game.add.text(game.world.centerX-200,game.world.centerY,“你拯救了银河!”,{font:“60px Phaser.RetroFont.text_SET10”,填充:“gold”});
winText.visible=false;
//更新显示的分数
scoreText.text=“分数:”+分数;
如果(分数=4800){
winText.visible=true;
//scoreText.visible=false;
}
}
};
函数fireball(){
如果(game.time.now>bulletsime){
bullet=bullets.getFirstExists(false);
如果(项目符号){
子弹重置(玩家x+28,玩家y);
bullet.bulletAngleOffset=90;
bullet.bulletAngleVariance=30;
子弹体速度y=-400;
Bullettime=game.time.now+200;
播放声音;
}
}
}
/*功能播放器自动停止(播放器){
//再次将宇宙飞船移到屏幕顶部
player.reset(player.x,60);
}*/
函数{
让我们,
Y
敌人;
对于(y=0;y<4;y+=1){
对于(x=0;x<12;x+=1){
敌人=敌人。创建(x*48,y*50,“敌人”);//创建敌人
敌方。锚。设定值(0.5,0.5);
}
}
x=100;
y=50;
//Tween用于在地图上移动敌人
var tween=game.add.tween(敌人).to({
x:200
},2000,Phaser.releasing.Linear.None,true,0,1000,true);
tween.onRepeat.add(下降,这个);
}
函数下降(){
y+=20;
}
函数goFull(){
if(game.scale.isFullScreen){
game.scale.stop全屏();
}否则{
game.scale.startFullScreen(假);
}
}
功能冲突处理程序(子弹,敌人){
子弹。杀死();
敌人。杀死();
//更新命中分数
分数+=100分;
}
//身份证
game.state.add('mainState',mainState);
游戏。状态。开始(“主状态”);

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

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

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

我认为你应该在Createf中移动你的分数文本