javascript为什么错误是TypeError: undefined is not a function
<!DOCTYPEhtml><html><head><scripttype="js/javascript"src="js/gameDraw.js"></script><s...
<!DOCTYPE html>
<html>
<head>
<script type="js/javascript" src="js/gameDraw.js"></script>
<script>
(function () {
alert("help me !!!!!");
var gameDraw = new gameDraw("myCanvas");
alert("help me11111 !!!!!");
gameDraw.drawBall();
alert("help me2222 !!!!!");
})();
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="400">Your Broswer don't support html5 canvas<canvas>
</body>
</html>
接下来是js文件
function gameDraw(canvas){
var canvasX,canvasY,ballX,ballY;
var ctx = document.getElementById(canvas).getContext("2d");
function upDate(){
this.drawBall();
this.drawCue();
};
function drawBall(){
this.drawArc(200,200,8);
};
function drawCue(){
this.drawRect(150,120,200,130)
};
function drawArc(certerX,certerY,R){
cxt.fillStyle="#000000";
cxt.beginPath();
cxt.arc(certerX,certerY,R,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();
};
function drawRect(leftTopX,leftTopY,rightLowX,rightLowY){
cxt.fillStyle="#FF0000";
cxt.fillRect(leftTopX,leftTopY,rightLowX,rightLowY);
};
}; 展开
<html>
<head>
<script type="js/javascript" src="js/gameDraw.js"></script>
<script>
(function () {
alert("help me !!!!!");
var gameDraw = new gameDraw("myCanvas");
alert("help me11111 !!!!!");
gameDraw.drawBall();
alert("help me2222 !!!!!");
})();
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="400">Your Broswer don't support html5 canvas<canvas>
</body>
</html>
接下来是js文件
function gameDraw(canvas){
var canvasX,canvasY,ballX,ballY;
var ctx = document.getElementById(canvas).getContext("2d");
function upDate(){
this.drawBall();
this.drawCue();
};
function drawBall(){
this.drawArc(200,200,8);
};
function drawCue(){
this.drawRect(150,120,200,130)
};
function drawArc(certerX,certerY,R){
cxt.fillStyle="#000000";
cxt.beginPath();
cxt.arc(certerX,certerY,R,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();
};
function drawRect(leftTopX,leftTopY,rightLowX,rightLowY){
cxt.fillStyle="#FF0000";
cxt.fillRect(leftTopX,leftTopY,rightLowX,rightLowY);
};
}; 展开
2个回答
展开全部
你gameDraw类没有写对哦,而且html也有点小错误:
1、<script type="js/javascript" src="gameDraw.js"></script>
把type="js/javascript"改成type="text/javascript"
2、var gameDraw = new gameDraw("...");变量名和类型重合了,需要修改成不一样的
3、gameDraw.js里面一个小错误,var ctx=document....和下面引用的时候名字不一样哦,一个是ctx,一个是cxt,注意下
4、gameDraw类里面的方法命名不对哦,比如第一个方法drawBall要改成这样的this.drawBall = function(){...}
以上4点修改后就可以跑起来了
1、<script type="js/javascript" src="gameDraw.js"></script>
把type="js/javascript"改成type="text/javascript"
2、var gameDraw = new gameDraw("...");变量名和类型重合了,需要修改成不一样的
3、gameDraw.js里面一个小错误,var ctx=document....和下面引用的时候名字不一样哦,一个是ctx,一个是cxt,注意下
4、gameDraw类里面的方法命名不对哦,比如第一个方法drawBall要改成这样的this.drawBall = function(){...}
以上4点修改后就可以跑起来了
追问
我改完了好像还是不能跑起来,你可以加下我Q吗43459635,十分感谢
追答
可能是你哪里还没有改完吧,我就是把你的代码copy下来改的,改了之后运行不报错,页面上有个灰色的小正方形
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询