I need to get the value of inputScore
currently held in the extractVariable
function into the assignValue
function (this function gives the value of inputScore
to an Index.html page). I tried to store inputScore
in a global object to solve scope issues, but inside assignValue
the global.scoreValue
variable is undefined. How can I get inputScore into assignValue please? New to programming - any help appreciated. Thanks.
global = {};
function extractVariable(inputScore) {
global['scoreValue'] = inputScore;
};
function assignValue() {
document.getElementById("inputScore").value = global.scoreValue;
};
Thanks for everyone’s help. I am so close to solving what I need to do. The issue seems to be getting inputScore
into the Index.html
page. I should have posted all of my code in the first place. Apologies. index.html
is a separate file, which has a link to the javascript file (Game.js
). I have tested the link and it is working. When a button is pressed in Game.js
, index.html
loads, reads the assignValue
function in Game.js
, and inserts the players score (inputScore
) into an input value in a form. At the moment all that is being inserted into the form is:
I cannot figure out why it is not working. I have included code from both files below. Any help is once again appreciated.
Game.js code:
function extractVariable(inputScore) {
return inputScore;
};
function assignValue(inputScore) {
document.getElementById("playerScore").value = inputScore;
};
var CrystalRunner = CrystalRunner || {};
CrystalRunner.GameState = {
init: function() {
//...code here
},
create: function() {
//...code here
},
update: function() {
//..code here
//check if the player needs to die
if(this.player.top >= this.game.world.height) {
this.gameOver();
}
},
gameOver: function(){
//..code here
this.updateHighscore();
//..code here
},
updateHighscore: function(){
this.highScore = +localStorage.getItem('highScore');
if(this.highScore < this.myScore){
this.highScore = this.myScore;
this.inputScore = this.highScore;
this.submitScoreButton = this.game.add.sprite(this.game.world.centerX-135, this.game.world.centerY+100, 'submitScoreButton');
this.submitScoreButton.events.onInputUp.add(function() {
window.location.href = "index1.php";
}, this);
extractVariable(this.inputScore);
}
localStorage.setItem('highScore', this.highScore);
},
};
Index.html code:
<?php
require_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Crystal Candy Game Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<link href="css/style.css" rel="stylesheet">
</head>
<body onload="assignValue(extractVariable())" class="bg">
<div id="preloader">
<div id="status"> </div>
</div><!--preloader-->
<div class="wrapper">
<div id="main">
<!-- Form -->
<form id="form-style" method="post" action="crystalhandle.php" autocomplete="off">
<div class="form-group">
<label class="header-text"><span>First Name</span></label>
<input class="form-control" type="text" id="name" name="username" placeholder="Name" title="Please enter your Firstname" required="">
</div>
<div class="form-group">
<label class="header-text"><span>Score</span></label>
<input class="form-control" type="tel" id="playerScore" name="score" value="" readonly>
</div>
<div class="w3ls-btn form-group">
<div class="wthreesubmitaits">
<input type="submit" name="signup" id="reg" class="button" id="next1" value="Send" style="font-family: sans-serif; font-size: 17px; font-weight: bold;"
</div>
</div>
</form>
</div>
</div>
<div id="bodytext"></div>
<script type='text/javascript' src='js/jquery-2.2.3.min.js'></script>
<script type="text/javascript" src="js/phaser.min.js"></script>
<script type="text/javascript" src="js/states/Game.js"></script>
<script>
$(window).on('load', function() {
$('#status').fadeOut();
$('#preloader').delay(350).fadeOut('slow');
$('body').delay(350).css({'overflow':'visible'});
})
</script>
</body>
</html>
I have done the following changes, I think a solution is at hand. In Index.html
I changed the following line by removing inputScore
:
<body onload="assignValue(extractVariable())" class="bg" >
In Game.js
I find that if I hard code a value into the extractVariable
function (see below), the hard coded value is passed into the value
attribute of the <input>
tag in Index.html
which is what I want it to do. However, I still cannot figure out why this only works with a hard coded value?
function extractVariable(inputScore) {
inputScore = 27; //hard coded value works. Why?
return inputScore;
};
function assignValue(inputScore) {
console.log(inputScore); //this console.logs the hard coded value from
//extractVariable like it should do
document.getElementById("playerScore").value = inputScore;
};