trying to creating a animated picture with arrays, cant seem to do it...
in
Programming Questions
•
1 year ago
I created this picture in processing and I would like to add arrays in it so that there are multiple slug "monsters" on the screen bouncing up and down.
w/o arrays, works fine
- float a = 0; //variables for movement
- float b = 0;
- void setup()
- {
- size(500, 500); //size of window
- background(226); //background color
- smooth(); //smooth lines
- strokeWeight(2.0); //line thickness
- fill(100); //color in option
- stroke(0); //line color
- }
- void draw(){
- a = a+1; // sets slug to move forward
- b = 20+sin(frameCount/30.0)*20; //sets slug to move up and down
- if (a>500){a=-300;} //resets slug
- background(200); //background color
- fill(0); //color of slug
- translate(a,b); //sets slug to move
- strokeWeight(2.0); //line thickness
- stroke(0); //line color
- beginShape(); //Continous Bézier Curves to draw slug
- curveVertex(20, 500); // the first control point
- curveVertex(10,500);
- curveVertex(80,300);
- curveVertex(150,230);
- curveVertex(210,220);
- curveVertex(260,225);
- curveVertex(290,240);
- curveVertex(300,290);
- curveVertex(260,290);
- curveVertex(230,280);
- curveVertex(210,270);
- curveVertex(180,320);
- curveVertex(170,380);
- curveVertex(200,500);
- curveVertex(170, 240); // is also the last control point
- endShape();
- fill(255);
- ellipse(270,260,8,18); //drawing for eyes
- ellipse(290,260,8,18);
- fill(0);
- ellipse(240,200,4,4);
- ellipse(255,200,4,4);
- fill(0);
- triangle(260, 240, 240, 200,270, 240); //drawing for antennas
- triangle(275, 240, 255, 200,285, 240);
- endShape();
with arrays, not really sure what im doing...
- final int NUMOFSLUGS = 5;
- float x = 0;
- float y = 0;
- float [] xpos = new float[NUMOFSLUGS];
- float [] ypos = new float[NUMOFSLUGS];
- void setup() {
- size(500, 500); //size of window
- background(226); //background color
- smooth(); //smooth lines
- strokeWeight(2.0); //line thickness
- fill(100); //color in option
- stroke(0); //line color
- }
- void draw() {
- changexpos(xpos);
- changeypos(ypos);
- drawSlug();
- for (int i=0; i < xpos.length; i++)
- {
- drawSlug(xpos[i], ypos[i]); //whats going on here?
- }
- }
- void changexpos (float[] x) {
- for (int i=0; i < x.length; i++) {
- x[i] = x[i]+1;
- }
- }
- void changeypos (float[] y) {
- for (int i=0; i < y.length; i++) {
- y[i] = 20+sin(frameCount/30.0)*20;
- }
- }
- void drawSlug() {
- background(200); //background color
- strokeWeight(1.5);
- fill(0);
- triangle(260+x, 240+y, 240+x, 200+y, 270+x, 240+y); //drawing for antennas
- triangle(275+x, 240+y, 255+x, 200+y, 285+x, 240+y);
- fill(0); //color of slug
- strokeWeight(2.0); //line thickness
- stroke(255); //line color
- beginShape(); //Continous Bézier Curves to draw slug
- curveVertex(20+x, 500+y); // the first control point
- curveVertex(10+x, 500+y);
- curveVertex(80+x, 300+y);
- curveVertex(150+x, 230+y);
- curveVertex(210+x, 220+y);
- curveVertex(260+x, 225+y);
- curveVertex(290+x, 240+y);
- curveVertex(300+x, 290+y);
- curveVertex(260+x, 290+y);
- curveVertex(230+x, 280+y);
- curveVertex(210+x, 270+y);
- curveVertex(180+x, 320+y);
- curveVertex(170+x, 380+y);
- curveVertex(200+x, 500+y);
- curveVertex(170+x, 240+y); // is also the last control point
- endShape();
- fill(255);
- strokeWeight(1.5);
- ellipse(270+x, 260+y, 8, 18); //drawing for eyes
- ellipse(290+x, 260+y, 8, 18);
- fill(0);
- ellipse(240+x, 200+y, 4, 4);
- ellipse(255+x, 200+y, 4, 4);
- }
can someone point me in the right direction at what im doing wrong?
1