我不知道为什么我会得到一个“;undefined不是函数“;错误
I have no idea why im getting a "undefined is not a function" error
所以我不知道为什么在第35行出现这个错误,有人能解释一下为什么不起作用吗,因为我似乎不明白为什么?
这是它上传到的网页的链接,你可以在控制台中看到错误消息,以及所有代码。http://matthew-hoyle.co.uk/files/beta/projects/platformer.php
window.onload = function() {
var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 600;
function Player() {
this.width = 20;
this.height = 20;
this.color = "red";
this.posY = (canvas.height / 2) - (this.height / 2);
this.posX = (canvas.width / 2) - (this.width / 2);
this.volY = 0;
this.volX = 0;
this.gravity = 0.5;
this.onGround = true;
this.draw = function() {this.posY += this.volY;
this.posX += this.volX;
this.volY += this.gravity;
c.fillStyle = this.color;
c.fillRect(this.posX, this.posY, this.width, this.height);
};
}
function Obstacle(x,y,w,h,c) {
this.posY = y;
this.posX = x;
this.width = w;
this.height = h;
this.color = c;
this.draw = function() {
c.fillStyle = this.color;
c.fillRect(this.posX, this.posY, this.width, this.height);
};
}
//objects
var player = new Player();
var ground = new Obstacle(0, canvas.height-20,canvas.width,20,"red");
//game update loop
window.setInterval(function() {
//clears screen
c.fillStyle = "lightblue";
c.fillRect(0,0, canvas.width, canvas.height);
//drawing objects
player.draw();
ground.draw();
}, 30);
};
var ground = new Obstacle(0, canvas.height-20,canvas.width,20,"red");
在这里,您使用5个参数将ground
初始化为Obstacle
。第五个,c
,是字符串"red"
。因此,当稍后调用ground.draw()
时,您正试图调用"red".fillRect
,而这不是字符串的有效成员函数。
潜在的问题是,Obstacle
构造函数的参数c
隐藏了全局c
变量,因为它是一个更接近的范围,因此隐藏了要使用的画布上下文引用。
相关文章:
- mongoose.connect undefined不是一个函数
- jQuery Mobile Undefined不是一个函数
- 为什么Grunt Copy给了我一个'undefined不是函数'警告
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- jasmine测试抱怨undefined不是一个对象
- NodeJS:undefined不是一个新函数'新'
- XMLHttpRequest-当将responseText返回到另一个函数时,它返回undefined
- Javascript:uncatch TypeError:undefined不是一个函数
- TypeError: undefined 不是一个函数(评估 'document.getsElementsByC
- TypeError:undefined不是一个对象(评估'c.trigger')
- jQuery Cordova问题 - Typerror,undefined不是一个函数
- TypeError: 'undefined' 不是一个函数(评估 'mockBackend.ex
- Sencha Touch Uncatch type错误:undefined 不是一个函数
- JavaScript undefined不是一个函数(使用splice函数)
- passportjs TypeError: undefined 不是一个函数
- TypeError: undefined' 不是一个函数(评估 'myAudio.play()'
- 一个“undefined"打印数组项时显示
- 所以我使用一个全局变量,为什么这段代码返回一个'undefined'的警告
- Javascript中的表单返回一个"undefined"警报