- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在识别某些变量之前已定义的函数时遇到了一些问题;他们应该是全局性的,但他们似乎并没有这样做。
var global = "Summat.";
function Function() {
alert(global); //alerts "undefined"
//some more code referencing similar variables
alert("Hey."); //doesn't display.
}
我不确定问题实际上是我认为全局变量的行为与全局变量不同,但我已通过有限的故障排除能力将其缩小到这个范围。如果有帮助,我也非常乐意发布并尝试解释完整/更多代码。
这是更扩展的版本:
<!DOCTYPE html>
<html>
<head>
<title>divMan: Canvas</title>
</head>
<body>
<canvas id="canvas" height="768px" width="1366px" style="position:fixed;top:0px;left:0px;"></canvas>
</body>
<script>
window.onload = drawFrame();
//----------------------------------------Global Variables-----------------------------//
var context = document.getElementById("canvas").getContext("2d");
//------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------Constructors-----------------------------------------------------------------------------------------------//
function Point(top,left) {this.top = top; this.left = left;}
function Stone(top,left,height,width) {//a seemingly functional constructor}
function Tree(top,left,trunkHeight,trunkWidth,crownHeight,crownWidth) {//a seemingly functional constructor}
function DivMan(top,left,headHeight,headWidth,bodyHeight,bodyWidth) {//a seemingly functional constructor}
function Sky(top,left,height,width) {//a seemingly functional constructor}
function Ground() {//a seemingly functional constructor}
function Grass(height,targetGround) {//a seemingly functional constructor}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------Objects------------------------------------------------//
var sky = new Sky(0,0,538,1366); //the sky.
var ground = new Ground(); //the ground.
var grass = new Grass(38,ground); //the grass.
var stone = new Stone(418,228,75,75); //a stone.
var pedestal = new Stone(508,630,30,200); //a stone pedestal.
var tree0 = new Tree(138,1000,150,60,250,300); //a tree.
var tree1 = new Tree(83,73,300,40,100,300); //another tree.
var divMan = new DivMan(408,700,34,30,65,60); //divMan!!
//----------------------------------------------------------------------------------------------------//
function drawFrame() {
sky.Draw();
ground.Draw();
grass.Draw();
tree1.Draw();
stone.Draw();
pedestal.Draw();
divMan.Draw();
tree0.Draw();
}
这是现在完整的样子:
<!DOCTYPE html>
<html>
<head>
<title>divMan: Canvas</title>
</head>
<style>
* {-ms-touch-action:none;}
</style>
<body>
<canvas id="canvas" height="768px" width="1366px" style="position:fixed;top:0px;left:0px;"></canvas>
</body>
<script>
window.onload = drawFrame();
//window.onclick = walk(event);
//----------------------------------------Global Variables-----------------------------//
var context = document.getElementById("canvas").getContext("2d");
//var _walk = false;
/*var staticEquilibrium = true;
//--Kinematic Variables--//
var vSubX = 0;
var aSubX = 0;
var jSubX = 0;
var vSubY = 0;
var aSubY = 0;
var jSubY = 0;*/
// var deltaT = .001; //standard time interval.
// var interval; //a timing loop variable.
//------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------Constructors-----------------------------------------------------------------------------------------------//
function Point(top,left) {this.top = top; this.left = left;} //Parameters are numbers, to be used for pixel values.
function Stone(top,left,height,width) { //stones.
this.origin = new Point(top,left); //make sure to give this a curved top, eventually.
this.height = height;
this.width = width;
this.color = "#a0a0a0";
this.Draw = drawStone;
function drawStone() {
context.fillStyle = this.color;
context.fillRect(this.origin.left,this.origin.top,this.width,this.height);
}
}
function Tree(top,left,trunkHeight,trunkWidth,crownHeight,crownWidth) { //trees.
this.origin = new Point(top,left);
this.trunkHeight = trunkHeight;
this.trunkWidth = trunkWidth;
this.trunkColor = "#702000";
this.crownHeight = crownHeight;
this.crownWidth = crownWidth;
this.crownColor = "#40d030";
this.Draw = drawTree;
function drawTree() {
context.fillStyle = this.crownColor;
context.fillRect(this.origin.left,this.origin.top,this.crownWidth,this.crownHeight);
context.fillStyle = this.trunkColor;
context.fillRect(this.origin.left+(this.crownWidth-this.trunkWidth)/2,this.origin.top+this.crownHeight,this.trunkWidth,this.trunkHeight);
}
}
function DivMan(top,left,headHeight,headWidth,bodyHeight,bodyWidth) { //divMEN.
this.origin = new Point(top,left);
this.headHeight = headHeight;
this.headWidth = headWidth;
this.bodyHeight = bodyHeight;
this.bodyWidth = bodyWidth;
this.color = "#000000";
this.Draw = drawDivMan;
function drawDivMan() {
context.fillStyle = this.color;
context.fillRect(this.origin.left+(this.bodyWidth-this.headWidth)/2,this.origin.top,this.headWidth,this.headHeight);
context.fillRect(this.origin.left,this.origin.top+this.headHeight+1,this.bodyWidth,this.bodyHeight);
}
}
function Sky(top,left,height,width) { //skies.
this.origin = new Point(top,left);
this.height = height;
this.width = width;
this.color = "#98e8ff";
this.Draw = drawSky;
function drawSky() {
alert("Yorishomu.");
context.fillStyle = this.color;
context.fillRect(this.origin.left,this.origin.top,this.width,this.height);
}
}
function Ground() { //the ground.
this.origin = new Point(538,0);
this.hillStart = new Point(538,1);
this.hillTop1 = new Point(488,114);
this.hillTop2 = new Point(488,429);
this.hillEnd = new Point(538,543);
this.screenEnd = new Point(538,1366);
this.bottomRight = new Point(768,1366);
this.bottomLeft = new Point(768,0);
this.color = "#401000";
this.Draw = drawGround;
function drawGround() {
context.fillStyle = this.color;
context.beginPath();
context.moveTo(this.origin.left,this.origin.top);
context.lineTo(this.hillStart.left,this.hillStart.top);
context.lineTo(this.hillTop1.left,this.hillTop1.top);
context.lineTo(this.hillTop2.left,this.hillTop2.top);
context.lineTo(this.hillEnd.left,this.hillEnd.top);
context.lineTo(this.screenEnd.left,this.screenEnd.top);
context.lineTo(this.bottomRight.left,this.bottomRight.top);
context.lineTo(this.bottomLeft.left,this.bottomLeft.top);
context.closePath();
context.fill();
}
}
function Grass(height,targetGround) { //the grass.
this.color = "#10b030"
this.height = height;
this.targetGround = targetGround;
this.Draw = drawGrass;
function drawGrass() {
context.strokeStyle = this.color;
context.lineWidth = this.height;
context.beginPath();
context.moveTo(this.targetGround.origin.left,this.targetGround.origin.top); //it follows the ground.
context.lineTo(this.targetGround.hillStart.left,this.targetGround.hillStart.top);
context.lineTo(this.targetGround.hillTop1.left,this.targetGround.hillTop1.top);
context.lineTo(this.targetGround.hillTop2.left,this.targetGround.hillTop2.top);
context.lineTo(this.targetGround.hillEnd.left,this.targetGround.hillEnd.top);
context.lineTo(this.targetGround.screenEnd.left,this.targetGround.screenEnd.top);
context.stroke();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------Objects------------------------------------------------//
var sky = new Sky(0,0,538,1366); //the sky.
var ground = new Ground(); //the ground.
var grass = new Grass(38,ground); //the grass.
var stone = new Stone(418,228,75,75); //a stone.
var pedestal = new Stone(508,630,30,200); //a stone pedestal.
var tree0 = new Tree(138,1000,150,60,250,300); //a tree.
var tree1 = new Tree(83,73,300,40,100,300); //another tree.
var divMan = new DivMan(408,700,34,30,65,60); //divMan!!
//----------------------------------------------------------------------------------------------------//
function drawFrame() {
context.fillStyle = "#000000";
context.fillRect(0,0,100,100);
alert("Yo." + sky + context);
sky.Draw();
alert("Hi.");
ground.Draw();
grass.Draw();
tree1.Draw();
stone.Draw();
pedestal.Draw();
divMan.Draw(); // <-- Here's divMan.
tree0.Draw();
alert("Hey.");
}
/*function walk(click) { //in future, walk in mini-hops.
_walk = true;
staticEquilibrium = false;
if (click.screenX > rSubX+30) {vSubX = 5;} else {vSubX = -5;}
update();
}
function update() {
interval = setInterval(function() {
for (i=0; i<1; i++) {
divMan.origin.left += vSubX*deltaT+aSubX*deltaT*deltaT/2;
divMan.origin.top += vSubY*deltaT+aSubY*deltaT*deltaT/2;
}
drawFrame();
if (false) {clearInterval(interval);}
}
}*/
</script>
</html>
最佳答案
您认为您的代码在页面加载时运行,但事实并非如此。替换为:
window.onload = drawFrame();
// -------------- remove ^^
// the parentheses call function immediately
// and assign its return value to window.onload
与:
window.onload = drawFrame;
// now you're actually assigning the function
// itself to window.onload, as it should be
然后 context
将可用,一切都会正常工作。
关于JavaScript:全局变量似乎不是本地可引用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17266961/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!