簡體   English   中英

如何將this.score傳輸到高分列表的文本文件? (AJAX)

[英]How do I transfer this.score to a text file for a high score list? (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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM