简体   繁体   English

不明白为什么我会收到未定义的错误

[英]Don't understand why i am getting an undefined error

My code is basically up on jsFiddle: http://jsfiddle.net/9jekdhn1/ . 我的代码基本上是基于jsFiddle的: http : //jsfiddle.net/9jekdhn1/ I am playing around with canvas and animation. 我在玩帆布和动画。

My problem is that i am getting an Uncaught TypeError: Cannot read property 'x' of undefined canvas2.js:39. 我的问题是我遇到了Uncaught TypeError:无法读取未定义canvas2.js:39的属性'x'。

I am not seeing why i would get this error and it acts like it does see the value for x when running the script. 我没有看到为什么我会收到此错误,并且它的行为就像在运行脚本时确实看到了x的值。 If someone could tell me why i am getting this error and how to fix it that would be great. 如果有人可以告诉我为什么我得到此错误以及如何解决它,那将是很好的。 My script: 我的剧本:

    var canvas = document.getElementById('canvasTwo');
     canvas.width = 1000;
     canvas.height = 500;
  var circ = [];
  var c = canvas.getContext('2d');
  var cW = c.canvas.width, cH = c.canvas.height;
  var max = 20;

 function newCircle(){
    var x = Math.floor(Math.random() * cW) + 1;
    var y = 300;
    var r = 5;
    var cWidth = Math.floor( 5* Math.random());
    var cHeight = Math.floor( 5* Math.random());
    circ.push({"x":x,"y":y,"width":cWidth,"height":cHeight,"r":r});
}


function draw(){
    newCircle();
    for(var i=0;i<max;i++){
        c.fillStyle = 'rgba(163,219,91,0.3)';
        c.beginPath();
        c.arc(circ[i].x+=3, circ[i].y+=.5, circ[i].r,0, 2* Math.PI, false);
        c.fill();
     if(circ[i].y > cH || circ[i].x>cW){
        circ.splice(i,1);
     }
   }
 };
 function animate(){
    c.save();
    c.clearRect(0, 0, cW, cH);
    draw();
    c.restore();
 }

 setInterval(animate, 30);

You are iterating to max , however initially there are not max (20) numbers of items in circ . 您要迭代到max ,但是最初在circ中没有max (20)个项目。 Change to: 改成:

for(var i=0;i<circ.length;i++)

http://jsfiddle.net/9jekdhn1/1/ http://jsfiddle.net/9jekdhn1/1/

The error pretty much tells you what the problem is, you have this 该错误几乎告诉您问题出在哪里,

circ[i].x

and it's telling you that there is no x property, so the object doesn't have such a property. 并告诉您没有x属性,因此该对象没有这样的属性。
Either you iterated over the wrong numbers, so i doesn't reference anything in the array, or you simply didn't add a x property. 要么您遍历了错误的数字,所以i没有引用数组中的任何内容,要么您根本没有添加x属性。

In this case, it's the former, and it's because you have an empty array 在这种情况下,是前者,这是因为您有一个空数组

var circ = [];

then you start pushing to that array 然后你开始推到那个数组

circ.push({"x":x,"y":y,"width":cWidth,"height":cHeight,"r":r});

so the array starts at 0, and the first time you run the loop it only has one object inside it, and yet you've set max to 20 so you're iterating to 20, but there's only one single object at the zero index ? 因此数组从0开始,并且第一次运行循环时,它内部只有一个对象,但是您将max设置为20因此要迭代到20,但是零索引处只有一个对象?

If you want to limit to max 20, add a condition that uses the length if it's below the max value, like this 如果要限制为最大20,请添加一个条件,该条件使用小于最大长度的长度,像这样

var val = circ.length < max ? circ.length-1 : max;

for(var i=0;i<val;i++){ ...

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM