我使用Phaser框架构建了一个游戏。游戏结束时,玩家的高分记录在变量(this.inputScore)中。要将他们的高分提交到服务器,玩家单击一个按钮,将其带到Index.html页面,在该页面中将填写表单。为避免作弊,我希望从游戏的JavaScript文件中传递高分变量(Game.js),将表单上的输入值作为只读值。我已经在Game.js文件中创建了一个函数(assignValue)来执行此任务,但是由于范围的原因,我无法让Index.html文件读取该函数。当我将函数放置在Game.js脚本的顶部(全局)时,Index.html读取该函数并相应地执行该函数(但出于测试目的使用硬编码值。)如何获取Index.html文件读取我的assignValue()函数并将this.inputScore传递给Index.html中的输入值?
Game.js文件:
//INDEX.HTML READS THIS assignValue function AND APPLIES IT BECAUSE IT
//IS IN GLOBAL SCOPE. BUT I NEED THE VALUE TO BE A VARIABLE TAKEN FROM A
//FUNCTION INSIDE THE GAME CODE. SEE GAME CODE BELOW:
function assignValue() {
document.getElementById("inputScore").value = 127;
};
//GAME CODE
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.counter === 0 || this.player.left <= 0 && this.counter === 0) {
this.gameOver();
}
},
gameOver: function(){
this.game.time.events.stop();
this.player.body.enable = false;
//..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.congrats = this.game.add.sprite(this.game.world.centerX-193, this.game.world.centerY-20, 'congrats');
this.congrats.inputEnabled = true;
this.congrats.fixedToCamera = true;
this.submitScoreButton = this.game.add.sprite(this.game.world.centerX-135, this.game.world.centerY+100, 'submitScoreButton');
this.submitScoreButton.inputEnabled = true;
this.submitScoreButton.fixedToCamera = true;
this.submitScoreButton.events.onInputUp.add(function() {
window.location.href = "index1.php";
}, this);
}
localStorage.setItem('highScore', this.highScore);
},
//THE FUNCTION BELOW IS NOT BEING READ BECAUSE OF SCOPE. HOW TO I MAKE INDEX.HTML READ IT?
assignValue: function() {
document.getElementById("inputScore").value = this.inputScore;
},
};
Index1.html文件:
<?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">
<script type='text/javascript' src='js/jquery-2.2.3.min.js'></script>
<script>
$(window).on('load', function() {
$('#status').fadeOut();
$('#preloader').delay(350).fadeOut('slow');
$('body').delay(350).css({'overflow':'visible'});
})
</script>
</head>
<body onload="assignValue()" class="bg">
<div id="preloader">
<div id="status"> </div>
</div>
<div class="wrapper">
<div class="texte">
<header>
<h1>SUBMIT YOUR SCORE</h1>
<img id="logo" src="assets/images/logo.png">
</header>
<p>Submit your highscore and you could stand a chance to win a
prize!</p>
</div>
<div id="main">
<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>Surname</span></label>
<input class="form-control" type="text" id="name" name="surname"
placeholder="Surname" title="Please enter your Lastname"
required="">
</div>
<div class="form-group">
<label class="header-text"><span>Email</span></label>
<input class="form-control" type="email" id="email" name="email"
placeholder="Mail@example.com" title="Please enter a Valid Email
Address" required="">
</div>
<div class="form-group">
<label class="header-text"><span>Phone</span></label>
<input class="form-control" type="tel" id="name" name="phone"
placeholder="Phone" title="Please enter your Phone No" required="">
</div>
<div class="form-group">
<label class="header-text"><span>Score</span></label>
<input class="form-control" type="tel" id="inputScore" name="score"
value="" readonly>
</div>
<!-- I need the above input value to have the variable from
assignValue inserted into it-->
<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>
<script type="text/javascript" src="js/phaser.min.js"></script>
<!--the below js file is where assignValue() is defined:-->
<script type="text/javascript" src="js/states/Game.js"/></script>
</body>
</html>