我使用此代码加载脚本:
if(!checkAlreadyLoaded || !Engine.Loader.isLoaded(file))
{
var newScript = document.createElement("script");
newScript.onload = function()
{
if(onLoad != undefined && onLoad != null)
onLoad();
};
newScript.type = "text/javascript"
newScript.src = file;
if(newScript != undefined)
document.getElementsByTagName("head")[0].appendChild(newScript);
else
alert("Failed to load!");
}
并且它可以完美地加载脚本,但是当我尝试访问任何脚本时,脚本是未定义的。但是,如果我将它们添加到html页面的头部,我将添加加载程序,它可以正常工作。
<head>
<script src="scripts/Engine/Loader.js" type="text/javascript"> </script>
</head>
以下是发生问题的代码:
loadScript(file)
{
if(!checkAlreadyLoaded || !Engine.Loader.isLoaded(file))
{
var newScript = document.createElement("script");
newScript.onload = function()
{
if(onLoad != undefined && onLoad != null)
onLoad();
};
newScript.type = "text/javascript"
newScript.src = file;
if(newScript != undefined)
document.getElementsByTagName("head")[0].appendChild(newScript);
else
alert("Failed to load!");
}
}
loadEngine: function()
{
var engineScripts =
[
"scripts/Engine/GameLoop.js",
"scripts/Engine/Utilities.js",
"scripts/Engine/Client.js",
"scripts/Engine/Renderer.js",
"scripts/Engine/Input.js",
];
for(var i = 0; i < engineScripts.length; i++)
Engine.Loader.loadScript(engineScripts[i]);
GameLoop gameLoop = new GameLoop(new Client("ws:/192.168.105:8080"), new Renderer());
gameLoop.initialise();
}
我做错了什么? 谢谢!