HTML5画布阵列创建错误

时间:2012-11-24 21:32:42

标签: javascript jquery html5 canvas html5-canvas

我使用以下代码试用canvas元素

    Shape = function(x,y){
        this.x = x;
        this.y = y; 
    };

    shapes = new array();

    shapes.push(new Shape(50,50,10,10));
    shapes.push(new Shape(100,100,10,10));
    shapes.push(new Shape(150,150,10,10));

    function animate(){
        context.clearRect(0,0, canvas.width(),canvas.height());
        var shapesLength = shapes.length;
        for(var i = 0; i < shapesLength; i++){
            var tmpShape = shapes[i];
            tmpShape.x++;
            context.fillRect(tmpShape.x,thmpShape.y,10,10);
        }
        context.fillStyle = 'yellow';
        context.fill();
        if(playAnimation){
            setTimeout(animate, 1)
        }
    }

但是,当我在浏览器中运行它时,我收到以下错误 -

ReferenceError: array is not defined    

shapes = new array();

我试过把它变成一个全局和局部变量,我只是看不出我哪里出错了?

1 个答案:

答案 0 :(得分:3)

array应该大写,因为这是构造函数的名称:

shapes = new Array();

此外,最好使用方括号表示法来创建数组。像这样:

shapes = [];