I have made a game using phaser and would like my users to be able to fill in their name on a form with one field for 'Name:' and then click a submit button that submits the name they have just entered and their high score.
Here is my code for the javascript page:
Game.Leader = function(game) {
buttonContinue = null;
state = null;
};
Game.Leader.prototype = {
create: function() {
this.showLeader();
},
showLeader: function() {
this.game.add.sprite(0, 0, 'screen-leader');
this.game.add.sprite(640-213-70, 80, 'spread');
this.state = 'Leader';
this.buttonContinue = this.add.button((640-200)/2, 960-103-1, 'button-restartsmall', this.startGame, this);
this.game.add.button(640-213-10, 20, 'twitter', function() {window.open("https://www.xxxxxxxx.com/", "_blank");}, this);
this.game.add.button(640-133-10, 20, 'whatsapp', function() {window.open("https://www.xxxxxxxx.com", "_blank");}, this);
this.game.add.button(640-293-10, 20, 'face', function() {window.open("https://www.xxxxxxxx.com", "_blank");}, this);
this.game.add.button(640-194-265, 700, 'button-leader2', function() {window.open("leader.htm", "_self");}, this);
this.game.add.sprite(640-194-220, 300, 'highscore-text');
highscoreText = this.game.add.text(290, 360, "0", { font: "40px ComicBook", fill: "#FFCC00", align: "right" });
storageAPI.initUnset('highscore',0);
var highscore = storageAPI.get('highscore');
highscoreText.setText(highscore);
},
startGame: function() {
this.game.state.start('Game');
}
};
So their high score is already held in the below variable and is outputted on the page above
var highscore = storageAPI.get('highscore');
and I want to grab this and to submit it with their name in a form by adding something to the above script - but what?
on submitting the form the following function needs to be executed to post it to the app42 leaderboard service - please see my two //comments:
function saveScore(n){
if(n){
var gameName = "xxxxx";
var userName = name; //this is where the name would go from the form
if(userName == ""){
userName = "Guest";
}
var gameScore = x; //what needs to go here to grab the highscore text already available?
var result;
var scoreBoardService = new App42ScoreBoard()
scoreBoardService.saveUserScore(gameName,userName,gameScore,{ success: function(object){} });
}
}
So questions:
- What do I need to add the first script that will add the form purely in javascript?
- What do I need to change where my two //comments are in the second script for it to post the data?
Many thanks for any help!