Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组冲突检测中的p5.js对象_Javascript_Arrays_Collision Detection_P5.js - Fatal编程技术网

Javascript 数组冲突检测中的p5.js对象

Javascript 数组冲突检测中的p5.js对象,javascript,arrays,collision-detection,p5.js,Javascript,Arrays,Collision Detection,P5.js,因此,此问题与问题类似,但与问题不同,因此不要将其标记为重复问题。 如果我创建两个单独的矩形,如下所示: //rec is an object I made for the rectangles rect1 = new rec(random(width-40), random(height-40), 40, 40); rect2 = new rec(random(width-40), random(height-40), 40, 40); //r is an array I made and

因此,此问题与问题类似,但与问题不同,因此不要将其标记为重复问题。 如果我创建两个单独的矩形,如下所示:

//rec is an object I made for the rectangles
rect1 = new rec(random(width-40), random(height-40), 40, 40);
rect2 = new rec(random(width-40), random(height-40), 40, 40);
//r is an array I made and rec is an object I made for the rectangles
for(var i = 0;i < 10;i++) {
    r[i] = new rec(random(width-40), random(height-40), 40, 40); 
}
console.log(r[1].x); //returns: Uncaught TypeError: Cannot read property 'x' of undefined
我的代码运行良好

但是如果我创建一个如下的矩形数组:

//rec is an object I made for the rectangles
rect1 = new rec(random(width-40), random(height-40), 40, 40);
rect2 = new rec(random(width-40), random(height-40), 40, 40);
//r is an array I made and rec is an object I made for the rectangles
for(var i = 0;i < 10;i++) {
    r[i] = new rec(random(width-40), random(height-40), 40, 40); 
}
console.log(r[1].x); //returns: Uncaught TypeError: Cannot read property 'x' of undefined
下面是我在p5.js中为对象冲突编写的代码:

var rect1;
var r;
var num;
function setup(){
    r = [];
    num = 2;
    createCanvas(windowWidth,windowHeight- 4);
    for(var i = 0;i < num; i++){

        r = new rec(random(width-40),random(height-40),40,40);

    }

}

function draw(){
    background(40);
    r.show();
    console.log(r[1].x);
    for(var i = 0;i < num; i++)    {
        for(var j = 0;j<num; j++){


            if(r[i].x + r[i].width+r[i].xa >= r[j].x && r[i].y +r[i].height >=r[j].y && r[i].y<=r[j].y +r[j].height && r[i].x + r[i].xa <= r[j].x + r[j].width){
                r[i].xa *= -1;
                r[j].xa *= -1;

            }
            if(r[i].y + r[i].height + r[i].ya>= r[j].y && r[i].x +r[i].width>=r[j].x && r[i].x<=r[j].x + r[j].height && r[i].y + r[i].ya <= r[j].y + r[j].height){
                r[i].ya *= -1;
                r[j].ya *= -1;
            }
        }    
    }


}
function rec(x,y,wid,hei){
    this.x = x;
    console.log(this.x);
    this.y = y;
    this.width = wid;
    this.height= hei;
    this.xa = random(2,5);
    this.ya = random(2,5);
    this.show = function(){
        this.x;
        this.y;
        fill(255);
        noStroke();
        rect(this.x,this.y,this.width,this.height);
        this.x += this.xa;
        this.y += this.ya;
        if(this.x > width-this.width||this.x <0){
            this.xa *= -1;
            if(this.x > width/2){
                this.x = width-this.width;
            }else{
                this.x = 0;
            }
        }
        if(this.y > height-this.height||this.y <0){
            this.ya *= -1;
            if(this.y > height/2){
                this.y = height-this.height;
            }else{
                this.y = 0;
            }

        }

    }
}
function windowResized(){
    createCanvas(windowWidth,windowHeight- 4);
}
var rect1;
var-r;
var-num;
函数设置(){
r=[];
num=2;
createCanvas(窗口宽度,窗口高度-4);
对于(变量i=0;i=r[j].y&&r[i].y=r[j].x&&r[i].x height this.height | | this.y height/2){
this.y=高度-this.height;
}否则{
这个。y=0;
}
}
}
}
函数windowResized(){
createCanvas(窗口宽度,窗口高度-4);
}

在代码中,您可以编写:

for(var i = 0;i < num; i++){

    r = new rec(random(width-40),random(height-40),40,40);

}
需要:

  r[1].show();
或类似的

加:

您可以看到您的代码正常工作!只需要添加其余的矩形

以下是演示:

var rect1;
var-r;
var-num;
函数设置(){
r=[];
num=2;
createCanvas(窗口宽度,窗口高度-4);
对于(变量i=0;i=r[j].x&&r[i].y+r[i].高度>=r[j].y&&r[i].y=r[j].x&&r[i].x宽度/2){
this.x=宽度-this.width;
}否则{
这个.x=0;
}
}
if(this.y>height-this.height | | this.y<0){
这是1.ya*=-1;
如果(此.y>高度/2){
this.y=高度-this.height;
}否则{
这个。y=0;
}
}
}
}
函数windowResized(){
createCanvas(窗口宽度,窗口高度-4);
}