-1

我正在使用以下代码来试用该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();

我尝试将其设为全局和局部变量,但我看不出哪里出错了?

4

1 回答 1

3

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

shapes = new Array();

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

shapes = [];
于 2012-11-24T21:34:07.730 回答