如何解决我如何解决这些错误 p5js
1.我现在很困惑,在this.move()函数中,我想移动大陆数组中的元素 不同的位置,例如,所有属于'AF'的国家向右走,我有气泡阵列,在气泡阵列中,我有一个大陆阵列。当我打电话给 this.move() 在 draw 函数中,它移动了我不想移动的所有国家(气泡) 移动所有国家(气泡),这让我对如何称呼它感到困惑。 move() 移动大陆。请帮助我如何解决它。谢谢?
var data;
var bubbles;
function preload(){
data = loadTable("countryPopulation.csv","csv","header",continent = loadJSON('continents.json'),)};
function setup(){
createCanvas(1000,1000);
bubbles = [];
const rows = data.getRows();
const numColumns = data.getColumnCount();
maxAmt = 0;
for(let i = 0; i < rows.length; i++){
let value = Number(rows[i].get(5));
//console.log(value);
//create bubbles to add array bubble
var newBubble = new Bubble(rows[i].get(0),rows[i].get(2),Number(rows[i].get(5)));
newBubble.setValue(value);
//push bubble array
bubbles.push(newBubble);
//console.log(bubbles);
}
}
function draw(){
// iterate through bubbles,and draw them
background(100);
// drawing these bubbles with different orgin
push();
textAlign(CENTER);
//translate in middle of screen
translate(width/2,height/2);
//traverse in bubble array
for(let i = 0; i < bubbles.length; i++){
//console.log(bubbles[i])
bubbles[i].move();
bubbles[i].updateDirection(bubbles);
bubbles[i].draw();
// bubbles[i].update();
//console.log(bubbles[i])
}
pop();
}
function Bubble(_name,_continentCode,_population){
this.name = _name;
this.id = getRandomID();
this.continentCode = _continentCode;
//console.log(typeof(this.continentCode))
this.population = _population;
//this.position = createVector(0,random(-250,250),250));
this.color = color(random(0,255),random(0,255));
// give these bubblues position
this.position = createVector(0,0);
// a direction to move and then do Collision detection
this.dir = createVector(0,0);
this.size = 10;
this.draw = function(){
push();
nostroke();
fill(this.color);
// draw ellipse for bubbles
ellipse(this.position.x,this.position.y,this.size);
fill(0);
text(this.continentCode,this.position.x,this.position.y);
// change position according to the direction
this.position.add(this.dir);
pop();
}
this.move = function(){
for(i = 0; i < data.getRowCount(); i++){
let c = data.getRow(i);
continetcode = c.getString('ContinentCode');
//console.log(continetcode)
if(continetcode === "AF"){
//console.log(continetcode)
// this.pos = createVector(200,300);
// ellipse(this.pos.x,this.pos.y,this.size);
}
else if(continetcode === "AS"){
// this.pos = createVector(400,500);
// ellipse(this.pos.x,this.size);
}
else if(continetcode === "EU"){
//this.pos = createVector(600,700);
}
else if(continetcode === "NA" || continetcode === "SA"){
//console.log(continetcode);
//this.pos = createVector(800,900);
}
else{
//this.position= createVector(200,300);
}
}
}
// get some values to add to the bubbles
this.setValue = function(value){
// this.size = map(this.data[i],maxAmt,20,250);
this.size = map(value,1376048940,250)
}
// give array of bubble as argument
this.updateDirection = function(_bubbles){
// set direction to stop keep moving bubbles
this.dir = createVector(0,0);
for(let i = 0; i < _bubbles.length; i++){
//console.log(_bubbles[i]);
if(_bubbles[i].id != this.id){
// calculate distance between 2 continets
var v = p5.Vector.sub(this.position,_bubbles[i].position);
//if it is overlooping,distance any continet
var d = v.mag();
//collosion detection
if(d < this.size/2 + _bubbles[i].size/2){
// direction vector random is zero so there is not difference between their
//positions
// to fix that we add random vector 2D
if(d === 0){
this.dir.add(p5.Vector.random2D());
}
else{
//console.log('collision!');
// to move bubbles: take distance between 2 overlaping bubbles and take vector
// and add it to the direction
this.dir.add(v);
}
}
}
}
// to avoid to be huge number and jumping bubbles,get normalize function to get
// standard lenght of vector
this.dir.normalize();
}
}
// create unique ID for this bubbles
function getRandomID(){
let alpha = 'abcdefghiklmnopqrstuvwxyz0123456789';
let s = '';
for(let i = 0; i < 10; i++){
s += alpha[floor(random(0,alpha.length))];
}
return s;
}
在此处输入代码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。