I need someone to help me complete this game , I've just promised to my young brother that i would make a game for him :((((( | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I need someone to help me complete this game , I've just promised to my young brother that i would make a game for him :(((((

Straight to the problem, i've written a game that has a lot of birds flying from left to right screen and whenever i press the mouse , the birds should be failed down . Fortunately , it works , however , there is only 1 bird , but i want it to be a lot of bird , with random speed and amout to appear :v , the Code : Bird[] thebird = new Bird[20]; void setup(){ size ( 700,700 ); for ( int i =0; i < thebird.length ; i++){ thebird[i] = new Bird(); } frameRate(30); smooth(); } void draw(){ background(255); for ( int i = 0 ; i < thebird.length ; i++){ thebird[i].move(); thebird[i].display(); } if ( speeda==0) b = b + speedb; } float a; float b; float speeda; int speedb; class Bird { Bird(){ a= 20; b= random(100,600); speeda=0.5; speedb=5; } void display(){ fill(0); ellipse(a,b,50,50); fill(255); ellipse(a-8,b-4,10,10); ellipse(a+8,b-4,10,10); ellipse(a,b+8,20,5); } void move(){ a += speeda; if ( a > width+30) a=-30; } } void mousePressed(){ if ( mouseX >=a && mouseX <= a + 25 || mouseX <= a && mouseX>= a-25){ if ( mouseY >=b && mouseY <= b+25 || mouseY <= b && mouseY >= b -25){ speeda=0; } } }

24th Mar 2017, 4:12 PM
Thành Long
Thành Long - avatar
5 Answers
+ 4
I have the same problem as you I to want to create array of class-objects
24th Mar 2017, 4:34 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 4
I found a solution
24th Mar 2017, 5:15 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 4
it floped
24th Mar 2017, 5:16 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 3
The variables of the bird must be instance variables to that class. I think thats the only issue, since your not creating new variables for each bird when you create a bird. Making the floats instance variables: class bird{ private float a; //add other floats just like i did a mousePressed(){ if(stuff){// changing b speeda=0; b+=speedb; } } }
24th Mar 2017, 9:41 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Here's the fix for the full program based on my previous comment, I've changed variable names, and un-needed syntaxes to fit some programming standards. Bird[] thebird = new Bird[20]; void setup(){ size ( 700,700 ); for ( int i =0; i < thebird.length ; i++){ // initialization thebird[i] = new Bird(); } frameRate(30); smooth(); } void draw(){ background(255); for ( int i = 0 ; i < thebird.length ; i++) // update/draw each bird { thebird[i].move(); thebird[i].display(); } for(int i = 0; i < thebird.length; i++) // gradually move bird down if(thebird[i].speedX == 0) thebird[i].yPos += thebird[i].speedY; } class Bird { private float xPos; private float yPos; private float speedX; // speed in x direction private int speedY; // speed after being pressed by mouse in y direction Bird(){ // initialize instance variables this.xPos = 20; this.yPos = random(100,600); this.speedX = 0.5; this.speedY = 5; } void display(){ fill(0); ellipse(xPos,yPos,50,50); fill(255); ellipse(xPos-8,yPos-4,10,10); ellipse(xPos+8,yPos-4,10,10); ellipse(xPos,yPos+8,20,5); } void move(){ xPos += speedX; if ( xPos > width+30) xPos=-30; } } // end bird class void mousePressed(){ for(int i = 0; i < thebird.length; i++) // check mouse for each bird if ( mouseX >=thebird[i].xPos -25 && mouseX <= thebird[i].xPos + 25 // mouse in x region && mouseY >=thebird[i].yPos -25 && mouseY <= thebird[i].yPos+25) // mouse in y region thebird[i].speedX=0; }
24th Mar 2017, 9:53 PM
Rrestoring faith
Rrestoring faith - avatar