首页 > 解决方案 > 有没有办法解决“Uncaught TypeError: Cannot read property of undefined In JavaScript”?

问题描述

我是 Js 的新手,一天前才开始。我对此一无所知,它的模式实际上让我感到害怕,因为我来自 python 和 C# 背景。无论如何,我试图通过创建它的对象数组来产生许多圆圈,但它一直给我这些错误。尽管如此,当我只为一个对象工作时,它完全可以工作。我不知道该怎么做,我查找并发现未定义意味着一些没有初始化,但对我来说一切似乎都已定义。请帮我一点忙

[![错误信息][1]][1]


canvas.height = window.innerHeight
canvas.width = window.innerWidth

var c = canvas.getContext('2d')


function circle(x,y, dx,dy, radius){
    this.x = x
    this.y = y
    this.dx = dx
    this.dy = dy
    this.radius = radius

    this.draw = function(){
        c.beginPath()
        c.arc(this.x,this.y, this.radius, 0, Math.PI * 2, false)
        c.strokeStyle = "blue"
        c.stroke();
    }

    this.update = function() {
        if( (this.x + this.radius > innerWidth) || (this.x - this.radius < 0))
        {
            this.dx = -this.dx
        }

        if( (this.y + this.radius > innerHeight) || (this.y - this.radius < 0))
        {
            this.dy = -this.dy
        }

        this.x += this.dx
        this.y += this.dy

        this.draw();

        }
}


var CircleArray = []

for(var i = 0 ; i < 10; i++)
{
    var x = Math.random() *  innerWidth
    var y = Math.random() *  innerHeight

    var dx = 4
    var dy = 4

    var radius = 30

    CircleArray.push(new circle(x,y,dx,dy, radius))

}

function animate()
{
    requestAnimationFrame(animate)
    c.clearRect(0,0, innerWidth, innerHeight)

    for(var j = 0 ; j < CircleArray.length; j++)
    {
        CircleArray[i].update()
    }

}
animate()```


  [1]: https://i.stack.imgur.com/qWQto.png

标签: javascriptarraysfunctionobjectgraphics

解决方案


推荐阅读