# Class Problem - Any Suggestions

**URL:** <https://discourse.processing.org/t/class-problem-any-suggestions/14864>\
**Category:** Libraries\
**Created:** [October 21, 2019, 7:48pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864 "2019-10-21T19:48:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cbeni](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Cbeni](https://discourse.processing.org/u/Cbeni)\
**Post date:** [October 21, 2019, 7:48pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/1 "2019-10-21T19:48:48Z")

</div>

My bolt class is not being recognized. Any suggestions?

```auto
import processing.sound.*;

SoundFile myAudio;
SoundFile AudioWin;

//variables
float rectx =100;
float recty =100;
float b=20;
PImage myImageRobot;
PImage myImageBolt;
PImage ImageBackground;
PImage ImageYouWin;
int s;
int score;
int d;
boolean GameOver=false;
boolean hit = false;

Bolts MyBolts1;
Bolts MyBolts2;
Bolts MyBolts3;

void setup() {
  size(400,400);
  //character
  ImageBackground = loadImage("Background.png");
  ImageYouWin = loadImage("YouWin.png");
  myImageBolt = loadImage("Bolt.jpg");
  myImageRobot = loadImage("Robot.png");
 //Audio
  myAudio = new SoundFile(this, "Coins.mp3");//import soundfile 
  AudioWin = new SoundFile(this, "YouWin.mp3");
//Characters
  MyBolts1 = new Bolts(7,6,1,2,12,15,myImageBolt);
  MyBolts2 = new Bolts(6,8,2,4,10,19,myImageBolt);
  MyBolts3 = new Bolts(8,10,3,6,15,9,myImageBolt);
noStroke();
}

void draw() {
 Background();
 MoveRobot();
 
text("My Score", 340, 20);
text(score, 400, 20);

MyBolts1.BouncingBolt();
MyBolts1.SetDistance(rectx,recty);
MyBolts1.Score();

MyBolts2.BouncingBolt();
MyBolts2.SetDistance(rectx,recty);
MyBolts2.Score();

MyBolts3.BouncingBolt();
MyBolts3.SetDistance(rectx,recty);
MyBolts3.Score();
}

void Background(){
background(ImageBackground); 
}

void MoveRobot(){
  imageMode(CENTER);
  image(myImageRobot, rectx, recty, 60, 60);
  
if(keyCode==LEFT){
  print(LEFT);
  rectx=rectx-2;
}
if(keyCode==RIGHT){
  print(RIGHT);
  rectx=rectx+2;
}
if(keyCode==UP){
  print(UP);
  recty=recty-2;
}
if(keyCode==DOWN){
  print(DOWN);
  recty=recty+2;}  

if (rectx<20) {
    rectx=20;
  }
  if (rectx>460) {
    rectx=460;
  }
  if (recty<30) {
    recty=30;
  }
  if (recty>400) {
    recty=400;
  }
  
}

///Class Bolts
class Bolts {

  //class variables
  float x;
  float y;
  PImage myImageBolt;
  float d;
  float directionY;
  float directionX;
  float speedX;
  float speedY;
  float score;
  boolean GameOver=false;
  boolean hit = false;
  float rectx = 200;
  float recty = 200;

  //Constructor
  Bolts(float X, float Y, Float dx, float dy, float sx, float sy, PImage B){
    x=X;
    y=Y;
    directionX=dx;
    directionY=dy;
    speedX=sx;
    speedY=sy;
    myImageBolt=B;
  }

  void BouncingBolt() {
    x=x+directionX*speedX;
    y=y+directionY*speedY;

    imageMode(CENTER);
    image(myImageBolt, x, y, 50, 50);

    if ((x>width-10)||(x<10)) {
      directionX = directionX*-1;//change direction
      println(x);
    }

    if ((y>height-10)||(y<10)) {
      directionY =directionY*-1;
      println(y);
    }
  }

  void SetDistance(float rectx, float recty) {
    d=dist(rectx, recty, x, y);
  }

  void Score() {
    fill(255);   
    if ((d<30)&&(hit==false)) {
      score=score+1;
      myAudio.play();
      hit = true;
    }

    if (d>30) {
      hit=false;
    }

    if ((score==5) && (GameOver==false)) {
      AudioWin.play();
      image(ImageYouWin, 240, 240, 480, 480);
      GameOver=true;
      noLoop();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 21, 2019, 8:07pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/2 "2019-10-21T20:07:37Z")

</div>

Welcome to the forum!

this works:

` MyBolts1 = new Bolts(7.0, 6.0, 1.0, 2.0, 12.0, 15.0, myImageBolt);`

---

<div class="post-metadata">

**Author:** ![Cbeni](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Cbeni](https://discourse.processing.org/u/Cbeni)\
**Post date:** [November 20, 2019, 3:46am UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/3 "2019-11-20T03:46:54Z")

</div>

Thank you! That worked =]

---

<div class="post-metadata">

**Author:** ![Cbeni](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Cbeni](https://discourse.processing.org/u/Cbeni)\
**Post date:** [December 2, 2019, 8:12pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/4 "2019-12-02T20:12:08Z")

</div>

whoops! Still not working, I’m not sure what else it could be. any further suggestions?

```auto
import processing.sound.*;

SoundFile myAudio;
SoundFile AudioWin;
SoundFile BackgroundMusic;
SoundFile Hit;
SoundFile GO;

//variables
float rectx =100;
float recty =100;
float b=20;
PImage myImageRobot;
PImage myImageBolt;
PImage ImageBackground;
PImage ImageYouWin;
PImage myImageGameOver;
int s;
int score;
int d;
boolean GameOver=false;
boolean hit = false;

Bolts MyBolts1;
Bolts MyBolts2;
Bolts MyBolts3;

void setup() {
  size(400,399);
  //character
  ImageBackground = loadImage("Background.png");
  ImageYouWin = loadImage("YouWin.png");
  myImageBolt = loadImage("Bolt.jpg");
  myImageRobot = loadImage("Robot.png");
  myImageGameOver = loadImage("GameOver.png");
 //Audio
  myAudio = new SoundFile(this, "RobotBlip.mp3");//import soundfile 
  AudioWin = new SoundFile(this, "Win.wav");
  BackgroundMusic = new SoundFile(this, "Wandering.mp3");
  Hit = new SoundFile(this, "ShortCircuit.mp3");
  GO = new SoundFile(this, "Fizzle.mp3");
//Characters
  MyBolts1 = new Bolts(7.0, 6.0, 1.0, 2.0, 12.0, 15.0, myImageBolt);
  MyBolts2 = new Bolts(6.0, 8.0, 2.0, 4.0, 10.0, 19.0, myImageBolt);
  MyBolts3 = new Bolts(8.0, 10.0, 3.0, 6.0, 15.0, 9.0, myImageBolt);
  
noStroke();
}

void draw() {
 Background();
 MoveRobot();
 
text("My Score", 300, 20);
text(score, 360, 20);

MyBolts1.BouncingBolt();
MyBolts1.SetDistance(rectx,recty);
MyBolts1.Score();

MyBolts2.BouncingBolt();
MyBolts2.SetDistance(rectx-1,recty-1);
MyBolts2.Score();

MyBolts3.BouncingBolt();
MyBolts3.SetDistance(rectx+1,recty+1);
MyBolts3.Score();
}

void Background(){
   size(400,399);
background(ImageBackground); 
BackgroundMusic.play();
}

void MoveRobot(){
  imageMode(CENTER);
  image(myImageRobot, rectx, recty, 60, 60);
  
if(keyCode==LEFT){
  print(LEFT);
  rectx=rectx-2;
}
if(keyCode==RIGHT){
  print(RIGHT);
  rectx=rectx+2;
}
if(keyCode==UP){
  print(UP);
  recty=recty-2;
}
if(keyCode==DOWN){
  print(DOWN);
  recty=recty+2;}  

if (rectx<20) {
    rectx=200;
  }
  if (rectx>460) {
    rectx=300;
  }
  if (recty<10) {
    recty=100;
  }
  if (recty>400) {
    recty=400;
  }
  
}

Tab2 - Class Bolts

class Bolts {

  //class variables
  float x;
  float y;
  PImage myImageBolt;
  float d;
  float directionY;
  float directionX;
  float speedX;
  float speedY;
  float score;
  boolean GameOver=false;
  boolean hit = false;
  float rectx = 10;
  float recty = 10;

  //Constructor
  Bolts(float X, float Y, Float dx, float dy, float sx, float sy, PImage B){
    x=X;
    y=Y;
    directionX=dx;
    directionY=dy;
    speedX=sx;
    speedY=sy;
    myImageBolt=B;
  }

  void BouncingBolt() {
    x=x+directionX*speedX;
    y=y+directionY*speedY;

    imageMode(CENTER);
    image(myImageBolt, x, y, 50, 50);

    if ((x>width-1)||(x<1)) {
      directionX = directionX*-1;//change direction
      println(x);
    }

    if ((y>height-1)||(y<1)) {
      directionY =directionY*-1;
      println(y);
    }
  }

  void SetDistance(float rectx, float recty) {
    d=dist(rectx, recty, x, y);
  }

  void Score() {
    fill(255);   
    if ((d<10)&&(hit==true)) {
      score=score-1;
      myAudio.play();
      hit = true;
    }
    
       if ((d>11)&&(hit==false)) {
      score=score+1;
      myAudio.play();
      hit = false;
    }

  if (d>11) {
      hit=false;
   }

    if ((score==50) && (GameOver==false)) {
      AudioWin.play();
      image(ImageYouWin, 240, 240, 380, 380);
      GameOver=true;
    }
    
  if ((score==-5) && (GameOver==true)) {
      GO.play();
      image(myImageGameOver, 240, 240, 380, 380);
      GameOver=true;
      noLoop();
    } 
}
}

```

Thank you!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 2, 2019, 9:19pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/5 "2019-12-02T21:19:36Z")

</div>

We can’t run your code

What happens falsely, what do you want to happen instead?

---

<div class="post-metadata">

**Author:** ![Cbeni](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@Cbeni](https://discourse.processing.org/u/Cbeni)\
**Post date:** [December 2, 2019, 10:24pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/6 "2019-12-02T22:24:11Z")

</div>

its supposed to be a simple game if the character us hit to minus point if the character evades the bolts you gain a point. game over if the score is -5 and you win if the score reaches 50 (which maybe too complicated)

So is it that some of my false needed to be trues?

Thank you

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 2, 2019, 10:32pm UTC](https://discourse.processing.org/t/class-problem-any-suggestions/14864/7 "2019-12-02T22:32:36Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

your code need lots of files to run  
so we can not test it

if you make a version without files  
and sound…  
we might test your logic.
