0
votes

I have a moving image as a background

PImage background;
int x=0; //global variable background location
boolean up;
boolean down;
Rocket myRocket;
Alien alien1,alien2;

void setup(){
 size(800,400);
 background = loadImage("spaceBackground.jpg");
 background.resize(width,height);
 myRocket = new Rocket();
 alien1 = new Alien(800,200,4,-3);
 alien2 = new Alien(800,200,5,2);
}

void draw ()
{
 image(background, x, 0); //draw background twice adjacent
 image(background, x+background.width, 0);
 x -=4;
 if(x == -background.width)
 x=0; //wrap background
 myRocket.run();
 alien1.run();
 alien2.run();

}

void keyPressed(){
  if(keyCode == UP)
  {
   up = true;
  }
  if(keyCode == DOWN)
  {
   down = true;
  }
}

void keyReleased(){
  if(keyCode == UP)
  {
   up = false;
  }
  if(keyCode == DOWN)
  {
   down = false;
  }
}   

First Class. The alien goes up and down towards the rocket.

class Alien { 
 int x;
 int y;
 int speedX,speedY;

 Alien(int x,int y,int dx,int dy){
   this.x = x;
   this.y = y;
   this.speedX = dx;
   this.speedY = dy;
 }

void run(){
  alien();
  restrict();
}

void alien(){
 fill(0,255,0);
 ellipse(x,y,30,30);
 fill(50,100,0);
 ellipse(x,y,50,15);
 x = x - speedX;
 y = y + speedY;
}

void restrict(){
 if (y < 15 || y > 380 ){
    speedY = speedY * -1;
  }
 if (x == 0){
     x = 800; 
 }
}
}

Second Class. You control the rocket going up and down

class Rocket {
  int x;
  int y;
  int speedy;

 Rocket(){
   x = 40;
   y = 200;
   speedy = 3;

 }

void run(){
 defender();
 move();
 restrict();
}

void defender(){
 fill(255,0,0);
 rect(x,y,50,20);
 triangle(x+50,y,x+50,y+20,x+60,y+10);
 fill(0,0,255);
 rect(x,y-10,20,10);
}

void move() {
 if(up)
 {
  y = y - speedy;
 }
 if(down)
 {
  y = y + speedy;
 }
} 

void restrict(){
 if (y < 10) {
    y = y + speedy;
  }
  if (y > 380) {
    y = y - speedy;
  }
}

boolean IsShot(Rocket myRocket){
 if (alien1.x == 40)
  {
   if(alien1.y>=y && alien1.y<=(y+50))
    {
      return true;
    }
   return false;
}
}
}

When one of the aliens hit the rocket i want the game to stop. On boolean IsShot(Rocket myRocket) i keep getting error "The method must return a result type boolean."

1
What happens if alien1.x != 40?Lew Bloch
i get the same errorAlexandros Constantinides
You cannot possibly know that until your code runs, which requires that it compile first. What I mean is, what will the code you showed us return for that condition, if allowed to compile? Trace it through for yourself. What code is in there right now to handle the case where alien1.x != 40? Look carefully at your code and trace the path out of the method.Lew Bloch

1 Answers

1
votes

Please try to narrow your problem down to a MCVE before you post a question. A simple rectangle colliding with another rectangle would be all the code you need to show this error.

Also, please get into the habit of using proper coding conventions. Functions should start with a lower-case letter, and code should be indented. That would help you see the problem with this function:

boolean isShot(Rocket myRocket) {
  if (alien1.x == 40)
  {
    if (alien1.y>=y && alien1.y<=(y+50))
    {
      return true;
    }
    return false;
  }
}

Here you're checking if alien1.x is equal to 40, and then you're returning either true or false inside that if statement. But what would happen if alien1.x was not equal to 40? Then that if statement would never be entered, and this function would not return anything. That's a violation of Processing's rules, which is why you're getting the error. This function needs to return something even if the if statement is not entered.

Even after you fix that, I'm suspicious of the check you're doing here. Do you really only want to check for alien1.x to be exactly 40? It's hard to help you without an MCVE, but my guess is that this is going to give you trouble down the road.