为什么我的分数没有正确更新。它会覆盖最后一个,但它不会自行更新。我查看了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");
答案 0 :(得分:1)
我认为您应该在创建功能中移动scoreText,当您更改分数时,只需更新文本:
function collisionHandler(bullet, enemy) {
bullet.kill();
enemy.kill();
// Updating score on hit
score += 100;
scoreText.text = "Score: " + score;
}
答案 1 :(得分:1)
它有相当简单的解决方案。
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");