Class Problem - Any Suggestions

My bolt class is not being recognized. Any suggestions?

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();
    }
  }
}
1 Like

Welcome to the forum!

this works:

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

2 Likes

Thank you! That worked =]

1 Like

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

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!

We can’t run your code

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

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

please format your code posting by pasting it into the

</> 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.