p5.j​​s对象在数组冲突检测中

时间:2017-10-01 03:56:02

标签: javascript arrays collision-detection p5.js

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

//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);
}

1 个答案:

答案 0 :(得分:1)

在你的代码中,你写道:

webpack

但你应该:

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

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

}

此外,在for(var i = 0;i < num; i++){ r[i] = new rec(random(width-40),random(height-40),40,40); }

draw()

需要:

  r.show();

或类似。

添加:

  r[1].show();

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

以下是演示:

r[0].show();
var rect1;
var r;
var num;

function setup() {
  r = [];
  num = 2;
  createCanvas(windowWidth, windowHeight - 4);
  for (var i = 0; i < num; i++) {
    r[i] = new rec(random(width - 40), random(height - 40), 40, 40);
  }
}

function draw() {
  background(40);
  r[0].show();
  r[1].show();
  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;
  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);
}