javascript - 有没有办法解决“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
解决方案
推荐阅读
- python - TensorFlow io.decode_csv 并沿一维选择数据
- php - 如何使具有不同产品 ID 的两种产品在 WooCommerce 购物车页面(BOGO)上充当一个
- amazon-web-services - 使用增加卷大小的 powershell 启动新的 ec2 实例
- java - 无法从 AsyncTask 访问 ProgressBar
- python - Gekko 求解器不提供任何解决方案
- oracle - 在 Oracle Apex 的后台运行进程
- r - 对齐两个列表中的许多短序列并找到互补的
- c# - C# 多线程和 WPF 页面控制
- ios - 从另一个 CurrentValueSubject 创建一个 CurrentValueSubject
- javascript - 将动态添加的 html 表行作为参数发布到控制器 - ASP.NET Core/MVC