0

How do I transfer this.score to a text file using ajax so when you game over there is a list of highscores?

And a alert with "your score has been saved succesfully". I don't really know alot about ajax so a little detail and explaination would be very helpfull :)

  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');  
Pooja Gupta
  • 785
  • 10
  • 22
Maurice
  • 9
  • 3
  • AJAX is merely a method of sending/receiving data. If you want to write to a text file you'd need some server side logic. As you've tagged PHP, you can [do this](https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php). Also, it should be mentioned that using text files to store information is just about the worst method, for a variety of reasons. mySQL would be a much better choice. – Rory McCrossan Apr 09 '18 at 09:24

1 Answers1

1

You don't need ajax to write a file ajax is used for transferring data from one system to another:

Assuming you want to write a text file in your system

 const fs = require('fs');
 gameOverFunction(this.score){
   var logStream = fs.createWriteStream('/some/pathtofile/a.txt', {'flags': 'w'});
   logStream.write(this.score);
 }
ashwintastic
  • 2,262
  • 3
  • 22
  • 49