Classes - variable reference to default value?


(Kamil) #1

Hello everyone!

I’ve started a project using classes. I think it is still pretty simple, but I have no idea why in those lines:

bulletLeft.x=left.x;
bulletLeft.y=left.y;

variables return 3 and 32. Default values when creating class. I use the same variables (left.x and left.y) to move this square. So shouldn’t it be different value? How does it work exactly?

#include <Arduboy2.h>

Arduboy2 ard;


class Player {
  public:
  int x=3;
  int y=32;
  int speedMove=1;
  bool ammo=1;
  bool shooting=0;

  void bounce() {
    speedMove=speedMove*-1;
  }

  void moving() {
    y+=speedMove;
    if (y<=0 and speedMove<0) bounce();
    if (y>=56 and speedMove>0) bounce();
    
  }
  
  void shoot() {
    ammo=0;
  }

};

class Bullet {
 public: 
  int x=10;
  int y=10;
  int speedMove=1;

  void moving() {
    x+=speedMove;
  }
};

Player left;
Player right;
Bullet bulletLeft;
Bullet bulletRight;


void setup() {
  // put your setup code here, to run once:
  ard.begin();
  ard.setFrameRate(45);
  right.x=117;
  bulletRight.speedMove=-1;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!(ard.nextFrame()))
   return;
  ard.pollButtons();
  ard.clear();


  controls();

  left.moving();
  right.moving();

  if (left.shooting==true) bulletLeft.moving();


  ard.drawRect(left.x,left.y,8,8,WHITE);
  ard.drawRect(right.x,right.y,8,8,WHITE);
  ard.drawRect(bulletLeft.x,bulletRight.y,2,2,WHITE);
  ard.setCursor(50,0);
  ard.print(left.x); // WHY 3???
  ard.display();
}

void controls() { //************CONTROLS******************
  
  if( ard.justPressed(LEFT_BUTTON) == true) {
    left.bounce();
    bulletLeft.x=left.x;
    bulletLeft.y=left.y;

    if (left.ammo==true) {
   
      left.shooting=true;
     
    }
  }

  if( ard.justPressed(B_BUTTON) == true) {
    right.bounce();
    if (right.ammo==true) {

      right.shoot();
    }
  }


}

(Pharap) #2

I spotted this earlier but had to put off replying because I’ve been helping with other issues elsewhere.


If you’re wondering why the bullet always draws at the same height, your problem is here:

ard.drawRect(bulletLeft.x,bulletRight.y,2,2,WHITE);

If you’re wondering why 3 is always printed, you’re printing left.x, which is the left player’s x coordinate, and that never changes.

ard.print(left.x); // WHY 3???
void moving() {
  y+=speedMove; // Changes y, but not x
  if (y<=0 and speedMove<0) bounce();
  if (y>=56 and speedMove>0) bounce(); 
}

(Kamil) #3

Im ashamed. Should I delete this topic? :smiley:

And thank you!


(Pharap) #4

Don’t be. Mistakes happen.
Mistakes are the process of learning.

Nah, everyone makes mistakes.
90% of my bugs are because I’ve typed the wrong thing by mistake.

One time I uploaded every single file but my main .ino file to Github, so I had to add it afterwards and I’d already made a release and posted a thread about the game, so it was really awkward when the first post on the thread was “Where is the .ino file?”.

(Besides which, I think only moderators can delete threads.)

You’re welcome.
I wish I’d looked at it sooner.
I was expecting it to be some complicated logic problem. :P


(Simon) #5

Sorry about that.  


(Pharap) #6

If you hadn’t said it someone else would have.
The moral of this story is to double check the master branch compiles before making a release.