javascript - 如何将 this.score 传输到文本文件以获得高分列表? ( Ajax )

标签 javascript php jquery ajax

如何使用 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);
 }

关于javascript - 如何将 this.score 传输到文本文件以获得高分列表? ( Ajax ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729579/

相关文章:

javascript - 更改图像顺序并更新数据库

javascript - 希望 div 在加载上面的图像后立即加载

javascript - 如何在javascript中声明没有空格的var

javascript - 替换响应表以获得更好的标记和可访问性

javascript - 无法通过react-two-renderer加载.mtl和.obj

javascript - 如何在页面加载后使用 JS 执行繁重的 PHP 函数以加快速度?

php - 如何在 mysql 插入上解析 "Illegal double ... value found during parsing"?

php - 动态表单(切换实体)symfony2

jquery - 按预定义顺序初始化 'jQuery UI Sortable List'

javascript - Bootstrap 3 : delete confirm dialog box not work