如何使用ajax将this.score
传输到文本文件,以便在玩游戏时有一个高分列表?
以及带有"您的分数已成功保存"的警报。我对ajax了解不多,所以一些细节和解释会非常有帮助:)
var game = new Phaser.Game(1520, 740);
this.score = 0;
this.labelScore = game.add.text(20, 20, "0", { font: "30px Arial",
fill: "#ffffff" });
},
update: function() {
game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe,
null, this);
if (this.bird.y < 0 || this.bird.y > game.world.height)
this.restartGame();
},
game.time.events.remove(this.timer);
this.pipes.forEach(function(p){
p.body.velocity.x = 0;
}, this);
},
restartGame: function() {
game.state.start('main');
},
addOnePipe: function(x, y) {
var pipe = game.add.sprite(x, y, 'pipe');
this.pipes.add(pipe);
game.physics.arcade.enable(pipe);
pipe.body.velocity.x = -500;
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
},
addRowOfPipes: function() {
var hole = Math.floor(Math.random()*5)+1;
for (var i = 0; i < 12; i++)
if (i != hole && i != hole +1)
this.addOnePipe(1520, i*60+10);
this.score += 1;
this.labelScore.text = this.score;
},
};
game.state.add('main', mainState);
game.state.start('main');
你不需要 ajax 来编写文件 ajax 用于将数据从一个系统传输到另一个系统:
假设您想在系统中写入文本文件
const fs = require('fs');
gameOverFunction(this.score){
var logStream = fs.createWriteStream('/some/pathtofile/a.txt', {'flags': 'w'});
logStream.write(this.score);
}