Class function works at top of draw() but not in if statement within draw()
in
Programming Questions
•
3 months ago
This is a continuation of
this sketch.
so far I have managed to solve a lot of little bugs, mostly typos and the like. Here is a new one that is currently baffling me. After finally getting a 'bouncing ball' class to work (I call it a "banana"), I am confused about why it will not work within an if statement. The setup() and draw() blocks are below. The class works and I know this is true because if I call newBanana.display(); and newBanana.move(); on line 23 it works.
However, I want to call these functions on lines 36 & 37 and here they do not work. Everything else in this statement works. I have put in print functions and they print when the conditional is met. But the function calls do not work. I have also tried reinstantiating(?) the class on line 35 (newBanana = new Banana();) but that also did not work.
I'm sure its easy and hopefully I'll figure it out before someone tells me the answer. :)
If you think viewing the full code will help,
click here.
- Car myCar; // A Car
- Grape newGrape; // A Grape
- Banana newBanana; // A Bananna
- int hitCount = 0; // Counter for how many times the grape has been hit
- int carSpeed = 7;
- void setup() {
- size(500, 500);
- myCar = new Car();
- newGrape = new Grape();
- newBanana = new Banana();
- }
- void draw() {
- background(255);
- //
- text (hitCount, width - 50, height - 20); // place HUD in bottom right corner
- fill(0);
- newGrape.display(); // draw a grape randomly on the screen
- myCar.display(); // Car starts in the middle of the screen
- myCar.move(); // Move the car where the user wants
- // Checks for collision between grape and car
- // Grape must be fully within the boundries of the car to be considered "hit"
- if (rectangle_collision (myCar.xpos, myCar.ypos, myCar.sizeW, myCar.sizeH,
- newGrape.xpos, newGrape.ypos, newGrape.sizeW, newGrape.sizeH)) {
- hitCount = hitCount + 1; // increase hit counter by 1
- if (hitCount == 10) {
- hitCount = 0;
- newGrape.kill();
- newBanana.display();
- newBanana.move();
- }
- else {
- newGrape = new Grape(); // Grape redraws in a random location.
- }
- }
1