Javascript 游戏,模块 raphael 未定义
我在这里开发一个游戏引擎: http://synodins.com/apps/tank_fight 但现在,由于某种原因,它不再运行了。 我无法启动 Raphael 模块,当我使用 mozilla Web 调试器运行该站点时,我得到“Raphael 未定义”。 但这没有任何意义,因为它在 raphael.js 模块中定义得非常清楚。
另外,有时候游戏运行得很好,比如我每 20 次尝试一次。 并且没有明确的模式来说明其工作原理。
我在尝试启动 raphael 之前打印出 Hello world 1,尝试后打印出 hello world 2。
有人能看出问题是什么吗?
I am developing a game engine here:
http://synodins.com/apps/tank_fight
But now, for some reason, it doesn't run anymore.
I cannot initiate the Raphael module, when i run the site with mozilla web debugger, i get "Raphael is not defined".
But that doesn't make any sense because it's very clearly defined in the raphael.js module.
Also, occationally the game runs fine, like, every 20th time i try.
And there's no clear pattern to what makes it work.
I print out Hello world 1 before trying to initiate raphael, and hello world 2 after trying.
Can anybody see what the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
load_script
调用块异步加载脚本。这就是为什么似乎没有明确的模式。无法保证在调用以下行时加载 raphael.js(因此定义了 Raphael)
。
您应该等到整个文档准备就绪后再尝试使用外部脚本中定义的变量。一般情况下,这是文档的
onload
事件。像 jquery 这样的各种库也提供了自己的“就绪”事件,如果您使用它们,您可以绑定它们。是否有任何理由以您的方式加载脚本,而不是简单地使用标记,如下所示?
The block of
load_script
calls loads the scripts asynchronously. This is why there appears to be no clear pattern.There is no guarantee that raphael.js is loaded (and therefore, that
Raphael
is defined) by the time that the line:is called.
You should wait until the whole document is ready before attempting to use variables defined in external scripts. Under normal circumstances, this is the
onload
event of the document. Various libraries like jquery also offer their own "ready" event, that you could tie to if you use them.Is there any reason why you load the scripts in the way you do, rather than simply using markup, as below?
您正在按需加载脚本 - 这将加快您的初始页面加载速度。但之后您尝试在 raphael.js 中执行代码 - 但加载 js 文件需要一些时间,并且您必须确保它们已加载,然后才能开始调用其中的功能。
您可以查看 Modernizer.load() - 这可以帮助您同步资源加载并在加载完成时添加回调: http://www.modernizr.com/docs/#load
You are loading your scripts on demand - and this will speed up your initial page load. But right after that you try execute code in raphael.js - but loading of js files take some time and you have to be sure that they're loaded before you start calling functionality in them.
You may have a look at Modernizer.load() - this helps you to synchronize loading of resources and add callbacks when loading completes: http://www.modernizr.com/docs/#load
您是否尝试过将“raphael.js”移至其他内容之前? Raphael 可能没有在上面的脚本需要时定义。
Have you tried moving 'raphael.js' to be included before anything else? It may be that Raphael is not defined at the point a script above it needs it.