【问题标题】:How to add object to an array properly in p5.js?如何在 p5.js 中正确地将对象添加到数组中?
【发布时间】:2018-05-20 08:13:11
【问题描述】:

好吧,在你阅读代码之前,先知道这是一个名为 P5.js 的 JavaScript 库 https://p5js.org

我注意到我的 addMCS 函数没有将对象添加到数组中,而是屏幕变为空白,并且我没有从控制台得到任何响应。

var mcs = [];

function mouseCircle(x,y,s,color){
//constructor: mouseCircle(x,y,s,color)
    this.x=x;
    this.y=y;
    this.s=s;
    this.color = color;
    this.mouseOver = false;
}

mouseCircle.prototype.mouseCollision = function(){
  if(dist(this.x,this.y,mouseX,mouseY)<=this.s/2){

    this.mouseOver=true;
  }else{
    this.mouseOver = false;
  }
};


mouseCircle.prototype.addMCS = function(){

    //THIS HERE IS THE ISSUE!!
    mcs.push({this.x,this.y,this.s,this.color,this.mouseOver});
};

mouseCircle.prototype.Display = function(){
    if(this.mouseOver) {
        fill(this.color);
    } else {
        fill(255,255,255);
    }

    ellipse(this.x,this.y,this.s,this.s);
};

function setup() {
    createCanvas(1000,650);
}

var mc1;
var mc2;
var mc3;

var mc2Color;

function draw() {

    background(200,200,200);

    mc1 = new mouseCircle(275,450,50,'green');//constructs 1st circle
    mc1.mouseCollision();
    mc1.Display();
    mc1.addMCS();
    console.log(mcs[0]);

    /*
    mc2Color = color(0,0,255);
    mc2 = new mouseCircle(375,450,50,mc2Color);//constructs 2nd circle
    mc2.mouseCollision();
    mc2.Display();

    mc3 = new mouseCircle(275,350,50,color(255,0,0));//constructs 3rd circle
    mc3.mouseCollision();
    mc3.Display();

    mc4 = new mouseCircle(375,350,50,'yellow');//constructs 3rd circle
    mc4.mouseCollision();
    mc4.Display();

    */

    //mouseCircle(575,150,50);
    //mouseCircle(475,520,50);
    //mouseCircle(375,150,50);
}

我看不到任何类型的逻辑或语法错误,我相信我记得我以前使用过这种方法(制作按钮)。

【问题讨论】:

    标签: javascript html arrays processing p5.js


    【解决方案1】:

    为什么不直接使用mcs.push(this);

    工作示例:

    var mcs = [];
    
    function mouseCircle(x, y, s, color) {
      //constructor: mouseCircle(x,y,s,color)
      this.x = x;
      this.y = y;
      this.s = s;
      this.color = color;
      this.mouseOver = false;
    }
    
    mouseCircle.prototype.mouseCollision = function() {
      if (dist(this.x, this.y, mouseX, mouseY) <= this.s / 2) {
    
        this.mouseOver = true;
      } else {
        this.mouseOver = false;
      }
    };
    
    
    mouseCircle.prototype.addMCS = function() {
    
    
      mcs.push(this);
    };
    
    mouseCircle.prototype.Display = function() {
      if (this.mouseOver) {
        fill(this.color);
      } else {
        fill(255, 255, 255);
      }
    
      ellipse(this.x, this.y, this.s, this.s);
    };
    
    function setup() {
      createCanvas(1000, 650);
    }
    
    var mc1;
    var mc2;
    var mc3;
    
    var mc2Color;
    
    function draw() {
    
      background(200, 200, 200);
    
      mc1 = new mouseCircle(275, 450, 50, 'green'); //constructs 1st circle
      mc1.mouseCollision();
      mc1.Display();
      mc1.addMCS();
      //console.log(mcs[0]);
    
      /*
            mc2Color = color(0,0,255);
            mc2 = new mouseCircle(375,450,50,mc2Color);//constructs 2nd circle
            mc2.mouseCollision();
            mc2.Display();
        
            mc3 = new mouseCircle(275,350,50,color(255,0,0));//constructs 3rd circle
            mc3.mouseCollision();
            mc3.Display();
        
            mc4 = new mouseCircle(375,350,50,'yellow');//constructs 3rd circle
            mc4.mouseCollision();
            mc4.Display();
        
            */
    
      //mouseCircle(575,150,50);
      //mouseCircle(475,520,50);
      //mouseCircle(375,150,50);
    }
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2020-08-30
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      相关资源
      最近更新 更多