在此下面的代码中获得以下错误 未捕获类型错误:ball.draw不是函数!! 但球被定义为一个新的球和球有一个原型功能,绘制。 当不在init()函数中时,球被拉得很好? 任何想法都慷慨地接受了。
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var ball2;
var ball;
window.onload = init(context);
ball2 = new Ball(50, '#100fff');
ball2.x = 150;
ball2.y = 150;
ball2.draw(context);
function init(){
ball = new Ball(50, '#0000ff');
ball.x = 100;
ball.y = 100;
ball.draw(context);
}
//ball object
function Ball (radius, color){
this.radius = radius;
this .color = color;
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
};
Ball.prototype.draw = function(context){
context.fillStyle = this.color;
context.beginPath();
context.arc(this.x, this.y, this.radius, 0, 2*Math.PI, true);
context.closePath();
context.fill();
};