2016-07-22 17 views
0

Warum mein Punktestand nicht richtig aktualisiert wird. Es überschreibt das letzte aber es aktualisiert sich nicht. Ich habe einige der Tutorials bei phaser.io angeschaut und in den Beispielen dort aktualisiert sich der Score selbst so wie ich scoreUpdateLink verwendet habe. Vielleicht ist das Problem woanders im Code und ich kann es nicht finden.Not score propery wird nicht aktualisiert. (Überschreibender Punktestand)

Hier ist mein Code:

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"); 

Antwort

1

Ich glaube, Sie Ihre scoreText bewegen sollte in Funktion erstellen und, wenn Sie Ihre Gäste ändern, aktualisieren Sie einfach den Text:

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

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

in der Tat sehr dumme Fehler. Danke für die schnelle Antwort – tabula

1

Es hat ziemlich einfache Lösung .

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(); 
 

 
     // 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; 
 

 
    }, 
 

 
    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); 
 

 
     
 

 
     // 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");